Python

What is Pythonic Code? Best pratices in Python

December 15, 2021

What is Pythonic Code? Best pratices in Python

In this article we’ll learn what pythonic code is, originated from English, the expression pythonic always refers to a well-done code in Python, but is it just that?

Pythonic Code cover

Hey you programmer, how are you? Let’s learn a little more about Python!

The expression originated in English as pythonic, due to the simplicity of solving some problems with Python

Other languages would need more lines of code or use more methods/functions, just to name a few.

How is a code considered pythonic?

It must follow a set of 20 aphorisms, these ‘norms’ are known as PEP 20 or The Zen of Python, you can find it here

These points are described as the ‘perfect description of Python’s philosophy’, by the language’s creator, Guido van Rossum

The points are:

  • 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 rules;
  • Though practicality trumps purity;
  • Errors should never pass silently;
  • Unless they are explicitly silenced;
  • Faced with ambiguity, refuse the temptation to guess;
  • There should be one – and preferably only one – obvious way to do something;
  • Although this mode may not be obvious at first, unless you are Dutch;
  • Now it’s better than ever;
  • Though it’s never often better than now;
  • If the implementation is hard to explain, it’s a bad idea;
  • If the implementation is easy to explain, it might be a good idea;
  • Namespaces are a great idea;

Note: as you may have noticed only 19 were written, and this is mentioned in the link I gave you earlier

Realize that nothing talks about code explicitly, but about a way to make your code

These are not software design rules but a philosophy that will make your code pythonic

Another material that can help you is the Python code style guide, see here:

Conclusion

In this article we learned  what pythonic code is.

Which is nothing more than an expression for a well done code in Python, following some principles that the set of them is called PEP 20

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x