const objects (was Re: Death to tuples!)

Magnus Lycka lycka at carmen.se
Wed Dec 14 10:46:50 EST 2005


Gabriel Zachmann wrote:
> 
> I was wondering why python doesn't contain a way to make things "const"?
> 
> If it were possible to "declare" variables at the time they are bound to 
> objects that they should not allow modification of the object, then we 
> would have a concept _orthogonal_ to data types themselves and, as a 
> by-product, a way to declare tuples as constant lists.
> 
> So this could look like this:
> 
>     const l = [1, 2, 3]

That was a bit confusing. Is it the name 'l' or the list
object [1, 2, 3] that you want to make const? If you want
to make the list object immutable, it would make more sense
to write "l = const [1, 2, 3]". I don't quite see the point
though.

If you could write "const l = [1, 2, 3]", that should logically
mean that the name l is fixed to the (mutable) list object
that initially contains [1, 2, 3], i.e. l.append(6) is OK,
but l = 'something completely different" in the same scope
as "const l = [1, 2, 3]" would be forbidden.

Besides, what's the use case for mutable numbers for instance,
when you always use freely rebindable references in your source
code to refer to these numbers. Do you want to be able to play
nasty tricks like this?

 >>> f = 5
 >>> v = f
 >>> v++
 >>> print f
6

It seems to me that you don't quite understand what the
assignment operator does in Python. Please read
http://effbot.org/zone/python-objects.htm




More information about the Python-list mailing list