One of the best ways to become efficient with Python is to really learn the standard libraries. However, do this with understanding that some are included for base functionality or compatibility, but have been superseded by better packages in PyPi. Requests over urllib2 is a great example of this. By being outside of Python, these packages can iterate faster and not be dependent on Python releases. Collections is not one that needed replacement.
You may have seen if __name__ == '__main__': at the bottom of a script. Why is this there and what does it mean? First, a python file is a module. They can be combined into a directory module and this is what is being done when you see an __init__.py file in the folder. We will get there in a little while, but first we will cover a little more than just __name__.
Iterators and Generators are two mechanisms for providing data to cycle through in Python. The difference is if the data is already in memory and we iterate along it, or if we are generating it as we go. The names actually make sense. These turn out to be very similar and just differ in execution. One is better if you already have a class. The other is a little simpler.
Context managers were added to Python with PEP 343. This allows proper handling of resources without worrying about missing something in your try/finally code. If you are not opening files using a context manager, you are most likely doing it wrong. Looking at opening a file and what could go wrong, will show how context managers make programming both more robust and simpler. The operation can be broken into 3 sections of code.
There are a whole bunch of underscores in Python. If you are new to Python, this may seem weird. There is a reason behind the flatness. There are even special names for them, under when you have a single underscore and dunder when two underscores are used (for double underscore). Unders There are many uses of single underscores in Python. We will hit a few common and easy to explain right away and get them over with.
If you came directly to this article, you might want to read my previous on in this series with the link at the bottom. We discussed the dangers of using mutable values for defaults in a function parameter. In this article I’ll discuss two things: using mutable defaults for good and decorators. In the end, combining these together. Using the default mutable for good In developing a Python workflow for a custom system, I’m interfacing with custom hardware to measure current and temperature.
Default arguments in Python allow you to make complex functions very easy to use. They can be called with mostly defaults or called with as much configuration as needed. This comes with a gotcha for those getting started in this. If you use mutable data structures as defaults, things don’t behave exactly as you would expect. However, before we start, lets cover some things you may not know that I will use in the code.
Python 3 Strings The handling of strings was one of the major breaking change between Python 2 and 3. Python 2 was easy for English speaking folks that could live in 8-bit ASCII land. It was easy to use a string as a sequence of bytes when doing binary transfer. However, turning 8-bit ASCII into Unicode was a little daunting. With Python 3, all strings are Unicode. To get a bytes object as a string would be in Python 2, you prefix a string with a b as in b'bytes object with \xf4 <- high 8-bit character'.
In my current role, I don’t do much mentoring to other programmers at a language specific level. Most of my job is isolated somewhat and I’m the only serious Python programmer at the company. I can offer insight in architecture, DB design and other pieces relative to our Android, iOS, and Web applications, but not Python. So I decided to capture some of my thoughts about what I would tell a new Python programmer, to help them progress into a successful Pythonista faster.