i have just confronted this situation which i am not able to understand:
In [3]: nk1=range(10)
In [4]: nk2=range(11,15)
In [5]: nk1.extend(nk2)
In [6]: nk1
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14]
In [7]: dir(range(10))==dir(list)==dir(range(11,15))==dir(nk1)==dir(nk2)
Out[7]: True
In [8]: print range(10).extend(range(11,15))
None
as you can see above that i can easily extend nk1, but why not the last statement which is returning None??
why it is returning None in In[8] input (while from In[7] we can see that all are same)???
so do i always have to make instance of range to extend it???
from python docs; i found this; but i am not getting how does above case happened.
When you extend a list the list is modified in-place. A new list is not returned.
In-place modifications indicate that the object whose method you’re calling is going to make changes to itself during that method execution. In most cases (to avoid the confusion you’re currently encountering) these types of operations do not return the object that performed the operation (though there are exceptions). Consider the following:
In the final example, the
rangefunction returns a list which is never assigned to anything. That list is then extended, the result of which is a return value ofNone. This is printed.