Spaces are used sparingly in Python. It is common to put spaces aroundthe assignment operator. The recommended style is

c = (f-32)*5/9
Do not take great pains to line up assignment operators vertically.

Variable names are often given as mixedCase; variable namestypically begin with lower-case letters. Thelower_case_with_underscoresstyle is also used, but is less popular.

Variable names are often given asmixedCase; variable namestypically begin with lower-case letters. Thelower_case_with_underscoresstyle is also used, but is less popular.

In addition, the following special forms using leading or trailingunderscores are recognized:

  • single_trailing_underscore_: used to avoid conflictswith Python keywords. For example:print_=42
  • __double_leading_and_trailing_underscore__: used forspecial objects or attributes, e.g.__init__,__dict__or __file__. These names are reserved; do not use nameslike these in your programs unless you specifically mean aparticular built-in feature of Python.
  • _single_underscore: means that the variable is “private”.


A number with a leading 0 (zero) is octal, base 8, and uses the digits 0to 7.0123 is octal and equal to 83 decimal.

A number with a leading 0x or0X is hexadecimal,base 16, and uses the digits 0 through 9, plusa,A, b,B,c, C,d,D,e,E,f, and F.0x2BC8 is hexadecimal and equal to 11208.

A number with a leading 0b or0B is binary, base 2, and uses digits 0 and 1.

In Python 3, the octal syntax will change. Octal constants willbegin with 0o to match hexadecimal constants which beginwith 0x.

0o123 will be octal and equal to 83 decimal.


Python represents truth and falsity in a variety of ways.

  • False. Also 0, the special valueNone, zero-length strings"", zero-length lists[], zero-length tuples(),empty mappings{} are all treated as False.
  • True. Anything else that is not equivalent to False.
keyword parameters

Positional parameters work well when there are few parameters and theirmeaning is obvious. Keyword parameters work best when there are a lot ofparameters, especially when there are optional parameters.

Good use of keyword parameters mandates good selection of keywords.Single-letter parameter names or obscure abbreviations do not makekeyword parameters helpfully informative.

Here are the rules we’ve seen so far:

  1. Supply values for all parameters given by name, irrespective of position.
  2. Supply values for all remaining parameters by position; in the eventof duplicates, raise aTypeError.
  3. Supply defaults for any parameters that have defaults defined; ifany parameters still lack values, raise aTypeError.

There are still more options available for handling variable numbers ofparameters. It’s possible for additional positional parameters to becollected into a sequence object. Further, additional keyword parameterscan be collected into a dictionary object.


limit your lines to 80 positions or less. You may need to breaklong statements with a \ at the end of a line. Also,parenthesized expressions can be continued onto the next line without a \.Some programmers will put in extra () just to make line breaks neat.

Names. Function names are typically mixedCase(). However, afew important functions were done in CapWords() style with aleading upper case letter. This can cause confusion with class names,and the recommended style is a leading lowercase letter for function names.

In some languages, many related functions will all be given a commonprefix. Functions may be called inet_addr(), inet_network(),inet_makeaddr(), inet_lnaof(), inet_netof(),inet_ntoa(), etc. Because Python has classes (covered in Data + Processing = Objects)and modules (covered in Components, Modules and Packages), this kind of function-nameprefix is not used in Python programs. The class or module name is theprefix. Look at the example of math and randomfor guidance on this.

Parameter names are also typically mixedCase. In the eventthat a parameter or variable name conflicts with a Python keyword, thename is extended with an _. In the following example, we want ourparameter to be named range, but this conflicts with thebuiltin function range(). We use a trailing _ to sort this out.

def integrate( aFunction, range_ ):
    """Integrate a function over a range."""
     ...

Blank lines are used sparingly in a Python file, generally to separateunrelated material. Typicaly, function definitions are separated bysingle blank lines. A long or complex function might have blank lineswithin the body. When this is the case, it might be worth consideringbreaking the function into separate pieces.

Docstrings. The first line of the body of a function is called a docstring.The recommended forms for docstrings are described in Python Extension Proposal (PEP) 257.

Typically, the first line of the docstring is a pithy summary of thefunction. This may be followed by a blank line and more detailedinformation. The one-line summary should be a complete sentence.






posted on 2022-07-05 18:12  我在全球村  阅读(38)  评论(0编辑  收藏  举报