Python newbie

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Sep 19 15:51:24 EDT 2008


Mladen Gogala:

Welcome to Python, you will need only one or few weeks to be able to
write useful Python programs. But even with the help of a good book
(that I suggest you to read) you may need several months or one year
to master it :-)

> 2) Why is it illegal to pass a built-in function "print" to map?

Maybe because one of the few original design mistakes of Python. The
new Python 3.0 "fixes" that problem, in Python 3 print is now a
function, so you can use it where you can use functions.
But in general I think it's better to not give map() a function that
has side effects like print().
So you want to use a list comp there instead, and create a new list:
[2 * el for el in a]

In Python (and several other modern languages, like Scala, plus some
more functional languages) you will find that people often prefer to
create new data structures instead of modifying old ones. This may
sound like a waste of memory and time, but using a more immutable-data
style has several advantages (often in code clarity) that are a bit
complex to explain here.

As you probably know, the good thing of learning new languages is that
they carry some principles, and they are the expression of some ideas
of the computer science. So learning Python may show you some new
ideas, or new ways to look at old computer science ideas you already
know. I think Alan J. Perlis said "A language that doesn't affect the
way you think about programming, is not worth knowing."

Bye,
bearophile



More information about the Python-list mailing list