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

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.

主站蜘蛛池模板: 南通市| 武穴市| 上饶市| 武冈市| 荥经县| 凌源市| 阳新县| 米泉市| 右玉县| 闻喜县| 汝阳县| 始兴县| 金沙县| 伊金霍洛旗| 左权县| 德格县| 繁峙县| 泰来县| 清原| 洞口县| 出国| 尚义县| 靖远县| 托克托县| 洞口县| 遂溪县| 临城县| 德兴市| 铅山县| 红原县| 如东县| 桑日县| 鲁山县| 唐海县| 米脂县| 屏边| 长丰县| 吉林省| 济阳县| 浮山县| 沙雅县|