Inconsistent reaction to extend

Fredrik Lundh fredrik at pythonware.com
Fri Sep 9 06:06:20 EDT 2005


Jerzy Karczmarczuk wrote:

> Gurus, before I am tempted to signal this as a bug, perhaps
> you might convince me that it should be so.

it's not a bug, and nobody should have to convince you about any-
thing; despite what you may think from reading certain slicing threads,
this mailing list is not an argument clinic.

> If I type
>
> l=range(4)
> l.extend([1,2])
>
> l  gives [0,1,2,3,1,2], what else...
>
> On the other hand, try
>
> p=range(4).extend([1,2])
>
> Then, p HAS NO VALUE (NoneType).

(footnote: None is a value in Python.  it can be used to represent "no value",
but it can also be used for other things)

> With append the behaviour is similar. I didn't try other methods, but
> I suspect that it won't improve.
>
> WHY?

because you're saving the return value of "extend", not "range", and "extend"
returns None.

if you split it up like this

    l = range(4)
    p = l.extend([1, 2])

it should be obvious that "l" and "p" doesn't necessarily contain the same thing.

</F> 






More information about the Python-list mailing list