[Tutor] (2.7.3) Inexplicable change of type

eryksun eryksun at gmail.com
Fri Sep 14 15:28:26 CEST 2012


On Fri, Sep 14, 2012 at 8:37 AM, Emile van Sebille <emile at fenx.com> wrote:
>
>>          source = source.remove('')
>
> To round things out, here's one way to do what I expect you're expecting:
>
>    >>> r=range(10)
>    >>> a = r.pop(r.index(4))
>    >>> a
>    4
>    >>> r
>    [0, 1, 2, 3, 5, 6, 7, 8, 9]

Ray was probably thinking in terms of immutable objects such as strings:

    >>> "1".replace('1', '2').replace('2', '3')
    '3'

Since Python doesn't do in-place operations on strings, it has to
return a new object for each call to replace().

list.pop() returns a value, but Ray doesn't want the value. Splitting
on a single character leaves empty strings between consecutive runs of
the character (or at the edges). For example, splitting 'babbbab' on
'b' returns ['', 'a', '', '', 'a', '']. Ray is looping until all of
the empty strings have been removed from the list. It's a completely
in-place modification.


More information about the Tutor mailing list