pep 8 constants

Steve Holden steve at holdenweb.com
Tue Feb 24 22:57:50 EST 2009


Gabriel Genellina wrote:
> 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?
> 
Thanks, Gabriel. I was beginning to think there was something terribly
obvious I had completely missed.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list