[Python-ideas] Extend module objects to support properties (original) (raw)

Ethan Furman ethan at stoneleaf.us
Thu Jan 31 05:22:50 CET 2013


On 01/30/2013 05:53 PM, Larry Hastings wrote:

On 01/30/2013 04:45 PM, Steven D'Aprano wrote: On 31/01/13 03:42, Larry Hastings wrote:

Also, I'm not sure there are any existing globals that we'd want to convert into properties. How about this? math.pi = 3 which really should give an exception. (I'm sure there are many others.) Well, hmm. The thing is, properties--at least the existing implementation with classes--doesn't mesh well with direct access via the dict. So, right now, >>> math.dict['pi'] 3.141592653589793 If we change math.pi to be a property it wouldn't be in the dict anymore. So that has the possibility of breaking code.

So make the property access the dict:

--> class Test(object): ... @property ... def pi(self): ... return self.dict['pi'] ... @pi.setter ... def pi(self, new_value): ... self.dict['pi'] = new_value ... --> t = Test() --> t <__main__.Test object at 0x7f165d689850> --> t.pi = 3.141596 --> t.pi 3.141596 --> t.dict['pi'] = 3 --> t.pi 3

Ethan



More information about the Python-ideas mailing list