What is a function parameter =[] for?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Nov 24 09:24:55 EST 2015


Op 24-11-15 om 15:00 schreef Chris Angelico:
> On Wed, Nov 25, 2015 at 12:43 AM, Antoon Pardon
> <antoon.pardon at rece.vub.ac.be> wrote:
>> I think that part of the problem is, that [] is not a constant object. So
>> that when you see a line like
>>
>>     ls = []
>>
>> It behaves more lke
>>
>>     ls = [].copy()
>>
>> than what you would expect with the normal python semantics.
> 
> You're still thinking in terms of [] being a literal. It isn't; the
> docs describe it as "list display", and it's closer to:
> 
> ls = list()

No I am not. The distinction you are making here is unimportant for
the point I am making. And that is that although

    v = ()
and
    v = []

look very similar. They behave differently.

> except that it doesn't look up the global name. Every time you call
> open(), you expect it to open a fresh file handle, right? (Even if you
> use the same file name.) And every time you call object(), you get a
> new and unique sentinel object. It's the same with list(), and it's
> the same with square brackets as well.
> 
> Start thinking of it as a constructor call rather than a literal, and
> you'll get past most of the confusion.

That doesn't change the fact it does look like a literal and not like
a constructor.

-- 
Antoon.



More information about the Python-list mailing list