Practical Zen of Python

30
PRACTICAL ZEN OF PYTHON @FEMMERLING - ID-PYTHON JOGJA NOV 2016

Transcript of Practical Zen of Python

Page 1: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

@FEMMERLING - ID-PYTHON JOGJA NOV 2016

Page 2: Practical Zen of Python

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

[email protected] [email protected]

Page 3: Practical Zen of Python

THE ZEN OF PYTHON IS ALL ABOUT ENGINEERING WISDOM

Page 4: Practical Zen of Python

“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

Page 5: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

THE ZEN OF PYTHON>>> import thisThe 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!

Page 6: Practical Zen of Python

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

Page 7: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

BEAUTIFUL IS BETTER THAN UGLY - CODE EXAMPLE

Page 8: Practical Zen of Python

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

Page 9: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

EXPLICIT IS BETTER THAN IMPLICIT - CODE EXAMPLE

Page 10: Practical Zen of Python

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

Page 11: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

SIMPLE IS BETTER THAN COMPLEX - CODE EXAMPLE

Page 12: Practical Zen of Python

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

Page 13: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

COMPLEX IS BETTER THAN COMPLICATED - CODE EXAMPLE

Page 14: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

FLAT IS BETTER THAN NESTED‣ Use modules instead of nested if‣ Use stdlib, it’s batteries included

Page 15: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

FLAT IS BETTER THAN NESTED - CODE EXAMPLE

Page 16: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

SPARSE IS BETTER THAN DENSE‣ Be loosely coupled‣ Do one thing and do it well - - the UNIX philosophy

Page 17: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

SPARSE IS BETTER THAN DENSE - CODE EXAMPLE

Page 18: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

READABLITY COUNTS‣ Use consistent whitespace (space VS tabs)‣ Use inline documentation‣ Code for humans

Page 19: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

READABILITY COUNTS - CODE EXAMPLE

Page 20: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

SPECIAL CASE ARE NOT SPECIAL ENOUGH TO BREAK THE RULES‣ Everything is an object‣ The scope rule

Page 21: Practical Zen of Python

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

Page 22: Practical Zen of Python

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

Page 23: Practical Zen of Python

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 :)

Page 24: Practical Zen of Python

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

Page 25: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

IF THE IMPLEMENTATION IS HARD TO EXPLAIN, IT’S A BAD IDEAIF 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

Page 26: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

NAMESPACES ARE ONE HONKING GREAT IDEA - - LET’S DO MORE OF THOSE‣ Closures for better module separation‣ Fun to use

Page 27: Practical Zen of Python

SO WHAT’S NEXT?

Page 28: Practical Zen of Python

PRACTICAL ZEN OF PYTHON

KEY TAKEAWAYS▸ Make your code understandable▸ Simplicity is the ultimate sophistication▸ When in doubt, “import this”▸ Do it today

Page 29: Practical Zen of Python

QUESTIONS?

Page 30: Practical Zen of Python

THANKS!!!