Re: how to cause a request for a missing class attribute cause its calculation
On 5/18/2010 4:54 PM, Chris Rebert wrote: snip Suggested reading: http://docs.python.org/library/functions.html#property I've placed a revision to this official *property* documentation at: http://wiki.python.org/moin/AlternativeDescriptionOfProperty There's also a gentle (I hope) intro to the *property* feature on the Wiki: http://wiki.python.org/moin/ComputedAttributesUsingPropertyObjects -John -- http://mail.python.org/mailman/listinfo/python-list
how to cause a request for a missing class attribute cause its calculation
Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y inst = foo(1,2) inst.xy # no value, but I what this to cause the calculation of inst.xy I don't what to have self.xy calculated before it is called. Thanks *Vincent Davis 720-301-3003 * vinc...@vincentdavis.net my blog http://vincentdavis.net | LinkedInhttp://www.linkedin.com/in/vincentdavis -- http://mail.python.org/mailman/listinfo/python-list
Re: how to cause a request for a missing class attribute cause its calculation
On Tue, May 18, 2010 at 1:39 PM, Vincent Davis vinc...@vincentdavis.net wrote: Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y inst = foo(1,2) inst.xy # no value, but I what this to cause the calculation of inst.xy I don't what to have self.xy calculated before it is called. class Foo(object): def __init__(self, x, y): self.x = x self.y = y @property def xy(self): '''Will calculate x+y every time''' return self.x + self.y Or if Foo is immutable and you wish to cache x+y: class Foo(object): def __init__(self, x, y): self.x = x self.y = y self._xy = None @property def xy(self): if self._xy is None: self._xy = self.x + self.y return self._xy Suggested reading: http://docs.python.org/library/functions.html#property Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: how to cause a request for a missing class attribute cause its calculation
Vincent Davis wrote: Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y ^ this needs to be self.x + self.y inst = foo(1,2) inst.xy # no value, but I what this to cause the calculation of inst.xy I don't what to have self.xy calculated before it is called. My current favorite method: def __getattr__(self, name): if name != 'xy': raise AttributeError(%s not found % name) self.xy = self.x + self.y return self.xy ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: how to cause a request for a missing class attribute cause its calculation
Ethan Furman wrote: Vincent Davis wrote: Lets say I have class foo(object): def __init__(self, x, y): self.x=x self.y=y def xplusy(self): self.xy = x+y ^ this needs to be self.x + self.y inst = foo(1,2) inst.xy # no value, but I what this to cause the calculation of inst.xy I don't what to have self.xy calculated before it is called. My current favorite method: def __getattr__(self, name): if name != 'xy': raise AttributeError(%s not found % name) self.xy = self.x + self.y return self.xy ~Ethan~ Chris' reply is more on-point, I think -- I was thinking of attributes that are only calculated once. *sigh* ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list