pep 8 constants

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 24 22:52:45 EST 2009


En Tue, 24 Feb 2009 22:52:20 -0200, Ethan Furman <ethan at stoneleaf.us>  
escribió:

> Steve Holden wrote:
>> Brian Allen Vanderburg II wrote:
>>
>>> 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.
>>  regards
>>  Steve
>
> --> 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.

Note that:
  - you defined the property inside the *class* tester
  - then, you created an *instance* of such class
  - you retrieve pi from the instance
  - if you retrieve pi from the class (tester.pi), you get a property  
object, not a float.
  - if you directly attach the property to the instance, testee.pi returns  
a property object, not a float.
Finally, consider that modules are instances of type `module`; all modules  
are instances of the same type.

How do you propose to make properties work at the module level?

-- 
Gabriel Genellina




More information about the Python-list mailing list