Appending to []

Ian Kelly ian.g.kelly at gmail.com
Sun Apr 22 05:35:27 EDT 2012


On Sun, Apr 22, 2012 at 2:29 AM, Bernd Nawothnig
<Bernd.Nawothnig at t-online.de> wrote:
>> But what about 2), the mixed (impure) functional design? Unfortunately,
>> it too has a failure mode: by returning a list, it encourages the error
>> of assuming the list is a copy rather than the original:
>>
>> mylist = [1, 2, 3, 4]
>> another_list = mylist.append(5)
>> # many pages of code later...
>> do_something_with(mylist)
>
> Yes, but mutable data is in general a candidate for unexpected
> behaviour, regardless wether you use an impure functional notation or
> not:
>
> mylist = [1, 2, 3, 4]
> mylist.append(5)
> another_list = mylist
> # many pages of code later...
> do_something_with(mylist)
>
> avoids that impure function call but can perfectly lead to the same
> unexpected behaviour. Your "many pages of code later" and that it is
> simply difficult or impossible to keep in mind all these possible
> state changes of variables is the real problem here.

Except that in this case the behaviour is less likely to be
unexpected.  Who would read that second code listing and come to the
conclusion that mylist is not being modified?



More information about the Python-list mailing list