[Baypiggies] Fwd: nested list help
Aahz
aahz at pythoncraft.com
Mon Aug 2 19:58:20 CEST 2010
On Tue, Jul 27, 2010, Robert Zuber wrote:
>
> I too find myself wishing that list methods returned a reference
> to the list so that I could chain those calls or use them in list
> comprehensions. Look forward to anyone's input on why they don't.
It's a signal. Here's a fake example for why they don't:
>>> a = [2, 7, 3, 0]
>>> b = a.sort()
>>> a
[0, 2, 3, 7] # WTF?!?!?!?!
IOW, it's too easy to forget about shared references, so mutating methods
return None. However, Python does provide (at least in this case) a
simple solution:
>>> a = [2, 7, 3, 0]
>>> b = sorted(a)
>>> a
[2, 7, 3, 0]
>>> b
[0, 2, 3, 7]
See also
http://docs.python.org/faq/design.html#why-doesn-t-list-sort-return-the-sorted-list
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"....Normal is what cuts off your sixth finger and your tail..." --Siobhan
More information about the Baypiggies
mailing list