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

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.

主站蜘蛛池模板: 当阳市| 高阳县| 大冶市| 富川| 鲜城| 龙里县| 沙坪坝区| 光山县| 嘉定区| 西青区| 当雄县| 青州市| 泽州县| 湟中县| 田阳县| 方正县| 镇康县| 繁昌县| 贞丰县| 望江县| 河源市| 额济纳旗| 宁安市| 隆安县| 深泽县| 平和县| 翁源县| 大埔区| 定兴县| 青河县| 多伦县| 蛟河市| 临朐县| 江陵县| 安远县| 湖州市| 扬州市| 大化| 沙湾县| 汕头市| 宝应县|