Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Defining/declaring constants in Python (http://www.velocityreviews.com/forums/t320213-defining-declaring-constants-in-python.html)

Sriram Chadalavada 07-28-2003 05:49 PM

Defining/declaring constants in Python
 
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

Christopher Koppler 07-28-2003 06:03 PM

Re: Defining/declaring constants in Python
 
On 28 Jul 2003 10:49:58 -0700, swenggsri@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/Coo...n/Recipe/65207

Christopher

Michael Hudson 07-29-2003 11:56 AM

Re: Defining/declaring constants in Python
 
Christopher Koppler <klapotec@chello.at> writes:

> 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/Coo...n/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

--
MAN: How can I tell that the past isn't a fiction designed to
account for the discrepancy between my immediate physical
sensations and my state of mind?
-- The Hitch-Hikers Guide to the Galaxy, Episode 12


All times are GMT. The time now is 05:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.