Pythonic Syntax and Common Pitfalls
In this chapter, you will learn how to write Pythonic code, along with finding out about some of the common pitfalls of Python and how to work around them. The pitfalls range from passing a list or dictionary (which are mutable) as an argument to more advanced pitfalls, such as late-binding in closures. You will also see how to fix or work around circular imports in a clean way. Some of the techniques used in the examples in this chapter might seem a bit too advanced for such an early chapter. Do not worry, though, as the inner workings will be covered later on.
We will explore the following topics in this chapter:
- Code style (PEP 8,
pyflakes
,flake8
, and more) - Common pitfalls (lists as function arguments, pass by value versus pass by reference, and inheritance behavior)
The definition of Pythonic code used in this chapter is based on commonly accepted coding guidelines and my subjective opinions. When working...