Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Zen - (only) one way to do it [duplicate]

This question might be sound subjective, but as "the Zen" says, there is (nearly always) one way to preferred, it shouldn't be subjective at the end.

What way is the better one?

[i.something() for i in l]
map(operator.methodcaller('something'), l)
map(lambda x: x.something(), l)

(1) is (IMO) very clear, but in many answers, map() is used. And if we do so, there is nearly equal readability between (2) and (3) (IMO, at least).

The same counts for many other tasks, but I have chosen this one, as it can stand for all of similiar ones.

like image 220
glglgl Avatar asked Jan 16 '14 07:01

glglgl


People also ask

What is the use of Zen of Python?

The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language. Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing list in 1999.

Who wrote the Zen of Python?

Came to be known as “The Zen of Python”, these aphorisms exploded amongst Python developers. Tim Peters wrote these BDFL's (Benevolent Dictator For Life, a nickname of Python creator Guido van Rossum) 20 guiding principles for Python's design but the last aphorism was left for van Rossum to fill in.

How do I show Zen in Python?

Thanks to his contribution, anyone can view the Zen straight from the Python interpreter by typing import this : >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit.

Which module prints the Zen of Python when imported?

As soon as we'd chosen "import this" I realized we just had to implement it. Python 2.2 was about to be released and I proposed that we turn off check-in notifications and sneak in a "this.py" module which when imported just printed the Zen of Python.


1 Answers

  • Simple is better than complex.
  • Readability counts.

Both are clear arguments for [i.something() for i in l].

This assumes that .something() doesn't mutate i, and that you're on Python 2.

like image 183
Tim Pietzcker Avatar answered Oct 11 '22 22:10

Tim Pietzcker