Velocity Reviews > Re: pep 8 constants

# Re: pep 8 constants

Brendan Miller
Guest
Posts: n/a

 01-14-2009
> FOO = 1
>
> def f(x=FOO):
> ...
>
>
>
> def f(x=1):
> ...

I tend to use constants as a means of avoiding the proliferation of
magic literals for maintenance reasons... Like say if your example of
FOO would have been used in 10 places. Maybe it is more pythonic to
simply denote such a thing as simply a normal variable? That doesn't
seem to give a hint that it shouldn't be assigned a second time.

Steven D'Aprano
Guest
Posts: n/a

 01-14-2009
On Tue, 13 Jan 2009 23:26:54 -0800, Brendan Miller wrote:

> I tend to use constants as a means of avoiding the proliferation of
> magic literals for maintenance reasons... Like say if your example of
> FOO would have been used in 10 places. Maybe it is more pythonic to
> simply denote such a thing as simply a normal variable?

But it isn't a "normal variable", it's a named constant, or at least it
would be if Python enforced constanticity. Or constantness. Or whatever.

> That doesn't
> seem to give a hint that it shouldn't be assigned a second time.

Absolutely. It's rather sad that I can do this:

import math
math.pi = 3.0

I like the ability to shoot myself in the foot, thank you very much, but
I should at least get a warning when I'm about to do so:

math.PI = 3.0 # use God-like powers to change a constant

Changing the laws of physics, one fundamental constant at a time-ly y'rs,

--
Steven

MRAB
Guest
Posts: n/a

 01-18-2009
Francesco Bochicchio wrote:
> On Wed, 14 Jan 2009 08:13:30 +0000, Steven D'Aprano wrote:
>
>
>> Absolutely. It's rather sad that I can do this:
>>
>> import math
>> math.pi = 3.0
>>
>> I like the ability to shoot myself in the foot, thank you very much, but
>> I should at least get a warning when I'm about to do so:
>>
>> math.PI = 3.0 # use God-like powers to change a constant
>>
>>

>
> 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
>
>>> class Constants(object):

def __setattr__(self, key, value):
if key in self.__dict__:
raise ValueError("Can't change constant")
self.__dict__[key] = value

>>> c = Constants()
>>> c.PI = 3.0
>>> c.PI

3.0
>>> c.PI = 4.0

Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
c.PI = 4.0
File "<pyshell#19>", line 4, in __setattr__
raise ValueError("Can't change constant")
ValueError: Can't change constant

Brendan Miller
Guest
Posts: n/a

 01-20-2009
> Constants would be a nice addition in python, sure enough.

My original question was about PEP-8 and whether it is pythonic to use
all caps to denote a variable that shouldn't be changed. More of a
style question than a language question.

I actually think *enforcing* constantness seems to go against the
grain of the language so to speek

Steven D'Aprano
Guest
Posts: n/a

 01-20-2009
On Mon, 19 Jan 2009 19:11:16 -0800, Brendan Miller wrote:

>> Constants would be a nice addition in python, sure enough.

>
> My original question was about PEP-8 and whether it is pythonic to use
> all caps to denote a variable that shouldn't be changed. More of a style
> question than a language question.
>
> I actually think *enforcing* constantness seems to go against the grain
> of the language so to speek

Why? Python has an infinite number of constants. The only difference is
that they are immutable objects, not names.

But regardless... yes, it is in my opinion Pythonic to use ALLCAPS to
designate constants (by convention). It isn't in PEP 8, but I don't think
that matters unless PEP 8 suggests a different convention.

--
Steven

Rhodri James
Guest
Posts: n/a

 01-21-2009
On Tue, 20 Jan 2009 04:58:30 -0000, Ben Finney
<(E-Mail Removed)> wrote:

> Unless someone's going to argue that “Variable Names” doesn't apply to
> constant names, even though Python doesn't make the distinction.

Python doesn't make the distinction, which is precisely why making the
distinction through CONVENTIONAL_NAMING_PRACTICE is useful.

--
Rhodri James *-* Wildebeeste Herder to the Masses

Brian Allen Vanderburg II
Guest
Posts: n/a

 01-22-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>
> Ciao
> ----
> FB
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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 ...

Brian Vanderburg II

Steve Holden
Guest
Posts: n/a

 01-23-2009
Brian Allen Vanderburg II wrote:
> (E-Mail Removed) 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
>>
>> Ciao
>> ----
>> FB
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

> 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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Ethan Furman
Guest
Posts: n/a

 02-25-2009
Steve Holden wrote:
> Brian Allen Vanderburg II wrote:
>
>>(E-Mail Removed) 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
>>>
>>>Ciao
>>>----
>>>FB

>>
>>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.
--
~Ethan~

Steve Holden
Guest
Posts: n/a

 02-25-2009
Ethan Furman wrote:
> Steve Holden wrote:
>> Brian Allen Vanderburg II wrote:
>>
>>> (E-Mail Removed) 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
>>>>
>>>> Ciao
>>>> ----
>>>> FB
>>>
>>> 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.

But that's at the class level, not the module level.

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