Composition of functions

Mladen Gogala gogala.mladen at gmail.com
Thu Jul 1 00:32:57 EDT 2010


On Wed, 30 Jun 2010 21:04:28 -0700, Stephen Hansen wrote:

> On 6/30/10 8:50 PM, Mladen Gogala wrote:
>>>>> x="quick brown fox jumps over a lazy dog"
>>>>> y=''.join(list(x).reverse())
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in<module>
>> TypeError
>>>>>
>>>>>
>>
>> Why is TypeError being thrown? The reason for throwing the type error
>> is the fact that the internal expression evaluates to None and cannot,
>> therefore, be joined:
> 
> The "reverse" method, like "sort" and a couple others, are in-place
> operations. Meaning, they do not return a new list but modify the
> existing list. All methods that are "in-place" modifications return None
> to indicate this. This way you can't make a mistake and think its
> returning a sorted / reversed copy but it isn't.

Thanks.

> 
> However, you can easily get what you want by using the 'reversed'
> function (and similarly, the 'sorted' function), a la:
> 
>  >>> y = ''.join(reversed(list(x)))
> 
> The 'reversed' and 'sorted' functions are generators that lazilly
> convert an iterable as needed.

Ah, that is even better. Thanks.



-- 
http://mgogala.byethost5.com



More information about the Python-list mailing list