We’ve been sharing with you the good practices we implement when programming in Python. If you
haven’t read it yet, the first part of the article you will find
5) The world of Python is now astride. Two branches
of Python, namely 2.x and 3.x, are developing simultaneously. Some but not all libraries have
already been ported to Python 3.x. Remember not to use features that are deprecated, because in
the future it will complicate running code on Python 3.x.
creators of Python have placed an "easter egg" in the Python interpreter, which contains a
collection of good patterns. We advise you to try to implement them in your projects. You don’t
have to apply all right away, but remember "now is better than never".
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!
The creators of Python implemented also anti-patterns explaining what the code shouldn’t look
like. Pay particular attention to the point: "Write optimal code before releasing." This is a
trap developers often fall into.
The function should write 'zuch' and then change the word 'zuch' to 'puch'. But it doesn’t happen,
because most variables in Python are local, and Python is not an interpreted language which executes
the code line by line. Therefore, when Python analyzed the entire declaration of foo function, it
found that the bar variable is a local variable, and when you wanted it to unsubscribe:
because it has not been defined yet. There are two solutions to this problem:
A) Don’t use global
variables unless you really need it. Remember about this, not only when programming in Python.
Nobody likes global variables, so think about the people that will inherit the code after you.
When the situation will force you to use it (e.g. you’re maintaining the old code) use the keyword
global. The above example can be modified as follows: