List mutation method gotcha - How well known?

Jarek Zgoda jzgoda at o2.usun.pl
Fri Mar 14 05:57:41 EDT 2008


Lie napisał(a):

>> foo = [1,2,3,4]
>> x = foo.append(5)
>> print x
>>
>> What will be the output (choose one):
>>
>> 1)  [1,2,3,4]
>> 2)  [1,2,3,4,5]
>> 3)  That famous picture of Albert Einstein sticking out his tongue
>> 4)  Nothing - no output
>> 5)  None of the above
>>
>> I undertake to summarise answers posted to complete this "survey".
> 
> I think I'll choose 3. Well, no, I suppose the correct behavior
> _should_ be undefined (i.e. what it returns is an implementation
> details that should not be relied on). The fact that it returns None
> is just a "coincidence" that happens to happen every time you tested
> it (you can't prove by ignorance)

I think in Python there's no notion of "void" return type. Deliberate
choice to return None for functions that modify objects in place seems
to be OK as long as it is used consistently and documented. Which is the
case.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: zgoda at jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)



More information about the Python-list mailing list