Defining/declaring constants in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sriram Chadalavada

    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

    #2
    Re: Defining/declaring constants in Python

    On 28 Jul 2003 10:49:58 -0700, swenggsri@yahoo .com (Sriram
    Chadalavada) wrote:
    [color=blue]
    >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.[/color]

    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!
    [color=blue]
    > Is there an elegant way of defining constants in Python? Please let
    >me know.[/color]

    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:


    Christopher

    Comment

    • Michael Hudson

      #3
      Re: Defining/declaring constants in Python

      Christopher Koppler <klapotec@chell o.at> writes:
      [color=blue]
      > 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[/color]

      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

      Comment

      Working...