Death to tuples!

Antoon Pardon apardon at forel.vub.ac.be
Wed Nov 30 04:48:10 EST 2005


On 2005-11-29, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> Antoon Pardon wrote:
>
>> The question is, should we consider this a problem. Personnaly, I 
>> see this as not very different from functions with a list as a default
>> argument. In that case we often have a list used as a constant too.
>> 
>> Yet python doesn't has a problem with mutating this list so that on
>> the next call a 'different' list is the default. So if mutating
>> a list used as a constant is not a problem there, why should it
>> be a problem in your example?
>> 
> Are you serious about that?

I'm at least that serious that I do consider the two cases
somewhat equivallent.

> The semantics of default arguments are quite clearly defined (although 
> suprising to some people): the default argument is evaluated once when the 
> function is defined and the same value is then reused on each call.
>
> The semantics of list constants are also clearly defined: a new list is 
> created each time the statement is executed. Consider:
>
>   res = []
>   for i in range(10):
>      res.append(i*i)
>
> If the same list was reused each time this code was executed the list would 
> get very long. Pre-evaluating a constant list and creating a copy each time 
> wouldn't break the semantics, but simply reusing it would be disastrous.

This is not about how things are defined, but about should we consider
it a problem if it were defined differently. And no I am not arguing
python should change this. It would break too much code and would
make python all the more surprising.

But lets just consider. Your above code could simply be rewritten
as follows.

  res = list()
  for i in range(10):
     res.append(i*i)

Personnaly I think that the two following pieces of code should
give the same result.

  def Foo(l=[]):                def Foo(l):
    ...                           ...
    
  Foo()                         Foo([])
  Foo()                         Foo([])

Just as I think, you want your piece of code to give the same
result as how I rewrote it.

I have a problem understanding people who find the below don't
have to be equivallent and the upper must.

-- 
Antoon Pardon



More information about the Python-list mailing list