During the last weeks of my entry into the Python world I have very quickly realized that I have to change my way of thinking and programming. A very good example is this thread where I tried to act smart and learned something!
Just now I read on the python-list an ancient mail about why print is a statement and not a function, there I also found out how to avoid the new line at the end of a print which never really bothered me, but I always asked myself how to avoid it, just in case I would ever need it.
print "no new line after me",
See the comma at the end! Huhu, cool thing. Some things are just easier than I thought. And it makes perfectly sense. I am using the print statement (without parentheses) quite a lot also with comma seperated arguments, instead of concatenated strings, so that should have been something, not obvious, but one of the first options to try.
PHP bashing
Another example of why I like Python better (over PHP). In PHP I would do the following:
if (in_array("x",$array) && $value)
In Python you do the same like this
if "x" in array and value
Which one is better to read?
Another interesting Python thing was finding the common words in two strings. I would not have come up with something that simple:
>>> import sets
>>> set1 = sets.Set("Bernd das Brot".split())
>>> set2 = sets.Set("Bäcker Bernd backt Brot".split())
>>> set1 & set2
set(['Brot', 'Bernd'])
Isn't that awesome? For those new to Python the ">>>" at the beginning of the line come from the Python shell, which btw is really great, I think I mentioned that. Sets seem to be in Python since 2.3.3.
OK, in PHP you would do it using the array_intersect function. But then you go into the hell of PHP's array-functions you first need to get the order of the parameters right :-). I think PHP's array functions are the best chaos you can find, especially the different orders of parameters for functions that do alike things. I don't even want to start about the naming. Many others have done that before.