6. 運算式¶
This chapter explains the meaning of the elements of expressions in Python.
Syntax Notes: In this and the following chapters, grammar notation will be used to describe syntax, not lexical analysis.
When (one alternative of) a syntax rule has the form:
name: othername
and no semantics are given, the semantics of this form of name are the same
as for othername.
6.1. Arithmetic conversions¶
When a description of an arithmetic operator below uses the phrase "the numeric arguments are converted to a common real type", this means that the operator implementation for built-in numeric types works as described in the Numeric Types section of the standard library documentation.
Some additional rules apply for certain operators and non-numeric operands
(for example, a string as a left argument to the % operator).
Extensions must define their own conversion behavior.
6.2. Atoms¶
Atoms are the most basic elements of expressions. The simplest atoms are names or literals. Forms enclosed in parentheses, brackets or braces are also categorized syntactically as atoms.
Formally, the syntax for atoms is:
atom: | 'True' | 'False' | 'None' | '...' |identifier|literal|enclosureenclosure: |parenth_form|list_display|dict_display|set_display|generator_expression|yield_atom
6.2.1. 內建常數¶
The keywords True, False, and None name
built-in constants.
The token ... names the Ellipsis constant.
Evaluation of these atoms yields the corresponding value.
備註
Several more built-in constants are available as global variables, but only the ones mentioned here are keywords. In particular, these names cannot be reassigned or used as attributes:
>>> False = 123
File "<input>", line 1
False = 123
^^^^^
SyntaxError: cannot assign to False
6.2.2. Identifiers (Names)¶
An identifier occurring as an atom is a name. See section Names (identifiers and keywords) for lexical definition and section Naming and binding for documentation of naming and binding.
When the name is bound to an object, evaluation of the atom yields that object.
When a name is not bound, an attempt to evaluate it raises a NameError
exception.
6.2.2.1. Private name mangling¶
When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class.
也參考
The class specifications.
More precisely, private names are transformed to a longer form before code is generated for them. If the transformed name is longer than 255 characters, implementation-defined truncation may happen.
The transformation is independent of the syntactical context in which the identifier is used but only the following private identifiers are mangled:
Any name used as the name of a variable that is assigned or read or any name of an attribute being accessed.
The
__name__attribute of nested functions, classes, and type aliases is however not mangled.The name of imported modules, e.g.,
__spaminimport __spam. If the module is part of a package (i.e., its name contains a dot), the name is