Last active
April 23, 2021 13:44
-
-
Save yhunko/088fcdc497802f2c0f05c9b3ff111d6c to your computer and use it in GitHub Desktop.
This example shows how dynamically defined ptotected method could call private one in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test(): | |
# This method defines a dynamic method | |
def _method1(self, name): | |
# Creating a dynamic protected method | |
# which calls the potected `method2` | |
exec("self._%s = self._%s__method2" % (name, __class__.__name__)) | |
def __method2(self): | |
print("from method2") | |
def _method3(self): | |
# Calling a dynamically defined | |
# protected method | |
self._test() | |
classInstance = Test() | |
classInstance._method1("test") | |
classInstance._method3() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment