I have the following decorator that saves the configuration file after calling the method decorated with @saveconfig :
class saveconfig(object): def __init__(self, f): self.f = f def __call__(self, *args): self.f(object, *args) # Here i want to access "cfg" defined in pbtools print "Saving configuration"
I use this decorator inside the next class. After calling the createkvm method, the createkvm configuration self.cfg must be saved inside the decorator:
class pbtools() def __init__(self): self.configfile = open("pbt.properties", 'r+') # This variable should be available inside my decorator self.cfg = ConfigObj(infile = self.configfile) @saveconfig def createkvm(self): print "creating kvm"
My problem is that I need to access the self.cfg object self.cfg inside the saveconfig decorator. The first naive approach was to add a parameter to the decorator that holds the object, for example @saveconfig(self) , but this does not work.
How can I access the variables of a method host object inside a decorator? Do I have to define a decorator inside the same class in order to access?
python decorator
ifischer
source share