[Tutor] (2.7.3) Inexplicable change of type
Steven D'Aprano
steve at pearwood.info
Fri Sep 14 11:07:18 CEST 2012
On 14/09/12 18:43, Ray Jones wrote:
> Between the two arrows, 'source' inexplicably switches from<type list>
> to<type NoneType>. Why?
source.remove('') does not do what you think it does. Checking the
Fine Manual is always a good idea, or experimentation at the interactive
interpreter:
py> source = list('abcd')
py> result = source.remove('a')
py> result is None
True
py> source
['b', 'c', 'd']
The list.remove method operates on the list in place, and like (nearly?)
all such in-place methods, it returns None.
So source = source.remove(' ') replaces source with None instead of the
list, which is then lost.
By the way, at the interactive interpreter you can also say:
help(list.remove)
to read some documentation on the method.
--
Steven
More information about the Tutor
mailing list