Skip to content

Instantly share code, notes, and snippets.

@aerosol
Forked from AeroNotix/t.py
Created May 14, 2014 19:25
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 aerosol/cec02b4147610796faed to your computer and use it in GitHub Desktop.
Save aerosol/cec02b4147610796faed to your computer and use it in GitHub Desktop.
def random_device(reuse_owner=False, **kwds):
def wrapper(f):
def wrapped(self):
if reuse_owner and hasattr(self, 'device'):
print "Reusing owner %s" % self.device
d = self.device
else:
print "no owner"
d = 'A NEW DEVICE'
self.device = d
return f(self)
return wrapped
return wrapper
class Foo(object):
def __init__(self):
self.x = 10
@random_device(reuse_owner=True)
def something(self, **kwargs):
print kwargs
print self.x
print self.device
Foo().something()
# Reusing owner lol
# {}
# 10
# lol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment