Last active
December 16, 2015 19:49
-
-
Save mrtimp/5487745 to your computer and use it in GitHub Desktop.
Implementing PHP style magic class methods (i.e. __call) 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 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