Created
June 19, 2016 17:17
-
-
Save uolot/58c87ecfd82fa989f11cb166c4ff158f to your computer and use it in GitHub Desktop.
Create dynamic python object instance, instantly
This file contains hidden or 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
from functools import partial | |
def instance(**kwargs): | |
class instobj(object): | |
pass | |
o = instobj() | |
for k, v in kwargs.iteritems(): | |
if callable(v): | |
v = partial(v, o) | |
setattr(o, k, v) | |
return o | |
# Example | |
requests = instance( | |
get=lambda s: "Hello world!", | |
post=lambda s, d: d | |
) | |
print requests.get() | |
print requests.post({'a': 1}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment