pep 8 constants

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Feb 25 04:03:17 EST 2009

Brian Allen Vanderburg II a écrit :
> bockman at wrote:
>> Constants would be a nice addition in python, sure enough.
>> But I'm not sure that this can be done without a run-time check every 
>> time
>> the constant is used, and python is already slow enough. Maybe a check
>> that is disabled when running with optimizing flags ?
>> But I'm sure this discussion has been already made and the FINAL WORD has
>> been already spoken.
> 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 ...

There are a couple problems with this suggestion:

- it would require modifying lookup rules to allow the protocol 
descriptor to be invoked on instance attributes[1] - which is not 
actually the case, by design.

- it adds the overhead of a method and a function call for what is 
mostly a simple "constant" attribute lookup.

FWIW, while it would already work for class-level pseudo-constants 
(using a very simple custom descriptor), I'd qualify such usage as a WTF.

More information about the Python-list mailing list