官术网_书友最值得收藏!

  • 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)

Note

The definition of Pythonic code is highly subjective and mainly reflects the opinion of this author. When working on a project, it is more important to stay consistent with the coding styles of that project than with the coding guidelines given by Python or this book.

主站蜘蛛池模板: 西林县| 天祝| 尼勒克县| 金门县| 吴川市| 融水| 绥化市| 怀安县| 祥云县| 凭祥市| 澄江县| 汕尾市| 绥江县| 扬中市| 德惠市| 金堂县| 邵阳市| 涿州市| 错那县| 平陆县| 绵阳市| 三亚市| 和田市| 大足县| 子长县| 泾源县| 纳雍县| 大港区| 株洲县| 乌审旗| 庆阳市| 内丘县| 车险| 延吉市| 桃源县| 重庆市| 疏勒县| 绥化市| 保康县| 昭通市| 准格尔旗|