Created
June 17, 2016 10:48
-
-
Save zeffii/778b44dca6b85d5de2fa2b24c22c4ce1 to your computer and use it in GitHub Desktop.
dynamic class methods
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 DemoClass: | |
def __init__(self): | |
self.power = 2 | |
def __getattr__(self, name): | |
def method(*args, **kw): | |
if name in ['work', 'stereo']: | |
if isinstance(kw, dict) and kw: | |
print('a dict', kw) | |
elif isinstance(args, tuple) and args: | |
print('a tuple', args) | |
return method | |
f = DemoClass() | |
f.work(damage=20, reverse=-1) | |
f.work(20, 1) | |
""" | |
a dict {'damage': 20, 'reverse': -1} | |
a tuple (20, 1) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment