Defining constant strings

Discussion in 'Python' started by Hans, Aug 27, 2006.

  1. Hans

    Hans Guest

    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
     
    Hans, Aug 27, 2006
    #1
    1. Advertising

  2. Hans

    Tal Einat Guest

    Hans wrote:
    > 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


    It is really not possible.

    The Pythonic way (as far as I have come to know it) is to stop trying
    to protect yourself from yourself, and just don't change something
    which should be constant. Good documentation (including in-code
    comments) and good design are better for preventing programming errors
    than defining things as constant.

    Actually, you can't really define anything as constant in C either. A
    #define can be overriden, a 'const' variable can be casted. These are
    merely conventions which are enforced to some degree by compilers, but
    can easily be worked around.

    There are conventions for constant values in Python too. Usually,
    variables with an all uppercase name are used. As for variables,
    functions, attributes, methods etc. which shouldn't be changed/used
    outside a certain module/class: these are usually prefixed with an
    underscore ("_"). These are not enforced by the interpreter at all,
    though.


    As a side note, Python strings are actually all constants, or
    "immutable" in Python-ish. The variables which reference string objects
    can be changed to reference any other object - that's the nature of
    Python variables. But the strings themselves don't change.

    - Tal
     
    Tal Einat, Aug 27, 2006
    #2
    1. Advertising

  3. Hans

    Tal Einat Guest

    Hans wrote:
    > 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


    One last note:

    If you truly insist on having constant variables, you could write a
    class which implements such behavior. You can write a simple class
    which answers your specific needs quite easily.

    Or you could go for a more generic approach, such as this:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/59878

    - Tal
     
    Tal Einat, Aug 27, 2006
    #3
  4. Hans

    Peter Otten Guest

    Hans wrote:

    > 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?


    No, this is not really not possible in Python:

    $ ls
    preprocess.pyp
    $ cat preprocess.pyp
    #define MYSTRING "Hello, world"
    def f():
    print "Goodbye, " MYOTHERSTRING
    print MYSTRING
    f()
    $ gcc -DMYOTHERSTRING="'sanity'" -xc -E preprocess.pyp -o preprocess.py
    $ python preprocess.py
    Hello, world
    Goodbye, sanity
    $

    :)

    Peter
     
    Peter Otten, Aug 27, 2006
    #4
  5. Hans

    danielx Guest

    I would really like to highlight something Tal has already said: Python
    strings are immutable. That means if you construct a string object, you
    don't have to worry about someone else going in and changing that
    object. What might happen, however, is that someone might reassign a
    variable you have which points to that object. You can tell people not
    to do this (although you can't force them) using a mechanism Tal has
    also described. You give your variable a name which is all caps.

    Hans wrote:
    > 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
    > ------=_NextPart_000_004E_01C6C9FF.C5137CF0
    > Content-Type: text/html; charset=iso-8859-1
    > Content-Transfer-Encoding: quoted-printable
    > X-Google-AttachSize: 902
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <HTML><HEAD>
    > <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
    > <META content="MSHTML 6.00.2900.2604" name=GENERATOR>
    > <STYLE></STYLE>
    > </HEAD>
    > <BODY bgColor=#e6e3df>
    > <DIV><FONT face=Arial size=2>Hi,</FONT></DIV>
    > <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    > <DIV><FONT face=Arial size=2>I want to define a couple of constant strings, like
    > in C:</FONT></DIV>
    > <DIV><FONT face=Arial size=2>#define mystring "This is my string"</FONT></DIV>
    > <DIV><FONT face=Arial size=2>or using a const char construction.</FONT></DIV>
    > <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    > <DIV><FONT face=Arial size=2>Is this really not possible in Python?</FONT></DIV>
    > <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    > <DIV><FONT face=Arial size=2>Hans</FONT></DIV></BODY></HTML>
    >
    > ------=_NextPart_000_004E_01C6C9FF.C5137CF0--
     
    danielx, Aug 27, 2006
    #5
    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. valentin tihomirov

    defining a flag-dependent constant

    valentin tihomirov, Nov 25, 2004, in forum: VHDL
    Replies:
    3
    Views:
    537
    Jim Lewis
    Nov 28, 2004
  2. johny smith
    Replies:
    8
    Views:
    443
    Peter Koch Larsen
    Jul 2, 2004
  3. Jani Yusef
    Replies:
    8
    Views:
    418
    Tim Peters
    Sep 21, 2004
  4. Joe Van Dyk

    Defining a constant in a class

    Joe Van Dyk, Apr 20, 2006, in forum: C++
    Replies:
    8
    Views:
    339
    Victor Bazarov
    Apr 21, 2006
  5. Rick C. Hodgin

    Non-constant constant strings

    Rick C. Hodgin, Jan 19, 2014, in forum: C Programming
    Replies:
    561
    Views:
    2,095
    David Brown
    Feb 24, 2014
Loading...

Share This Page