Reputation: 1056
In this code:
def online_only(func, self):
def f(*args, **kwargs):
if self.running:
return func(*args, **kwargs)
else:
return False
return f
class VM(object):
@property
def running(self):
return True
@property
@online_only
def diskinfo(self):
return True
I want diskinfo
to run only when VM.running
returned True
. How can I get online_only
to be able to read self.running
?
Upvotes: 4
Views: 3065
Reputation: 229593
self
is passed as the first parameter to the wrapping function, so just treat the first parameter specially in f
:
def online_only(func):
def f(self, *args, **kwargs):
if self.running:
return func(self, *args, **kwargs)
else:
return False
return f
Upvotes: 7
Reputation: 88747
def online_only(func, self)
? it will raise TypeError, so change it to def online_only(func)
def online_only(func):
def f(self):
if self.running:
return func(self)
else:
return False
return f
class VM(object):
@property
def running(self):
return True
@property
@online_only
def diskinfo(self):
return True
print VM().diskinfo
Upvotes: 1