Defining/declaring constants in Python

Christopher Koppler klapotec at chello.at
Mon Jul 28 14:03:38 EDT 2003


On 28 Jul 2003 10:49:58 -0700, swenggsri at yahoo.com (Sriram
Chadalavada) wrote:

>Hello everyone,
>  I am a newbie to Python with experience in C programming. For my
>project, I am re-writing C routines as Python functions.
>      I am currently using raw numerical values and  was wondering if
>there is an equivalent of #define in Python for declaring constants.

Python doesn't need no steenkeeng conztantz!

More seriously though, no, there's no equivalent for constants in
Python, as the programmer is generally considered intelligent enough
to leave a value he wants to stay constant alone. So just assign your
raw numerical value to whatever name you think fits best, like any
other variable, and then don't assign it any other value. Accepted
usage is to write the name in ALL CAPS, though - ah, just like in good
old C!

> 	 Is there an elegant way of defining constants in Python? Please let
>me know.

If you really, really, REALLY want a varia^Wconstant that cannot be
changed, no matter how hard you try, in Python, have a look at this:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207

Christopher




More information about the Python-list mailing list