SlideShare a Scribd company logo
1 of 30
PRACTICAL ZEN
OF PYTHON
@FEMMERLING - ID-PYTHON JOGJA NOV
2016
PRACTICAL ZEN OF PYTHON
A BIT ABOUT ME
▸Fauzan Erich Emmerling
▸Married, 3 kids all of which will grow up learning python :)
▸Using python since 2010 and still learning more
▸Lives in Jogja after 14 years struggling with massive traffic jam in
Jakarta
▸VP of Engineering at Prism, yes we’re hiring!
▸@femmerling on twitter @fauzan on python-id slack
fauzan@python.or.id fauzan@prismapp.io
THE ZEN OF
PYTHON
IS ALL ABOUT
ENGINEERING
“IN REALITY, PROGRAMMING
LANGUAGES ARE HOW
PROGRAMMERS EXPRESS AND
COMMUNICATE IDEAS — AND THE
AUDIENCE FOR THOSE IDEAS IS
OTHER PROGRAMMERS, NOT
COMPUTERS.”
Guido van Rossum
PRACTICAL ZEN OF PYTHON
PRACTICAL ZEN OF PYTHON
THE ZEN OF PYTHON>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
PRACTICAL ZEN OF PYTHON
BEAUTIFUL IS BETTER THAN UGLY
▸Write codes that is nice to read and understand
▸Simple expression syntax
▸Be consistent
▸I know this is hard: pick meaningful names
PRACTICAL ZEN OF PYTHON
BEAUTIFUL IS BETTER THAN UGLY -
CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
EXPLICIT IS BETTER THAN IMPLICIT
▸State what you mean, explicitly
▸The for loop is very explicit
▸Remember, the goal is to exchange ideas
▸Leverage the Booleans
▸Be explicit and revealing when it comes to variable or
function naming
PRACTICAL ZEN OF PYTHON
EXPLICIT IS BETTER THAN IMPLICIT -
CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
SIMPLE IS BETTER THAN COMPLEX
▸Always aim for simplicity
▸Do not use OOP when it’s not needed
▸Aim for lesser lines of code
▸Less code == less confusion == more clarity
PRACTICAL ZEN OF PYTHON
SIMPLE IS BETTER THAN COMPLEX -
CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
COMPLEX IS BETTER THAN
COMPLICATED
▸When simple is not possible, be complex
▸Complicated can lead to misunderstanding
▸Complex code increases comprehension
PRACTICAL ZEN OF PYTHON
COMPLEX IS BETTER THAN
COMPLICATED - CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
FLAT IS BETTER THAN NESTED
‣ Use modules instead of nested if
‣ Use stdlib, it’s batteries included
PRACTICAL ZEN OF PYTHON
FLAT IS BETTER THAN NESTED - CODE
EXAMPLE
PRACTICAL ZEN OF PYTHON
SPARSE IS BETTER THAN DENSE
‣ Be loosely coupled
‣ Do one thing and do it well - - the UNIX philosophy
PRACTICAL ZEN OF PYTHON
SPARSE IS BETTER THAN DENSE -
CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
READABLITY COUNTS
‣ Use consistent whitespace (space VS tabs)
‣ Use inline documentation
‣ Code for humans
PRACTICAL ZEN OF PYTHON
READABILITY COUNTS - CODE EXAMPLE
PRACTICAL ZEN OF PYTHON
SPECIAL CASE ARE NOT SPECIAL
ENOUGH TO BREAK THE RULES
‣ Everything is an object
‣ The scope rule
PRACTICAL ZEN OF PYTHON
ALTHOUGH PRACTICALITY BEATS PURITY,
ERRORS SHOULD NEVER PASS SILENTLY,
UNLESS EXPLICITLY SILENCED
‣ Python has a multi-model approach. It is:
‣ Procedural
‣ OOP
‣ Functional
‣ Errors should be handled using exceptions
‣ Graceful handling should be done via try - except
PRACTICAL ZEN OF PYTHON
IN THE FACE OF AMBIGUITY, REFUSE
THE TEMPTATION TO GUESS
‣ Type handling is sometimes surprising
‣ You can’t add 2 different types
‣ You can multiply them
PRACTICAL ZEN OF PYTHON
THERE SHOULD BE ONE - - AND PREFERABLY ONLY
ONE - - OBVIOUS WAY TO DO IT.
ALTHOUGH THAT WAY MAY NOT BE OBVIOUS AT
FIRST UNLESS YOU’RE DUTCH
‣ Be DRY
‣ Don’t be redundant
‣ Better learning curve
‣ Easier for distributed teams
‣ When confused, remember that the BDFL is Dutch :)
PRACTICAL ZEN OF PYTHON
NOW IS BETTER THAN NEVER,
ALTHOUGH NEVER IS OFTEN BETTER THAN *RIGHT*
NOW‣ Ports to other language, i.e: Jython, IronPython
‣ The python 2 VS 3 battle
‣ stdlib has stricter requirements
PRACTICAL ZEN OF PYTHON
IF THE IMPLEMENTATION IS HARD TO EXPLAIN, IT’S
A BAD IDEA
IF THE IMPLEMENTATION IS EASY TO EXPLAIN, IT
MAY BE A GOOD IDEA
‣ Back to Guido’s quote on slide 3
‣ Explain ideas through your codes
‣ Simplicity is the ultimate sophistication
PRACTICAL ZEN OF PYTHON
NAMESPACES ARE ONE HONKING GREAT
IDEA - - LET’S DO MORE OF THOSE
‣ Closures for better module separation
‣ Fun to use
SO WHAT’S
NEXT?
PRACTICAL ZEN OF PYTHON
KEY TAKEAWAYS
▸Make your code understandable
▸Simplicity is the ultimate sophistication
▸When in doubt, “import this”
▸Do it today
QUESTION
S?
THANKS!!!

More Related Content

Viewers also liked

Rowing: London Tideway River Thames navigation & steering for coxswains
Rowing: London Tideway River Thames navigation & steering for coxswainsRowing: London Tideway River Thames navigation & steering for coxswains
Rowing: London Tideway River Thames navigation & steering for coxswainsrowperfect
 
Indian fashion from ancient to modern
Indian fashion from ancient to modernIndian fashion from ancient to modern
Indian fashion from ancient to modernraajhashree ilangovan
 
Fashion and clothing in ancient times
Fashion and clothing in ancient timesFashion and clothing in ancient times
Fashion and clothing in ancient timesmswilsonri
 
Fashion in ancient civilizations
Fashion in ancient civilizationsFashion in ancient civilizations
Fashion in ancient civilizationsmswilsonri
 
Fashion and clothing in ancient civilisations
Fashion and clothing in ancient civilisationsFashion and clothing in ancient civilisations
Fashion and clothing in ancient civilisationsmswilsonri
 
Building Automated REST APIs with Python
Building Automated REST APIs with PythonBuilding Automated REST APIs with Python
Building Automated REST APIs with PythonJeff Knupp
 
Fashion in Ancient Greece and Rome
Fashion in Ancient Greece and RomeFashion in Ancient Greece and Rome
Fashion in Ancient Greece and RomeTina Savchyshyn
 
Pass That Exam! Test Prep Strategies & Resources
Pass That Exam! Test Prep Strategies & ResourcesPass That Exam! Test Prep Strategies & Resources
Pass That Exam! Test Prep Strategies & ResourcesShelly Sanchez Terrell
 
Groovy Grammar! Interesting ways to learn grammar!
Groovy Grammar! Interesting ways to learn grammar!Groovy Grammar! Interesting ways to learn grammar!
Groovy Grammar! Interesting ways to learn grammar!Shelly Sanchez Terrell
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBNicola Iarocci
 
Work Hacks : Body Language
Work Hacks : Body LanguageWork Hacks : Body Language
Work Hacks : Body LanguageInterQuest Group
 
Top Office Etiquette Mistakes
Top Office Etiquette MistakesTop Office Etiquette Mistakes
Top Office Etiquette Mistakesej4video
 
You Are Not As Rational As You Think
You Are Not As Rational As You ThinkYou Are Not As Rational As You Think
You Are Not As Rational As You ThinkYang Ao Wei 楊翱維
 
Digitized Student Development, Social Media, and Identity
Digitized Student Development, Social Media, and IdentityDigitized Student Development, Social Media, and Identity
Digitized Student Development, Social Media, and IdentityPaul Brown
 
GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom Brian Housand
 

Viewers also liked (17)

Rowing: London Tideway River Thames navigation & steering for coxswains
Rowing: London Tideway River Thames navigation & steering for coxswainsRowing: London Tideway River Thames navigation & steering for coxswains
Rowing: London Tideway River Thames navigation & steering for coxswains
 
Indian fashion from ancient to modern
Indian fashion from ancient to modernIndian fashion from ancient to modern
Indian fashion from ancient to modern
 
Fashion and clothing in ancient times
Fashion and clothing in ancient timesFashion and clothing in ancient times
Fashion and clothing in ancient times
 
Fashion in ancient civilizations
Fashion in ancient civilizationsFashion in ancient civilizations
Fashion in ancient civilizations
 
Fashion and clothing in ancient civilisations
Fashion and clothing in ancient civilisationsFashion and clothing in ancient civilisations
Fashion and clothing in ancient civilisations
 
Building Automated REST APIs with Python
Building Automated REST APIs with PythonBuilding Automated REST APIs with Python
Building Automated REST APIs with Python
 
Greek Fashion
Greek FashionGreek Fashion
Greek Fashion
 
Fashion in Ancient Greece and Rome
Fashion in Ancient Greece and RomeFashion in Ancient Greece and Rome
Fashion in Ancient Greece and Rome
 
Pass That Exam! Test Prep Strategies & Resources
Pass That Exam! Test Prep Strategies & ResourcesPass That Exam! Test Prep Strategies & Resources
Pass That Exam! Test Prep Strategies & Resources
 
Groovy Grammar! Interesting ways to learn grammar!
Groovy Grammar! Interesting ways to learn grammar!Groovy Grammar! Interesting ways to learn grammar!
Groovy Grammar! Interesting ways to learn grammar!
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDB
 
Micro Expressions
Micro ExpressionsMicro Expressions
Micro Expressions
 
Work Hacks : Body Language
Work Hacks : Body LanguageWork Hacks : Body Language
Work Hacks : Body Language
 
Top Office Etiquette Mistakes
Top Office Etiquette MistakesTop Office Etiquette Mistakes
Top Office Etiquette Mistakes
 
You Are Not As Rational As You Think
You Are Not As Rational As You ThinkYou Are Not As Rational As You Think
You Are Not As Rational As You Think
 
Digitized Student Development, Social Media, and Identity
Digitized Student Development, Social Media, and IdentityDigitized Student Development, Social Media, and Identity
Digitized Student Development, Social Media, and Identity
 
GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Practical Zen of Python

  • 1. PRACTICAL ZEN OF PYTHON @FEMMERLING - ID-PYTHON JOGJA NOV 2016
  • 2. PRACTICAL ZEN OF PYTHON A BIT ABOUT ME ▸Fauzan Erich Emmerling ▸Married, 3 kids all of which will grow up learning python :) ▸Using python since 2010 and still learning more ▸Lives in Jogja after 14 years struggling with massive traffic jam in Jakarta ▸VP of Engineering at Prism, yes we’re hiring! ▸@femmerling on twitter @fauzan on python-id slack fauzan@python.or.id fauzan@prismapp.io
  • 3. THE ZEN OF PYTHON IS ALL ABOUT ENGINEERING
  • 4. “IN REALITY, PROGRAMMING LANGUAGES ARE HOW PROGRAMMERS EXPRESS AND COMMUNICATE IDEAS — AND THE AUDIENCE FOR THOSE IDEAS IS OTHER PROGRAMMERS, NOT COMPUTERS.” Guido van Rossum PRACTICAL ZEN OF PYTHON
  • 5. PRACTICAL ZEN OF PYTHON THE ZEN OF PYTHON>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
  • 6. PRACTICAL ZEN OF PYTHON BEAUTIFUL IS BETTER THAN UGLY ▸Write codes that is nice to read and understand ▸Simple expression syntax ▸Be consistent ▸I know this is hard: pick meaningful names
  • 7. PRACTICAL ZEN OF PYTHON BEAUTIFUL IS BETTER THAN UGLY - CODE EXAMPLE
  • 8. PRACTICAL ZEN OF PYTHON EXPLICIT IS BETTER THAN IMPLICIT ▸State what you mean, explicitly ▸The for loop is very explicit ▸Remember, the goal is to exchange ideas ▸Leverage the Booleans ▸Be explicit and revealing when it comes to variable or function naming
  • 9. PRACTICAL ZEN OF PYTHON EXPLICIT IS BETTER THAN IMPLICIT - CODE EXAMPLE
  • 10. PRACTICAL ZEN OF PYTHON SIMPLE IS BETTER THAN COMPLEX ▸Always aim for simplicity ▸Do not use OOP when it’s not needed ▸Aim for lesser lines of code ▸Less code == less confusion == more clarity
  • 11. PRACTICAL ZEN OF PYTHON SIMPLE IS BETTER THAN COMPLEX - CODE EXAMPLE
  • 12. PRACTICAL ZEN OF PYTHON COMPLEX IS BETTER THAN COMPLICATED ▸When simple is not possible, be complex ▸Complicated can lead to misunderstanding ▸Complex code increases comprehension
  • 13. PRACTICAL ZEN OF PYTHON COMPLEX IS BETTER THAN COMPLICATED - CODE EXAMPLE
  • 14. PRACTICAL ZEN OF PYTHON FLAT IS BETTER THAN NESTED ‣ Use modules instead of nested if ‣ Use stdlib, it’s batteries included
  • 15. PRACTICAL ZEN OF PYTHON FLAT IS BETTER THAN NESTED - CODE EXAMPLE
  • 16. PRACTICAL ZEN OF PYTHON SPARSE IS BETTER THAN DENSE ‣ Be loosely coupled ‣ Do one thing and do it well - - the UNIX philosophy
  • 17. PRACTICAL ZEN OF PYTHON SPARSE IS BETTER THAN DENSE - CODE EXAMPLE
  • 18. PRACTICAL ZEN OF PYTHON READABLITY COUNTS ‣ Use consistent whitespace (space VS tabs) ‣ Use inline documentation ‣ Code for humans
  • 19. PRACTICAL ZEN OF PYTHON READABILITY COUNTS - CODE EXAMPLE
  • 20. PRACTICAL ZEN OF PYTHON SPECIAL CASE ARE NOT SPECIAL ENOUGH TO BREAK THE RULES ‣ Everything is an object ‣ The scope rule
  • 21. PRACTICAL ZEN OF PYTHON ALTHOUGH PRACTICALITY BEATS PURITY, ERRORS SHOULD NEVER PASS SILENTLY, UNLESS EXPLICITLY SILENCED ‣ Python has a multi-model approach. It is: ‣ Procedural ‣ OOP ‣ Functional ‣ Errors should be handled using exceptions ‣ Graceful handling should be done via try - except
  • 22. PRACTICAL ZEN OF PYTHON IN THE FACE OF AMBIGUITY, REFUSE THE TEMPTATION TO GUESS ‣ Type handling is sometimes surprising ‣ You can’t add 2 different types ‣ You can multiply them
  • 23. PRACTICAL ZEN OF PYTHON THERE SHOULD BE ONE - - AND PREFERABLY ONLY ONE - - OBVIOUS WAY TO DO IT. ALTHOUGH THAT WAY MAY NOT BE OBVIOUS AT FIRST UNLESS YOU’RE DUTCH ‣ Be DRY ‣ Don’t be redundant ‣ Better learning curve ‣ Easier for distributed teams ‣ When confused, remember that the BDFL is Dutch :)
  • 24. PRACTICAL ZEN OF PYTHON NOW IS BETTER THAN NEVER, ALTHOUGH NEVER IS OFTEN BETTER THAN *RIGHT* NOW‣ Ports to other language, i.e: Jython, IronPython ‣ The python 2 VS 3 battle ‣ stdlib has stricter requirements
  • 25. PRACTICAL ZEN OF PYTHON IF THE IMPLEMENTATION IS HARD TO EXPLAIN, IT’S A BAD IDEA IF THE IMPLEMENTATION IS EASY TO EXPLAIN, IT MAY BE A GOOD IDEA ‣ Back to Guido’s quote on slide 3 ‣ Explain ideas through your codes ‣ Simplicity is the ultimate sophistication
  • 26. PRACTICAL ZEN OF PYTHON NAMESPACES ARE ONE HONKING GREAT IDEA - - LET’S DO MORE OF THOSE ‣ Closures for better module separation ‣ Fun to use
  • 28. PRACTICAL ZEN OF PYTHON KEY TAKEAWAYS ▸Make your code understandable ▸Simplicity is the ultimate sophistication ▸When in doubt, “import this” ▸Do it today