Skip to content

Instantly share code, notes, and snippets.

@mrtimp
Last active December 16, 2015 19:49
Show Gist options
  • Save mrtimp/5487745 to your computer and use it in GitHub Desktop.
Save mrtimp/5487745 to your computer and use it in GitHub Desktop.
Implementing PHP style magic class methods (i.e. __call) in Python
class MyClass:
def _test(self, *args):
print "This is a dynamic method"
return
def __getattr__(self, name, *args):
if name == 'myFunction':
def function (*args):
return self._test(*args)
else:
def function (*args):
return False
return function
c = MyClass()
c.myFunction('testing', 1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment