How do you create constants?

Rainer Deyke root at rainerdeyke.com
Mon Oct 30 16:46:52 EST 2000


"Huaiyu Zhu" <hzhu at users.sourceforge.net> wrote in message
news:slrn8vroiv.2sc.hzhu at rocket.knowledgetrack.com...
> So a hyperthetical Python with the const keyword might look like this:
>
> x = const "a string" # as current string
> x = "a string"       # a true class object that's mutable
> x = [1, 2, 3]        # as current list
> x = const [1, 2, 3]  # as current tuple
> const x = [1, 2, 3]  # x is fixed to a list whose contents can change
> const x = const [1, 2, 3] # x is fixed to a const list (tuple)
> x = const {1:2, 3:4} # a dict whose contents cannot change

Two thoughts:

1. The usage "const x = ..." means that the __dict__ is made partially
immutable.  This is possible, but probably more trouble than it's worth.

2. It is not obvious wether const-ness applies to objects or references.  I
vote for the latter.

a = [1, 2, 3]
b = const a
a[0] = 5
print b # --> '[5, 2, 3]'
b[0] = 1 # Raises ConstError

> Is this a good thing?
> - It gives programmers more expresiveness when needed.
> - It does not force a change in programming style if not wanted.
> - It may allow substantial efficiency gains.

Actually it would probably hurt performance, because there is more
information to keep track of.

> - It allows more consistency for things like augmented assignment.

No.

> - It may be combined with safe exec module to build other security
modules.
>
> Any potential problems?
> What's the implementational cost?

Depends on the implementation.  One bit/byte per object or per reference,
which really makes four bytes because of alignment issues.  Alternately,
each type could have a const version, which means each object needs to be
converted to the const type.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list