Defining/declaring constants in Python

Discussion in 'Python' started by Sriram Chadalavada, Jul 28, 2003.

  1. 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
     
    Sriram Chadalavada, Jul 28, 2003
    #1
    1. Advertising

  2. On 28 Jul 2003 10:49:58 -0700, (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
     
    Christopher Koppler, Jul 28, 2003
    #2
    1. Advertising

  3. Christopher Koppler <> 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/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

    --
    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
     
    Michael Hudson, Jul 29, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Troy Knight

    declaring constants

    Troy Knight, Jul 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    508
  2. johny smith
    Replies:
    8
    Views:
    447
    Peter Koch Larsen
    Jul 2, 2004
  3. Generic Usenet Account

    Declaring constants within the scope of a class

    Generic Usenet Account, Mar 15, 2005, in forum: C++
    Replies:
    5
    Views:
    3,621
    Howard
    Mar 15, 2005
  4. Declaring constants

    , May 29, 2006, in forum: VHDL
    Replies:
    5
    Views:
    723
  5. Gary Wessle
    Replies:
    1
    Views:
    293
    Heinz Ozwirk
    Aug 23, 2006
Loading...

Share This Page