How do you create constants?

Quinn Dunkan quinn at bolivar.ugcs.caltech.edu
Sat Oct 28 16:43:54 EDT 2000


On Sat, 28 Oct 2000 17:27:17 GMT, marc cheatham <marcc at yieldworks.com> wrote:
>For safety. I like knowing there is no way for myself or anyone else can
>modify this value at run time. These types of bugs can be very hard to
>find, especially in large projects. By making them immutable you avoid
>the problem of someone modifying your constant, you always know what
>that value will be.

I think python's rationale here is the same as the dynamic typing rationale:
documentation and testing is preferred over implementing complicated
restrictions on programmers 'for their own good'.

I must admit I've never worked on any multi-developer python projects, but I
think that if you have a module called 'consts' or a convention like ALLCAPS
constants and some programmer goes and writes code that modifies them, you're
better off educating or losing that programmer than implementing restrictions
in the language.  Even if you have no convention, if your fellow programmers
don't all have an idea about "if you play outside of your scope make sure you
know what you're playing with" your project is doomed anyway.  Once again, I
don't actually have any experience with it, so if you've worked on
multi-developer python projects and had this issue come up I'm interested in
hearing about it.

One real use I can think of for enforced immutable values is efficiency (e.g.,
sather's immutable classes which enable some optimization), and even though
python is not an efficiency-minded language, it still provides tuples (and
immutable strings which probably also has something to do with the popularity
of keying dicts with them).

>I do not like global variables and only use them when I have no other
>choice. With Python it seems you are forced to use global variables as
>constants which opens yourself up to lots of potential problems. I think
>this is one of the few drawbacks of Python. A good discussion on global
>variables can be found in "Code Complete" by Steve McConnell.

No, you're not (forced to use 'globals' for constants):

class A:
    class_constant1 = 'spam'
    class_constant2 = 1,2,3
    def __init__(self, args):
        local_constant = 42
        if arg == self.__class__.class_constant1:
            # blah blah

And anyway, python doesn't really have globals, so I'm not sure that a book
about them has much value for it :)

>I also agree with Fredrik, this and enums would be a very features to
>add to Python 2.2
>Marc
>
>Fredrik Lundh wrote:
>
>> Tom Wright wrote:
>> > shame they cant be made immutable once they have been set,
>> > feature for 2.1 ??? ;-)
>>
>> can you explain why you think you need immutable
>> "constants"?
>>
>> </F>

I think you're misquoting him here, Tom Wright made the suggestion.  I do like
the idea of enumerated types, although their main use is in a static-typing
context.  Not that they don't make *any* sense in python: python already has a
very popular enumerated type: Int, which is effectively 

enum IntType: -(sys.maxint-1) | ... | -1 | 0 | 1 | ... | sys.maxint

So you can fake up one of your own by using ints or your own classes, but ints
don't print out nicely and classes or metaclasses are wordy and ugly.  So I
guess I'm stuck with strings (Foo='Foo'; Bar='Bar'), which work ok but are a
pain to type :)  Not to mention an average... hmm what would it be, log(n)?
compare time instead of constant, but that's a drop in the bucket (yes I could
use 'is' but that's dangerous, what if the class is pickled?).



More information about the Python-list mailing list