Skip to content

Instantly share code, notes, and snippets.

@yhunko
Last active April 23, 2021 13:44
Show Gist options
  • Save yhunko/088fcdc497802f2c0f05c9b3ff111d6c to your computer and use it in GitHub Desktop.
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
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