Composition of functions

Mladen Gogala gogala.mladen at gmail.com
Wed Jun 30 23:50:43 EDT 2010


If I write things with the intermediate variables like below, everything 
works:

>>> x="quick brown fox jumps over a lazy dog"
>>> y=list(x)
>>> y
['q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 
'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 'a', ' 
', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
>>> y.reverse()
>>> y
['g', 'o', 'd', ' ', 'y', 'z', 'a', 'l', ' ', 'a', ' ', 'r', 'e', 'v', 
'o', ' ', 's', 'p', 'm', 'u', 'j', ' ', 'x', 'o', 'f', ' ', 'n', 'w', 
'o', 'r', 'b', ' ', 'k', 'c', 'i', 'u', 'q']
>>> z=''.join(y)
>>> z
'god yzal a revo spmuj xof nworb kciuq'

That is all well and kosher. Now, if I try to shorten things up, I will 
get a type error:

>>> 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:

>>> y=list(x).reverse()
>>> print y
None

And that is strange. From the example above, I saw that if I assigned the 
intermediate array to hold list(x), did the reverse on that variable and 
then did "join", everything works as advertised. Version of Python is 
2.6.5 on Ubuntu 10. Why is the intermediate variable necessary?
I am a complete newbie and am trying the usual stuff, reversing strings, 
displaying them in hex, writing things to file "test1.txt" and alike.
-- 

http://mgogala.byethost5.com



More information about the Python-list mailing list