tough-to-explain Python

John Nagle nagle at animats.com
Sat Jul 11 02:02:19 CEST 2009


Bearophile wrote:
> kj, as Piet van Oostrum as said, that's the difference between mutable
> an immutable. It comes from the procedural nature of Python, and
> probably an explanation of such topic can't be avoided if you want to
> learn/teach Python.

     The problem with Python's mutable/immutable distinction is that
it's not very visible.  In a language with no declarations, no named
constants, and no parameter attributes like "value", "in", "out", or
"const", it's not at all obvious which functions can modify what.
Which is usually the place where this causes a problem.

     The classic, of course, is

def additemtolist(item, lst = []) :
     lst.append(item)
     return(lst)

lista = additemtolist(1)
listb = additemtolist(2)
print(listb)

The general problem is inherent in Python's design.  This particular problem,
though, results in the occasional suggestion that parameters shouldn't
be allowed to have mutable defaults.

				John Nagle



More information about the Python-list mailing list