Defining/declaring constants in Python

S

Sriram Chadalavada

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.
Is there an elegant way of defining constants in Python? Please let
me know.

Thanks,
Sriram
 
C

Christopher Koppler

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
 
M

Michael Hudson

Christopher Koppler said:
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

Of course, it's still possbile to change the values of these
'constants', you just have to muck about quite a bit to manage it.

Making something really, truly, constant in Python is probably
impossible (and certainly hard enough to be a silly thing to do in
real code).

Cheers,
mwh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top