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 not mangled, e.g., the__fooinimport __foo.baris not mangled.The name of an imported member, e.g.,
__finfrom spam import __f.
The transformation rule is defined as follows:
The class name, with leading underscores removed and a single leading underscore inserted, is inserted in front of the identifier, e.g., the identifier
__spamoccurring in a class namedFoo,_Fooor__Foois transformed to_Foo__spam.If the class name consists only of underscores, the transformation is the identity, e.g., the identifier
__spamoccurring in a class named_or__is left as is.
6.2.3. Literals¶
A literal is a textual representation of a value. Python supports numeric, string and bytes literals. Format strings and template strings are treated as string literals.
Numeric literals consist of a single NUMBER
token, which names an integer, floating-point number, or an imaginary number.
See the Numeric literals section in Lexical analysis documentation for details.
String and bytes literals may consist of several tokens. See section String literal concatenation for details.
Note that negative and complex numbers, like -3 or 3+4.2j,
are syntactically not literals, but unary or
binary arithmetic operations involving the - or +
operator.
Evaluation of a literal yields an object of the given type
(int, float, complex, str,
bytes, or Template) with the given value.
The value may be approximated in the case of floating-point
and imaginary literals.
The formal grammar for literals is:
literal:strings|NUMBER
6.2.3.1. Literals and object identity¶
All literals correspond to immutable data types, and hence the object's identity is less important than its value. Multiple evaluations of literals with the same value (either the same occurrence in the program text or a different occurrence) may obtain the same object or a different object with the same value.
CPython implementation detail
For example, in CPython, small integers with the same value evaluate to the same object:
>>> x = 7
>>> y = 7
>>> x is y
True
However, large integers evaluate to different objects:
>>> x = 123456789
>>> y = 123456789
>>> x is y
False
This behavior may change in future versions of CPython. In particular, the boundary between "small" and "large" integers has already changed in the past.
CPython will emit a SyntaxWarning when you compare literals
using is:
>>> x = 7
>>> x is 7
<input>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
True
See 我什麼時候可以依靠 is 運算子進行識別性測試? for more information.
Template strings are immutable but may reference mutable
objects as Interpolation values.
For the purposes of this section, two t-strings have the "same value" if
both their structure and the identity of the values match.
CPython 實作細節: Currently, each evaluation of a template string results in a different object.
6.2.3.2. String literal concatenation¶
Multiple adjacent string or bytes literals, possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation:
>>> "hello" 'world'
"helloworld"
This feature is defined at the syntactical level, so it only works with literals. To concatenate string expressions at run time, the '+' operator may be used:
>>> greeting = "Hello"
>>> space = " "
>>> name = "Blaise"
>>> print(greeting + space + name) # 不是:print(greeting space name)
Hello Blaise
Literal concatenation can freely mix raw strings, triple-quoted strings, and formatted string literals. For example:
>>> "Hello" r', ' f"{name}!"
"Hello, Blaise!"
This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings. For example:
re.compile("[A-Za-z_]" # letter or underscore
"[A-Za-z0-9_]*" # letter, digit or underscore
)
However, bytes literals may only be combined with other byte literals; not with string literals of any kind. Also, template string literals may only be combined with other template string literals:
>>>