Death to tuples!

Antoon Pardon apardon at forel.vub.ac.be
Wed Nov 30 09:31:47 EST 2005


On 2005-11-30, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> Antoon Pardon wrote:
>> But lets just consider. Your above code could simply be rewritten
>> as follows.
>> 
>>   res = list()
>>   for i in range(10):
>>      res.append(i*i)
>> 
> I don't understand your point here? You want list() to create a new list 
> and [] to return the same (initially empty) list throughout the run of the 
> program?

No, but I think that each occurence returning the same (initially empty)
list throughout the run of the program would be consistent with how
default arguments are treated.
>
>> 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.
>
> The left one is equivalent to:
>
> __anon = []
> def Foo(l):
>    ...
>
> Foo(__anon)
> Foo(__anon)

So, why shouldn't: 

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

be equivallent to:

  __anon = list()
  ...

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

> The left has one list created outside the body of the function, the right 
> one has two lists created outside the body of the function. Why on earth 
> should these be the same?

Why on earth should it be the same list, when a function is called
and is provided with a list as a default argument?

I see no reason why your and my question should be answered differently.

> Or to put it even more simply, it seems that you are suggesting:
>
> __tmp = []
> x = __tmp
> y = __tmp
>
> should do the same as:
>
> x = []
> y = []

No, I'm not suggesting it should, I just don't see why it should be
considered a problem if it would do the same, provided this is the
kind of behaviour we already have with list as default arguments.

Why is it a problem when a constant list is mutated in an expression,
but isn't it a problem when a constant list is mutated as a default
argument?

-- 
Antoon Pardon



More information about the Python-list mailing list