How do you create constants?

Steve Horne sh at ttsoftware.co.uk
Mon Nov 6 12:03:10 EST 2000


OK, I can understand your concern, but even so - in any language there
are ways to break the const-ness of data. In C and C++, for example,
you can directly typecast using const_cast which exists precisely for
that purpose. In pascal, you'd probably have to use pointer tricks,
but it could be done. At the end of the day, const-ness is only
assured when the data is in ROM - not likely in a desktop app.

When I call these things paranoia constructs, its not really an
insult. I still use them when appropriate. But these things only
protect against accidental misuse - not against someone who is tempted
to do things he shouldn't.

Accidental modification of something that should be constant is not,
in my experience, a common error. It is an occasional hint that you
have assigned to the wrong variable, but mismatched types are much
more effective at this.

In Python, variables do not have a type - only values have type. So a
much more effective accidental-incorrect-assignment catcher would be
the ability to restrict a variable to holding a particular data type.

If you merely want to make lists and dictionaries immutable - OK, I
can see that. But consider the following (if I invent an immutable
keyword for a sec)...

  # initial assignment

  list_var = immutable [1, 2, 3]

  # So that list cannot be changed, eh?  What about...

  list_var = filter (lambda i : i != 2, list_var)

In short, just because the list is immutable, doesn't mean you can't
create a modified copy of the list and then assign it back to the same
variable.

Of course, if you make the variable 'list_var' into a constant, so
further assignments are no longer allowed, then all that need be done
to break that is to make a copy of the programs symbol-table
dictionary which excludes that one symbol, then recreate it as a
variable, then assign the copy back to the main python dictionary.

So I stand by a slightly modified version of my original argument -
Python is a scripting language. This gives it some benefits, but also
has some complementary costs. For example, the reduced development
time for moderate-sized applications in Python is largely because it
is an easy-going kind of language. If you add all the paranoia
constructs from other languages, python will lose this easy-going
nature, and development times will increase.

I wouldn't mind seeing a paranoid variant of python - something
perhaps designed to compete with ada for very-large-very-secure
applications - but I'd hate to see easy-going python die as a result.

-- 
Steve Horne
sh at ttsoftware.co.uk



More information about the Python-list mailing list