- Mastering Python
- Rick van Hattem
- 442字
- 2021-07-16 11:10:32
Chapter 2. Pythonic Syntax, Common Pitfalls, and Style Guide
The design and development of the Python programming language have always been in the hands of its original author, Guido van Rossum, in many cases lovingly referred to as the Benevolent Dictator For Life (BDFL). Even though van Rossum is thought to have a time machine (he has repeatedly answered feature requests with "I just implemented that last night": http://www.catb.org/jargon/html/G/Guido.html), he is still just a human and needs help with the maintenance and development of Python. To facilitate that, the Python Enhancement Proposal (PEP) process has been developed. This process allows anyone to submit a PEP with a technical specification of the feature and a rationale to defend its usefulness. After a discussion on the Python mailing lists and possibly some improvements, the BDFL will make a decision to accept or reject the proposal.
The Python style guide (PEP 8
: https://www.python.org/dev/peps/pep-0008/) was once submitted as one of those PEPs, and it is has been accepted and improved regularly since. It has a lot of great and widely accepted conventions as well as a few disputed ones. Especially, the maximum line length of 79 characters is a topic of many discussions. Limiting a line to 79 characters does have some merits, however. In addition to this, while just the style guide itself does not make code Pythonic, as "The Zen of Python" (PEP 20
: https://www.python.org/dev/peps/pep-0020/) elegantly says: "Beautiful is better than ugly." PEP 8
defines how code should be formatted in an exact way, and PEP 20
is more of a philosophy and mindset.
The common pitfalls are a list of common mistakes made, varying from beginner mistakes to advanced ones. They range from passing a list or dictionary (which are mutable) as arguments to late-binding problems in closures. An even more important issue is how to work around circular imports in a clean way.
Some of the techniques used in the examples in this chapter might be a bit too advanced for such an early chapter, but please don't worry. This chapter is about style and common pitfalls. The inner workings of the techniques used will be covered in later chapters.
We will cover 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)
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- OpenDaylight Cookbook
- Python自動(dòng)化運(yùn)維快速入門
- Servlet/JSP深入詳解
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- Instant 960 Grid System
- 利用Python進(jìn)行數(shù)據(jù)分析(原書第3版)
- HTML 5與CSS 3權(quán)威指南(第3版·上冊(cè))
- Android Wear Projects
- C專家編程
- 一步一步跟我學(xué)Scratch3.0案例
- Learning D
- Android熱門應(yīng)用開發(fā)詳解
- Building Microservices with Go
- C++ Data Structures and Algorithm Design Principles