Appending to []

Kiuhnm kiuhnm03.4t.yahoo.it
Sat Apr 21 12:14:15 EDT 2012


On 4/21/2012 17:41, Bernd Nawothnig wrote:
> On 2012-04-21, Kiuhnm wrote:
>>> Returning a None-value is pretty useless. Why not returning self, which would be
>>> the resulting list in this case? Returning self would make the
>>> language a little bit more functional, without any drawback.
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>
>>> Then nested calls like
>>>
>>> a = [].append('x').append('y').append('z')
>>
>> You just answered to your own question: append returns None so that
>> people can't use it the way you did.
>
> That is one possible way to design the method, but not the only
> possible way.
>
>> You make the reader believe that you're adhering to the functional
>> paradigm whereas 'append' has actually side effects!
>> Moreover, you use an assignment just to reinforce this wrong belief.
>
> I know about side effects and I know that letting append return self
> would not make Python a purely functional language with only immutable
> data.
>
> I just asked a simple question about a detail I personally would
> consider it to be useful.
>
> Please no further religious war about that ;-)

Sorry if I wasn't clear. I meant that one should either relies on 
side-effects and write something like
   a.append('x').append('t').append('z')
or use a more functional style and write
   a = a + [x] + [z]
Mixing the two doesn't seem very elegant to me.
I also think that if you need to repeatedly modify an object in the same 
expression, then you should prefer the more functional approach.
I wasn't suggesting that you didn't know what functional programming and 
side effects are.
Mine was a justification (maybe opinable) of why append was designed 
that way.

Kiuhnm



More information about the Python-list mailing list