Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
print " ".join(a) That's ugly :/ . This makes sense implementation-wise1, but I still2 hate the fact that join is method of the separator rather than the group of strings you join. [1] This let's you join work on any iterable (a string, a generator, a set...), not just a list. And the only condition for being an iterable in python is to define __iter__. Because of the duck typing3 philosophy of python, iterables are only defined by what they do (can be itered over), not what they are (member of a class, or doing a role), so there is nowhere to write code that would apply to all iterables. This means writing join somewhere else than in the object whose elements you join. Since you get a string, putting that in the string module is the obvious place, but a class method like str.join(*iterables, sep=" ") would have made more sense to me (plus you get a default separator to mirror the default of split). [2] I used to hate the fact that you end an iteration with an exception. Because I used to see them as meaning that something bad or invalid as happened. But if you see them as meaning "not the nominal case", which is what exception means in English, it's actually an interesting way to solve the Semipredicate problem (maybe a little on the heavy side, since you have to unroll the whole try catch mecanism...). [3] Iterables are actually one of the cases where I like duck programming. It's an iterable if you can iterate over it, period. But otherwise I prefer perl's "it's a string if I'm doing something stringy, it's a number if I'm counting on it". I got quite philosophical on that post xD In reply to Re: Python tricks
by Eily
|
|