My question is are the following two pieces of code run the same by the interpreter:
class A(object):
def __init__(self):
self.__x = None
@property
def x(self):
if not self.__x:
self.__x = ... #some complicated action
return self.__x
and the much simpler:
class A(object):
@property
def x(self):
return ... #some complicated action
I.e., is the interpreter smart enough to cache the property x
?
My assumption is that x
does not change - finding it is hard, but once you find it once there is no reason to find it again.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…