Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created June 17, 2016 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/778b44dca6b85d5de2fa2b24c22c4ce1 to your computer and use it in GitHub Desktop.
Save zeffii/778b44dca6b85d5de2fa2b24c22c4ce1 to your computer and use it in GitHub Desktop.
dynamic class methods
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