RE: Python Global Constant

Discussion in 'Python' started by Michael Chermside, Jul 9, 2003.

  1. Krisztian Kepes wrote:
    > How [do] I create an global constant in [a] module [that] is
    > accessable in from other modules ?


    Christoph Becker-Freyseng replies:
    > What about using __builtins__ wouldn't make this the constant
    > really global?


    While adding it to __builtins__ *would* work like you're thinking,
    it's not necessary, or appropriate. Python provides lots of ways
    for "power users" to "get under the covers" and change things.
    This is useful in those situations where you really need it (some
    other languages provide NO way to get at fundamental language
    features), but such deep black magic should be reserved for those
    cases when you really need it. Modifying __builtins__ definitely
    falls into such a category: if you aren't sure whether you need
    to do it, then you don't. If you *think* you need to do it, then
    you shouldn't. If you understand what's going on so well that you
    are *certain* that you *must* modify it to achieve what you want...
    well, go ahead!

    In this case, the OP (original poster) was asking about a very
    common and straightforward situation: module level constants that
    need to be visible from other modules. There is a standard and
    straightforward way to create these: just assign a value to the
    variable in your module, then use it from other modules by
    importing the module. Spell the variable with ALL_CAPS to clearly
    mark that it is intended to be a constant and others shouldn't
    modify it. You'll need to watch out for cyclic import dependencies,
    but other than that this is a really easy solution.

    Now, some might complain that the variable isn't "constant"...
    spelling it with all caps tells others that they SHOULDN'T modify
    it, but doesn't PREVENT them from doing so. Really, that's a lot
    like the issue with __builtins__ that I mentioned above. It's very
    Pythonic to have a clear *indication* that something shouldn't be
    messed with (being in all caps or marking with double underscore),
    but not to *enforce* it -- if someone wants to change it they do
    so at their own risk. However, if you REALLY wanted to keep them
    from changing the "constant" (perhaps it controls the security
    level for running untrusted code) there are ways to do so -- check
    out the Python Cookbook for some examples.

    -- Michael Chermside


    -------------------------------------------------
    This mail sent through IMP: http://horde.org/imp/
    Michael Chermside, Jul 9, 2003
    #1
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,082
  2. Martin Magnusson
    Replies:
    2
    Views:
    502
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    556
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    337
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    12,917
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page