pep 8 constants

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Feb 25 10:07:00 CET 2009


Ethan Furman a écrit :
> Steve Holden wrote:
>> Brian Allen Vanderburg II wrote:
>>
(snip)
>>> One idea to make constants possible would be to extend properties to be
>>> able to exist at the module level as well as the class level:
>>>
>>> @property
>>> def pi():
>>>   return 3.14159.....
>>>
>>> print(pi) # prints 3.14159....
>>> pi=32 # Raise an error Cannot set attribute ...
>>>
>>
>> I don't understand why this would print 3.14159 ... instead of <function
>> __math__.pi>, or whatever.
>>
>> property would clearly have to do something very different in module
>> scope in order to make this work.
>>
> 
> --> class tester(object):
> ...   @property
> ...   def pi(self):
> ...     return 3.141596
> ...
> --> testee = tester()
> --> testee.pi
> 3.1415959999999998
> 
> Looks like that's how property works, so the same behavior on a module 
> level would do as Brian suggests.

s/module/instance/

At runtime, modules are instances of the module type - so 'module-level' 
names are really instance attributes of the module instance -, and the 
descriptor protocol is only invoked for class attributes.



More information about the Python-list mailing list