Situation
Similar to this question, I want to replace a property. Unlike that question, I do not want to override it in a sub-class. I want to replace it in the init and in the property itself for efficiency, so that it doesn't have to call a function which calculates the value each time the property is called.
I have a class which has a property on it. The constructor may take the value of the property. If it is passed the value, I want to replace the property with the value (not just set the property). This is because the property itself calculates the value, which is an expensive operation. Similarly, I want to replace the property with the value calculated by the property once it has been calculated, so that future calls to the property do not have to re-calculate:
class MyClass(object):
def __init__(self, someVar=None):
if someVar is not None: self.someVar = someVar
@property
def someVar(self):
self.someVar = calc_some_var()
return self.someVar
Problem
The above code does not work because doing self.someVar = does not replace the someVar function. It tries to call the property's setter, which is not defined.
Potential Solution
I know I can achieve the same thing in a slightly different way as follows:
class MyClass(object):
def __init__(self, someVar=None):
self._someVar = someVar
@property
def someVar(self):
if self._someVar is None:
self._someVar = calc_some_var()
return self._someVar
This will be marginally less efficient as it will have to check for None every time the property is called. The application is performance critical, so this may or may not be good enough.
Question
Is there a way to replace a property on an instance of a class? How much more efficient would it be if I was able to do this (i.e. avoiding a None check and a function call)?
This is basically the same as Denis Otkidach's
CachedAttribute
, but slightly more robust in that it allows either:or
Sure, you can set the attribute in the private dictionary of the class instance, which takes precedence before calling the property function
foo
(which is in the static dictionaryA.__dict__
)If you want to reset again to work on the property, just
del self.__dict__['foo']
What you are looking for is Denis Otkidach's excellent CachedAttribute:
It can be used like this:
Notice that accessing
foo.bar
subsequent times does not call the getter function. (Calculating self.bar
is not printed.)Deleting
foo.bar
fromfoo.__dict__
re-exposes the property defined inFoo
. Thus, callingfoo.bar
again recalculates the value again.The decorator was published in the Python Cookbook and can also be found on ActiveState.
This is efficient because although the property exists in the class's
__dict__
, after computation, an attribute of the same name is created in the instance's__dict__
. Python's attribute lookup rules gives precedence to the attribute in the instance's__dict__
, so the property in class becomes effectively overridden.You can change what code a function has by replacing the functions's
__code__
object with the__code__
object from another function.Here is a decorator function that I created to do just that for you. Feel free to modify it as you see fit. The big thing to remember though is that the both functions need to have the same number of 'free variables' to be swapped like this. This can easily be done by using nonlocal to force it (as shown below).