473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 18 '05 #1
2 25436
On 28 Jul 2003 10:49:58 -0700, sw*******@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
Jul 18 '05 #2
Christopher Koppler <kl******@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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
1
by: Troy Knight | last post by:
This may seem like a dumb question but I've only just got back into perl after a few years. I would be very grateful if someonme could answer this... I'm trying to define constants which can be...
38
by: Ted | last post by:
Is there a way to define a constant value for a color in a CSS declaration? I have numerous colors in different CSS elements, and if I make a change in color, I have to change all the reference...
29
by: Joe | last post by:
This is a very basic question, but why can't I do the following ? class schedule { private: static const string mScheduleFile = "schedule.txt"; .... }
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
17
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.