Skip to content

Instantly share code, notes, and snippets.

@uolot
Created June 19, 2016 17:17
Show Gist options
  • Save uolot/58c87ecfd82fa989f11cb166c4ff158f to your computer and use it in GitHub Desktop.
Save uolot/58c87ecfd82fa989f11cb166c4ff158f to your computer and use it in GitHub Desktop.
Create dynamic python object instance, instantly
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