Python-idioms-2014-01-16.pdf

(132 KB) Pobierz
Python Idioms
Safe Hammad
Python Northwest
16 th January 2014
1287187626.002.png
What is an idiom?
“The speciic grammatical, syntactic, and
structural character of a given language.”
“A commonly used and understood way of
expressing an fact, idea or intention.”
1287187626.003.png
Why care about Python idioms?
"Programs must be written for people to read, and
only incidentally for machines to execute."
- Abelson & Sussman, SICP
“There should be one - and preferably only one -
obvious way to do it.”
- Tim Peters, The Zen of Python (PEP 20)
The use of commonly understood syntax or coding
constructs can aid readability and clarity.
Some idioms can be faster or use less memory than
their “non-idiomatic” counterparts.
Python's idioms can make your code Pythonic!
1287187626.004.png
Ten idioms
(In no particular order)
1287187626.005.png
1. Make a script both
importable and executable
if __name__ == '__main__':
1287187626.001.png
Zgłoś jeśli naruszono regulamin