�ɲɾ�����ӯ�����һ��ˣ��������С���˴��ͣ�������P���ҹ��ñ˽��ά�Բ��������˸߸ԣ�������ơ��ҹ��ñ�����ά�Բ���ˡ���˳^�ӣ������ӡ� ���ͯj�ӣ��ƺ���ӣ� ? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!? PNG ?%k25u25%fgd5n!PKr|0]d8m-- _pydoc.cssnu[/* CSS file for pydoc. Contents of this file are subject to change without notice. */ body { background-color: #f0f0f8; } table.heading tr { background-color: #7799ee; } .decor { color: #ffffff; } .title-decor { background-color: #ffc8d8; color: #000000; } .pkg-content-decor { background-color: #aa55cc; } .index-decor { background-color: #ee77aa; } .functions-decor { background-color: #eeaa77; } .data-decor { background-color: #55aa55; } .author-decor { background-color: #7799ee; } .credits-decor { background-color: #7799ee; } .error-decor { background-color: #bb0000; } .grey { color: #909090; } .white { color: #ffffff; } .repr { color: #c040c0; } table.heading tr td.title { vertical-align: bottom; } table.heading tr td.extra { vertical-align: bottom; text-align: right; } .heading-text { font-family: helvetica, arial; } .bigsection { font-size: larger; } .title { font-size: x-large; } .code { font-family: monospace; } table { width: 100%; border-spacing : 0; border-collapse : collapse; border: 0; } td { padding: 2; } td.section-title { vertical-align: bottom; } td.multicolumn { width: 25%; vertical-align: bottom; } td.singlecolumn { width: 100%; } PKr|0]!__pycache__/topics.cpython-36.pycnu[3 \ N@sdddddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&dd'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLMZdMS)NauThe "assert" statement ********************** Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= "assert" expression ["," expression] The simple form, "assert expression", is equivalent to if __debug__: if not expression: raise AssertionError The extended form, "assert expression1, expression2", is equivalent to if __debug__: if not expression1: raise AssertionError(expression2) These equivalences assume that "__debug__" and "AssertionError" refer to the built-in variables with those names. In the current implementation, the built-in variable "__debug__" is "True" under normal circumstances, "False" when optimization is requested (command line option "-O"). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace. Assignments to "__debug__" are illegal. The value for the built-in variable is determined when the interpreter starts. us+Assignment statements ********************* Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: assignment_stmt ::= (target_list "=")+ (starred_expression | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" [target_list] ")" | "[" [target_list] "]" | attributeref | subscription | slicing | "*" target (See section Primaries for the syntax definitions for *attributeref*, *subscription*, and *slicing*.) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy). Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows. * If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target. * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. * If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be an iterable with at least as many items as there are targets in the target list, minus one. The first items of the iterable are assigned, from left to right, to the targets before the starred target. The final items of the iterable are assigned to the targets after the starred target. A list of the remaining items in the iterable is then assigned to the starred target (the list can be empty). * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): * If the name does not occur in a "global" or "nonlocal" statement in the current code block: the name is bound to the object in the current local namespace. * Otherwise: the name is bound to the object in the global namespace or the outer namespace determined by "nonlocal", respectively. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, "TypeError" is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily "AttributeError"). Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression, "a.x" can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS target "a.x" is always set as an instance attribute, creating it if necessary. Thus, the two occurrences of "a.x" do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment: class Cls: x = 3 # class variable inst = Cls() inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3 This description does not necessarily apply to descriptor attributes, such as properties created with "property()". * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list), the subscript must yield an integer. If it is negative, the sequence’s length is added to it. The resulting value must be a nonnegative integer less than the sequence’s length, and the sequence is asked to assign the assigned object to its item with that index. If the index is out of range, "IndexError" is raised (assignment to a subscripted sequence cannot add new items to a list). If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping’s key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed). For user-defined objects, the "__setitem__()" method is called with appropriate arguments. * If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence’s length. The bounds should evaluate to integers. If either bound is negative, the sequence’s length is added to it. The resulting bounds are clipped to lie between zero and the sequence’s length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it. **CPython implementation detail:** In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages. Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example "a, b = b, a" swaps two variables), overlaps *within* the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints "[0, 2]": x = [0, 1] i = 0 i, x[i] = 1, 2 # i is updated, then x[i] is updated print(x) See also: **PEP 3132** - Extended Iterable Unpacking The specification for the "*target" feature. Augmented assignment statements =============================== Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. Annotated assignment statements =============================== Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement: annotated_assignment_stmt ::= augtarget ":" expression ["=" expression] The difference from normal Assignment statements is that only single target and only single right hand side value is allowed. For simple names as assignment targets, if in class or module scope, the annotations are evaluated and stored in a special class or module attribute "__annotations__" that is a dictionary mapping from variable names (mangled if private) to evaluated annotations. This attribute is writable and is automatically created at the start of class or module body execution, if annotations are found statically. For expressions as assignment targets, the annotations are evaluated if in class or module scope, but not stored. If a name is annotated in a function scope, then this name is local for that scope. Annotations are never evaluated and stored in function scopes. If the right hand side is present, an annotated assignment performs the actual assignment before evaluating annotations (where applicable). If the right hand side is not present for an expression target, then the interpreter evaluates the target except for the last "__setitem__()" or "__setattr__()" call. See also: **PEP 526** - Syntax for Variable Annotations The proposal that added syntax for annotating the types of variables (including class variables and instance variables), instead of expressing them through comments. **PEP 484** - Type hints The proposal that added the "typing" module to provide a standard syntax for type annotations that can be used in static analysis tools and IDEs. aIdentifiers (Names) ******************* An identifier occurring as an atom is a name. See section 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. **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. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier "__spam" occurring in a class named "Ham" will be transformed to "_Ham__spam". This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done. u Literals ******** Python supports string and bytes literals and various numeric literals: literal ::= stringliteral | bytesliteral | integer | floatnumber | imagnumber Evaluation of a literal yields an object of the given type (string, bytes, integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section Literals for details. 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. u-Customizing attribute access **************************** The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access =================================== For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ======================== The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors ==================== In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ ========= *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* -------------------------- * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". aAttribute references ******************** An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the "__getattr__()" method. If this attribute is not available, the exception "AttributeError" is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. aAugmented assignment statements ******************************* Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. uj Binary arithmetic operations **************************** The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non- numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr The "*" (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. The "@" (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. New in version 3.5. The "/" (division) and "//" (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the "ZeroDivisionError" exception. The "%" (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the "ZeroDivisionError" exception. The arguments may be floating point numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 + 0.34".) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [1]. The floor division and modulo operators are connected by the following identity: "x == (x//y)*y + (x%y)". Floor division and modulo are also connected with the built-in function "divmod()": "divmod(x, y) == (x//y, x%y)". [2]. In addition to performing the modulo operation on numbers, the "%" operator is also overloaded by string objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section printf-style String Formatting. The floor division operator, the modulo operator, and the "divmod()" function are not defined for complex numbers. Instead, convert to a floating point number using the "abs()" function if appropriate. The "+" (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The "-" (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. a$Binary bitwise operations ************************* Each of the three bitwise operations has a different priority level: and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr The "&" operator yields the bitwise AND of its arguments, which must be integers. The "^" operator yields the bitwise XOR (exclusive OR) of its arguments, which must be integers. The "|" operator yields the bitwise (inclusive) OR of its arguments, which must be integers. uxCode Objects ************ Code objects are used by the implementation to represent “pseudo- compiled” executable Python code such as a function body. They differ from function objects because they don’t contain a reference to their global execution environment. Code objects are returned by the built- in "compile()" function and can be extracted from function objects through their "__code__" attribute. See also the "code" module. A code object can be executed or evaluated by passing it (instead of a source string) to the "exec()" or "eval()" built-in functions. See The standard type hierarchy for more information. a.The Ellipsis Object ******************* This object is commonly used by slicing (see Slicings). It supports no special operations. There is exactly one ellipsis object, named "Ellipsis" (a built-in name). "type(Ellipsis)()" produces the "Ellipsis" singleton. It is written as "Ellipsis" or "...". uThe Null Object *************** This object is returned by functions that don’t explicitly return a value. It supports no special operations. There is exactly one null object, named "None" (a built-in name). "type(None)()" produces the same singleton. It is written as "None". u5Type Objects ************ Type objects represent the various object types. An object’s type is accessed by the built-in function "type()". There are no special operations on types. The standard module "types" defines names for all standard built-in types. Types are written like this: "". aBoolean operations ****************** or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: "False", "None", numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. User-defined objects can customize their truth value by providing a "__bool__()" method. The operator "not" yields "True" if its argument is false, "False" otherwise. The expression "x and y" first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The expression "x or y" first evaluates *x*; if *x* is true, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. Note that neither "and" nor "or" restrict the value and type they return to "False" and "True", but rather return the last evaluated argument. This is sometimes useful, e.g., if "s" is a string that should be replaced by a default value if it is empty, the expression "s or 'foo'" yields the desired value. Because "not" has to create a new value, it returns a boolean value regardless of the type of its argument (for example, "not 'foo'" produces "False" rather than "''".) a$The "break" statement ********************* break_stmt ::= "break" "break" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional "else" clause if the loop has one. If a "for" loop is terminated by "break", the loop control target keeps its current value. When "break" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the loop. uEmulating callable objects ************************** object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". uCCalls ***** A call calls a callable object (e.g., a *function*) with a possibly empty series of *arguments*: call ::= primary "(" [argument_list [","] | comprehension] ")" argument_list ::= positional_arguments ["," starred_and_keywords] ["," keywords_arguments] | starred_and_keywords ["," keywords_arguments] | keywords_arguments positional_arguments ::= ["*"] expression ("," ["*"] expression)* starred_and_keywords ::= ("*" expression | keyword_item) ("," "*" expression | "," keyword_item)* keywords_arguments ::= (keyword_item | "**" expression) ("," keyword_item | "," "**" expression)* keyword_item ::= identifier "=" expression An optional trailing comma may be present after the positional and keyword arguments but does not affect the semantics. The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a "__call__()" method are callable). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal *parameter* lists. If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a "TypeError" exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is "None", it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don’t specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a "TypeError" exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. **CPython implementation detail:** An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use "PyArg_ParseTuple()" to parse their arguments. If there are more positional arguments than there are formal parameter slots, a "TypeError" exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments). If any keyword argument does not correspond to a formal parameter name, a "TypeError" exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments. If the syntax "*expression" appears in the function call, "expression" must evaluate to an *iterable*. Elements from these iterables are treated as if they were additional positional arguments. For the call "f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, *yM*, this is equivalent to a call with M+4 positional arguments *x1*, *x2*, *y1*, …, *yM*, *x3*, *x4*. A consequence of this is that although the "*expression" syntax may appear *after* explicit keyword arguments, it is processed *before* the keyword arguments (and any "**expression" arguments – see below). So: >>> def f(a, b): ... print(a, b) ... >>> f(b=1, *(2,)) 2 1 >>> f(a=1, *(2,)) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'a' >>> f(1, *(2,)) 1 2 It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise. If the syntax "**expression" appears in the function call, "expression" must evaluate to a *mapping*, the contents of which are treated as additional keyword arguments. If a keyword is already present (as an explicit keyword argument, or from another unpacking), a "TypeError" exception is raised. Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Changed in version 3.5: Function calls accept any number of "*" and "**" unpackings, positional arguments may follow iterable unpackings ("*"), and keyword arguments may follow dictionary unpackings ("**"). Originally proposed by **PEP 448**. A call always returns some value, possibly "None", unless it raises an exception. How this value is computed depends on the type of the callable object. If it is— a user-defined function: The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a "return" statement, this specifies the return value of the function call. a built-in function or method: The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods. a class object: A new instance of that class is returned. a class instance method: The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument. a class instance: The class must define a "__call__()" method; the effect is then the same as if that method was called. u Class definitions ***************** A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. u4)Comparisons *********** Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like "a < b < c" have the interpretation that is conventional in mathematics: comparison ::= or_expr (comp_operator or_expr)* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in" Comparisons yield boolean values: "True" or "False". Comparisons can be chained arbitrarily, e.g., "x < y <= z" is equivalent to "x < y and y <= z", except that "y" is evaluated only once (but in both cases "z" is not evaluated at all when "x < y" is found to be false). Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and *op1*, *op2*, …, *opN* are comparison operators, then "a op1 b op2 c ... y opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except that each expression is evaluated at most once. Note that "a op1 b op2 c" doesn’t imply any kind of comparison between *a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though perhaps not pretty). Value comparisons ================= The operators "<", ">", "==", ">=", "<=", and "!=" compare the values of two objects. The objects do not need to have the same type. Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object’s value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation. Because all types are (direct or indirect) subtypes of "object", they inherit the default comparison behavior from "object". Types can customize their comparison behavior by implementing *rich comparison methods* like "__lt__()", described in Basic customization. The default behavior for equality comparison ("==" and "!=") is based on the identity of the objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of instances with different identities results in inequality. A motivation for this default behavior is the desire that all objects should be reflexive (i.e. "x is y" implies "x == y"). A default order comparison ("<", ">", "<=", and ">=") is not provided; an attempt raises "TypeError". A motivation for this default behavior is the lack of a similar invariant as for equality. The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that. The following list describes the comparison behavior of the most important built-in types. * Numbers of built-in numeric types (Numeric Types — int, float, complex) and of the standard library types "fractions.Fraction" and "decimal.Decimal" can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision. The not-a-number values "float('NaN')" and "Decimal('NaN')" are special. They are identical to themselves ("x is x" is true) but are not equal to themselves ("x == x" is false). Additionally, comparing any number to a not-a-number value will return "False". For example, both "3 < float('NaN')" and "float('NaN') < 3" will return "False". * Binary sequences (instances of "bytes" or "bytearray") can be compared within and across their types. They compare lexicographically using the numeric values of their elements. * Strings (instances of "str") compare lexicographically using the numerical Unicode code points (the result of the built-in function "ord()") of their characters. [3] Strings and binary sequences cannot be directly compared. * Sequences (instances of "tuple", "list", or "range") can be compared only within each of their types, with the restriction that ranges do not support order comparison. Equality comparison across these types results in inequality, and ordering comparison across these types raises "TypeError". Sequences compare lexicographically using comparison of corresponding elements, whereby reflexivity of the elements is enforced. In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element "x", "x == x" is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison, and may be surprising: The non-reflexive not-a-number values for example result in the following comparison behavior when used in a list: >>> nan = float('NaN') >>> nan is nan True >>> nan == nan False <-- the defined non-reflexive behavior of NaN >>> [nan] == [nan] True <-- list enforces reflexivity and tests identity first Lexicographical comparison between built-in collections works as follows: * For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, "[1,2] == (1,2)" is false because the type is not the same). * Collections that support order comparison are ordered the same as their first unequal elements (for example, "[1,2,x] <= [1,2,y]" has the same value as "x <= y"). If a corresponding element does not exist, the shorter collection is ordered first (for example, "[1,2] < [1,2,3]" is true). * Mappings (instances of "dict") compare equal if and only if they have equal *(key, value)* pairs. Equality comparison of the keys and values enforces reflexivity. Order comparisons ("<", ">", "<=", and ">=") raise "TypeError". * Sets (instances of "set" or "frozenset") can be compared within and across their types. They define order comparison operators to mean subset and superset tests. Those relations do not define total orderings (for example, the two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one another, nor supersets of one another). Accordingly, sets are not appropriate arguments for functions which depend on total ordering (for example, "min()", "max()", and "sorted()" produce undefined results given a list of sets as inputs). Comparison of sets enforces reflexivity of its elements. * Most other built-in types have no comparison methods implemented, so they inherit the default comparison behavior. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: * Equality comparison should be reflexive. In other words, identical objects should compare equal: "x is y" implies "x == y" * Comparison should be symmetric. In other words, the following expressions should have the same result: "x == y" and "y == x" "x != y" and "y != x" "x < y" and "y > x" "x <= y" and "y >= x" * Comparison should be transitive. The following (non-exhaustive) examples illustrate that: "x > y and y > z" implies "x > z" "x < y and y <= z" implies "x < z" * Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: "x == y" and "not x != y" "x < y" and "not x >= y" (for total ordering) "x > y" and "not x <= y" (for total ordering) The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the "total_ordering()" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. Python does not enforce these consistency rules. In fact, the not-a-number values are an example for not following these rules. Membership test operations ========================== The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. Object identity is determined using the "id()" function. "x is not y" yields the inverse truth value. [4] uxeCompound statements ******************* Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The "if", "while" and "for" statements implement traditional control flow constructs. "try" specifies exception handlers and/or cleanup code for a group of statements, while the "with" statement allows the execution of initialization and finalization code around a block of code. Function and class definitions are also syntactically compound statements. A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of a suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which "if" clause a following "else" clause would belong: if test1: if test2: print(x) Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the "print()" calls are executed: if x < y < z: print(x); print(y); print(z) Summarizing: compound_stmt ::= if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | async_with_stmt | async_for_stmt | async_funcdef suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT statement ::= stmt_list NEWLINE | compound_stmt stmt_list ::= simple_stmt (";" simple_stmt)* [";"] Note that statements always end in a "NEWLINE" possibly followed by a "DEDENT". Also note that optional continuation clauses always begin with a keyword that cannot start a statement, thus there are no ambiguities (the ‘dangling "else"’ problem is solved in Python by requiring nested "if" statements to be indented). The formatting of the grammar rules in the following sections places each clause on a separate line for clarity. The "if" statement ================== The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. The "while" statement ===================== The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. The "for" statement =================== The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) The "try" statement =================== The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. The "with" statement ==================== The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Function definitions ==================== A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. Class definitions ================= A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. Coroutines ========== New in version 3.5. Coroutine function definition ----------------------------- async_funcdef ::= [decorators] "async" "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite Execution of Python coroutines can be suspended and resumed at many points (see *coroutine*). In the body of a coroutine, any "await" and "async" identifiers become reserved keywords; "await" expressions, "async for" and "async with" can only be used in coroutine bodies. Functions defined with "async def" syntax are always coroutine functions, even if they do not contain "await" or "async" keywords. It is a "SyntaxError" to use "yield from" expressions in "async def" coroutines. An example of a coroutine function: async def func(param1, param2): do_stuff() await some_coroutine() The "async for" statement ------------------------- async_for_stmt ::= "async" for_stmt An *asynchronous iterable* is able to call asynchronous code in its *iter* implementation, and *asynchronous iterator* can call asynchronous code in its *next* method. The "async for" statement allows convenient iteration over asynchronous iterators. The following code: async for TARGET in ITER: BLOCK else: BLOCK2 Is semantically equivalent to: iter = (ITER) iter = type(iter).__aiter__(iter) running = True while running: try: TARGET = await type(iter).__anext__(iter) except StopAsyncIteration: running = False else: BLOCK else: BLOCK2 See also "__aiter__()" and "__anext__()" for details. It is a "SyntaxError" to use "async for" statement outside of an "async def" function. The "async with" statement -------------------------- async_with_stmt ::= "async" with_stmt An *asynchronous context manager* is a *context manager* that is able to suspend execution in its *enter* and *exit* methods. The following code: async with EXPR as VAR: BLOCK Is semantically equivalent to: mgr = (EXPR) aexit = type(mgr).__aexit__ aenter = type(mgr).__aenter__(mgr) VAR = await aenter try: BLOCK except: if not await aexit(mgr, *sys.exc_info()): raise else: await aexit(mgr, None, None, None) See also "__aenter__()" and "__aexit__()" for details. It is a "SyntaxError" to use "async with" statement outside of an "async def" function. See also: **PEP 492** - Coroutines with async and await syntax The proposal that made coroutines a proper standalone concept in Python, and added supporting syntax. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] A string literal appearing as the first statement in the function body is transformed into the function’s "__doc__" attribute and therefore the function’s *docstring*. [3] A string literal appearing as the first statement in the class body is transformed into the namespace’s "__doc__" item and therefore the class’s *docstring*. uWith Statement Context Managers ******************************* A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. aThe "continue" statement ************************ continue_stmt ::= "continue" "continue" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition or "finally" clause within that loop. It continues with the next cycle of the nearest enclosing loop. When "continue" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really starting the next loop cycle. uArithmetic conversions ********************** When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type,” this means that the operator implementation for built-in types works as follows: * If either argument is a complex number, the other is converted to complex; * otherwise, if either argument is a floating point number, the other is converted to floating point; * otherwise, both must be integers and no conversion is necessary. Some additional rules apply for certain operators (e.g., a string as a left argument to the ‘%’ operator). Extensions must define their own conversion behavior. u3Basic customization ******************* object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. uiF"pdb" — The Python Debugger *************************** **Source code:** Lib/pdb.py ====================================================================== The module "pdb" defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible – it is actually defined as the class "Pdb". This is currently undocumented but easily understood by reading the source. The extension interface uses the modules "bdb" and "cmd". The debugger’s prompt is "(Pdb)". Typical usage to run a program under control of the debugger is: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') > (0)?() (Pdb) continue > (1)?() (Pdb) continue NameError: 'spam' > (1)?() (Pdb) Changed in version 3.3: Tab-completion via the "readline" module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the "p" command. "pdb.py" can also be invoked as a script to debug other scripts. For example: python3 -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post- mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit. New in version 3.2: "pdb.py" now accepts a "-c" option that executes commands as if given in a ".pdbrc" file, see Debugger Commands. The typical usage to break into the debugger from a running program is to insert import pdb; pdb.set_trace() at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the "continue" command. The typical usage to inspect a crashed program is: >>> import pdb >>> import mymodule >>> mymodule.test() Traceback (most recent call last): File "", line 1, in File "./mymodule.py", line 4, in test test2() File "./mymodule.py", line 3, in test2 print(spam) NameError: spam >>> pdb.pm() > ./mymodule.py(3)test2() -> print(spam) (Pdb) The module defines the following functions; each enters the debugger in a slightly different way: pdb.run(statement, globals=None, locals=None) Execute the *statement* (given as a string or a code object) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type "continue", or you can step through the statement using "step" or "next" (all these commands are explained below). The optional *globals* and *locals* arguments specify the environment in which the code is executed; by default the dictionary of the module "__main__" is used. (See the explanation of the built-in "exec()" or "eval()" functions.) pdb.runeval(expression, globals=None, locals=None) Evaluate the *expression* (given as a string or a code object) under debugger control. When "runeval()" returns, it returns the value of the expression. Otherwise this function is similar to "run()". pdb.runcall(function, *args, **kwds) Call the *function* (a function or method object, not a string) with the given arguments. When "runcall()" returns, it returns whatever the function call returned. The debugger prompt appears as soon as the function is entered. pdb.set_trace() Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). pdb.post_mortem(traceback=None) Enter post-mortem debugging of the given *traceback* object. If no *traceback* is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used). pdb.pm() Enter post-mortem debugging of the traceback found in "sys.last_traceback". The "run*" functions and "set_trace()" are aliases for instantiating the "Pdb" class and calling the method of the same name. If you want to access further features, you have to do this yourself: class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True) "Pdb" is the debugger class. The *completekey*, *stdin* and *stdout* arguments are passed to the underlying "cmd.Cmd" class; see the description there. The *skip* argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] By default, Pdb sets a handler for the SIGINT signal (which is sent when the user presses "Ctrl-C" on the console) when you give a "continue" command. This allows you to break into the debugger again by pressing "Ctrl-C". If you want Pdb not to touch the SIGINT handler, set *nosigint* to true. The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. Example call to enable tracing with *skip*: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 3.1: The *skip* argument. New in version 3.2: The *nosigint* argument. Previously, a SIGINT handler was never set by Pdb. Changed in version 3.6: The *readrc* argument. run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) set_trace() See the documentation for the functions explained above. Debugger Commands ================= The commands recognized by the debugger are listed below. Most commands can be abbreviated to one or two letters as indicated; e.g. "h(elp)" means that either "h" or "help" can be used to enter the help command (but not "he" or "hel", nor "H" or "Help" or "HELP"). Arguments to commands must be separated by whitespace (spaces or tabs). Optional arguments are enclosed in square brackets ("[]") in the command syntax; the square brackets must not be typed. Alternatives in the command syntax are separated by a vertical bar ("|"). Entering a blank line repeats the last command entered. Exception: if the last command was a "list" command, the next 11 lines are listed. Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point ("!"). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debugger’s state is not changed. The debugger supports aliases. Aliases can have parameters which allows one a certain level of adaptability to the context under examination. Multiple commands may be entered on a single line, separated by ";;". (A single ";" is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ";;" pair, even if it is in the middle of a quoted string. If a file ".pdbrc" exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file. Changed in version 3.2: ".pdbrc" can now contain commands that continue debugging, such as "continue" or "next". Previously, these commands had no effect. h(elp) [command] Without argument, print the list of available commands. With a *command* as argument, print help about that command. "help pdb" displays the full documentation (the docstring of the "pdb" module). Since the *command* argument must be an identifier, "help exec" must be entered to get help on the "!" command. w(here) Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands. d(own) [count] Move the current frame *count* (default one) levels down in the stack trace (to a newer frame). u(p) [count] Move the current frame *count* (default one) levels up in the stack trace (to an older frame). b(reak) [([filename:]lineno | function) [, condition]] With a *lineno* argument, set a break there in the current file. With a *function* argument, set a break at the first executable statement within that function. The line number may be prefixed with a filename and a colon, to specify a breakpoint in another file (probably one that hasn’t been loaded yet). The file is searched on "sys.path". Note that each breakpoint is assigned a number to which all the other breakpoint commands refer. If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored. Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any. tbreak [([filename:]lineno | function) [, condition]] Temporary breakpoint, which is removed automatically when it is first hit. The arguments are the same as for "break". cl(ear) [filename:lineno | bpnumber [bpnumber ...]] With a *filename:lineno* argument, clear all the breakpoints at this line. With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). disable [bpnumber [bpnumber ...]] Disable the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled. enable [bpnumber [bpnumber ...]] Enable the breakpoints specified. ignore bpnumber [count] Set the ignore count for the given breakpoint number. If count is omitted, the ignore count is set to 0. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true. condition bpnumber [condition] Set a new *condition* for the breakpoint, an expression which must evaluate to true before the breakpoint is honored. If *condition* is absent, any existing condition is removed; i.e., the breakpoint is made unconditional. commands [bpnumber] Specify a list of commands for breakpoint number *bpnumber*. The commands themselves appear on the following lines. Type a line containing just "end" to terminate the commands. An example: (Pdb) commands 1 (com) p some_variable (com) end (Pdb) To remove all commands from a breakpoint, type commands and follow it immediately with "end"; that is, give no commands. With no *bpnumber* argument, commands refers to the last breakpoint set. You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution. Specifying any command resuming execution (currently continue, step, next, return, jump, quit and their abbreviations) terminates the command list (as if that command was immediately followed by end). This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint—which could have its own command list, leading to ambiguities about which list to execute. If you use the ‘silent’ command in the command list, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the other commands print anything, you see no sign that the breakpoint was reached. s(tep) Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function). n(ext) Continue execution until the next line in the current function is reached or it returns. (The difference between "next" and "step" is that "step" stops inside a called function, while "next" executes called functions at (nearly) full speed, only stopping at the next line in the current function.) unt(il) [lineno] Without argument, continue execution until the line with a number greater than the current one is reached. With a line number, continue execution until a line with a number greater or equal to that is reached. In both cases, also stop when the current frame returns. Changed in version 3.2: Allow giving an explicit line number. r(eturn) Continue execution until the current function returns. c(ont(inue)) Continue execution, only stop when a breakpoint is encountered. j(ump) lineno Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run. It should be noted that not all jumps are allowed – for instance it is not possible to jump into the middle of a "for" loop or out of a "finally" clause. l(ist) [first[, last]] List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With "." as argument, list 11 lines around the current line. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count. The current line in the current frame is indicated by "->". If an exception is being debugged, the line where the exception was originally raised or propagated is indicated by ">>", if it differs from the current line. New in version 3.2: The ">>" marker. ll | longlist List all source code for the current function or frame. Interesting lines are marked as for "list". New in version 3.2. a(rgs) Print the argument list of the current function. p expression Evaluate the *expression* in the current context and print its value. Note: "print()" can also be used, but is not a debugger command — this executes the Python "print()" function. pp expression Like the "p" command, except the value of the expression is pretty- printed using the "pprint" module. whatis expression Print the type of the *expression*. source expression Try to get source code for the given object and display it. New in version 3.2. display [expression] Display the value of the expression if it changed, each time execution stops in the current frame. Without expression, list all display expressions for the current frame. New in version 3.2. undisplay [expression] Do not display the expression any more in the current frame. Without expression, clear all display expressions for the current frame. New in version 3.2. interact Start an interactive interpreter (using the "code" module) whose global namespace contains all the (global and local) names found in the current scope. New in version 3.2. alias [name [command]] Create an alias called *name* that executes *command*. The command must *not* be enclosed in quotes. Replaceable parameters can be indicated by "%1", "%2", and so on, while "%*" is replaced by all the parameters. If no command is given, the current alias for *name* is shown. If no arguments are given, all aliases are listed. Aliases may be nested and can contain anything that can be legally typed at the pdb prompt. Note that internal pdb commands *can* be overridden by aliases. Such a command is then hidden until the alias is removed. Aliasing is recursively applied to the first word of the command line; all other words in the line are left alone. As an example, here are two useful aliases (especially when placed in the ".pdbrc" file): # Print instance variables (usage "pi classInst") alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k]) # Print instance variables in self alias ps pi self unalias name Delete the specified alias. ! statement Execute the (one-line) *statement* in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a "global" statement on the same line, e.g.: (Pdb) global list_options; list_options = ['-l'] (Pdb) run [args ...] restart [args ...] Restart the debugged Python program. If an argument is supplied, it is split with "shlex" and the result is used as the new "sys.argv". History, breakpoints, actions and debugger options are preserved. "restart" is an alias for "run". q(uit) Quit from the debugger. The program being executed is aborted. -[ Footnotes ]- [1] Whether a frame is considered to originate in a certain module is determined by the "__name__" in the frame globals. aThe "del" statement ******************* del_stmt ::= "del" target_list Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints. Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a "global" statement in the same code block. If the name is unbound, a "NameError" exception will be raised. Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object). Changed in version 3.2: Previously it was illegal to delete a name from the local namespace if it occurs as a free variable in a nested block. u Dictionary displays ******************* A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression | "**" or_expr dict_comprehension ::= expression ":" expression comp_for A dictionary display yields a new dictionary object. If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given. A double asterisk "**" denotes *dictionary unpacking*. Its operand must be a *mapping*. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings. New in version 3.5: Unpacking into dictionary displays, originally proposed by **PEP 448**. A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual “for” and “if” clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced. Restrictions on the types of the key values are listed earlier in section The standard type hierarchy. (To summarize, the key type should be *hashable*, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails. aInteraction with dynamic features ********************************* Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aBThe "if" statement ****************** The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. uExceptions ********** Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. u$Execution model *************** Structure of a program ====================== A Python program is constructed from code blocks. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the "-c" option) is a code block. The string argument passed to the built-in functions "eval()" and "exec()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed. Naming and binding ================== Binding of names ---------------- *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names ------------------- A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution --------------------------------- **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features --------------------------------- Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. Exceptions ========== Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. uoExpression lists **************** expression_list ::= expression ("," expression)* [","] starred_list ::= starred_item ("," starred_item)* [","] starred_expression ::= expression | (starred_item ",")* [starred_item] starred_item ::= expression | "*" or_expr Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. An asterisk "*" denotes *iterable unpacking*. Its operand must be an *iterable*. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking. New in version 3.5: Iterable unpacking in expression lists, originally proposed by **PEP 448**. The trailing comma is required only to create a single tuple (a.k.a. a *singleton*); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: "()".) aFloating point literals *********************** Floating point literals are described by the following lexical definitions: floatnumber ::= pointfloat | exponentfloat pointfloat ::= [digitpart] fraction | digitpart "." exponentfloat ::= (digitpart | pointfloat) exponent digitpart ::= digit (["_"] digit)* fraction ::= "." digitpart exponent ::= ("e" | "E") ["+" | "-"] digitpart Note that the integer and exponent parts are always interpreted using radix 10. For example, "077e010" is legal, and denotes the same number as "77e10". The allowed range of floating point literals is implementation-dependent. As in integer literals, underscores are supported for digit grouping. Some examples of floating point literals: 3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. u The "for" statement ******************* The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) u YFormat String Syntax ******************** The "str.format()" method and the "Formatter" class share the same syntax for format strings (although in the case of "Formatter", subclasses can define their own format string syntax). The syntax is related to that of formatted string literals, but there are differences. Format strings contain “replacement fields” surrounded by curly braces "{}". Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: "{{" and "}}". The grammar for a replacement field is as follows: replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | digit+] attribute_name ::= identifier element_index ::= digit+ | index_string index_string ::= + conversion ::= "r" | "s" | "a" format_spec ::= In less formal terms, the replacement field can start with a *field_name* that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is preceded by an exclamation point "'!'", and a *format_spec*, which is preceded by a colon "':'". These specify a non-default format for the replacement value. See also the Format Specification Mini-Language section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings "'10'" or "':-]'") within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form "'.name'" selects the named attribute using "getattr()", while an expression of the form "'[index]'" does an index lookup using "__getitem__()". Changed in version 3.1: The positional argument specifiers can be omitted for "str.format()", so "'{} {}'.format(a, b)" is equivalent to "'{0} {1}'.format(a, b)". Changed in version 3.4: The positional argument specifiers can be omitted for "Formatter". Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the "__format__()" method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling "__format__()", the normal formatting logic is bypassed. Three conversion flags are currently supported: "'!s'" which calls "str()" on the value, "'!r'" which calls "repr()" and "'!a'" which calls "ascii()". Some examples: "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own “formatting mini-language” or interpretation of the *format_spec*. Most built-in types support a common formatting mini-language, which is described in the next section. A *format_spec* field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the *format_spec* string is interpreted. This allows the formatting of a value to be dynamically specified. See the Format examples section for some examples. Format Specification Mini-Language ================================== “Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax and Formatted string literals). They can also be passed directly to the built-in "format()" function. Each formattable type may define how the format specification is to be interpreted. Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types. A general convention is that an empty format string ("""") produces the same result as if you had called "str()" on the value. A non-empty format string typically modifies the result. The general form of a *standard format specifier* is: format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type] fill ::= align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= digit+ grouping_option ::= "_" | "," precision ::= digit+ type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" If a valid *align* value is specified, it can be preceded by a *fill* character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (“"{"” or “"}"”) as the *fill* character in a formatted string literal or when using the "str.format()" method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the "format()" function. The meaning of the various alignment options is as follows: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'<'" | Forces the field to be left-aligned within the available | | | space (this is the default for most objects). | +-----------+------------------------------------------------------------+ | "'>'" | Forces the field to be right-aligned within the available | | | space (this is the default for numbers). | +-----------+------------------------------------------------------------+ | "'='" | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form ‘+000000120’. This alignment option is only | | | valid for numeric types. It becomes the default when ‘0’ | | | immediately precedes the field width. | +-----------+------------------------------------------------------------+ | "'^'" | Forces the field to be centered within the available | | | space. | +-----------+------------------------------------------------------------+ Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The *sign* option is only valid for number types, and can be one of the following: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'+'" | indicates that a sign should be used for both positive as | | | well as negative numbers. | +-----------+------------------------------------------------------------+ | "'-'" | indicates that a sign should be used only for negative | | | numbers (this is the default behavior). | +-----------+------------------------------------------------------------+ | space | indicates that a leading space should be used on positive | | | numbers, and a minus sign on negative numbers. | +-----------+------------------------------------------------------------+ The "'#'" option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and Decimal types. For integers, when binary, octal, or hexadecimal output is used, this option adds the prefix respective "'0b'", "'0o'", or "'0x'" to the output value. For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for "'g'" and "'G'" conversions, trailing zeros are not removed from the result. The "','" option signals the use of a comma for a thousands separator. For a locale aware separator, use the "'n'" integer presentation type instead. Changed in version 3.1: Added the "','" option (see also **PEP 378**). The "'_'" option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type "'d'". For integer presentation types "'b'", "'o'", "'x'", and "'X'", underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error. Changed in version 3.6: Added the "'_'" option (see also **PEP 515**). *width* is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the *width* field by a zero ("'0'") character enables sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of "'0'" with an *alignment* type of "'='". The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with "'f'" and "'F'", or before and after the decimal point for a floating point value formatted with "'g'" or "'G'". For non- number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The *precision* is not allowed for integer values. Finally, the *type* determines how the data should be presented. The available string presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'s'" | String format. This is the default type for strings and | | | may be omitted. | +-----------+------------------------------------------------------------+ | None | The same as "'s'". | +-----------+------------------------------------------------------------+ The available integer presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'b'" | Binary format. Outputs the number in base 2. | +-----------+------------------------------------------------------------+ | "'c'" | Character. Converts the integer to the corresponding | | | unicode character before printing. | +-----------+------------------------------------------------------------+ | "'d'" | Decimal Integer. Outputs the number in base 10. | +-----------+------------------------------------------------------------+ | "'o'" | Octal format. Outputs the number in base 8. | +-----------+------------------------------------------------------------+ | "'x'" | Hex format. Outputs the number in base 16, using lower- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'X'" | Hex format. Outputs the number in base 16, using upper- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'d'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | None | The same as "'d'". | +-----------+------------------------------------------------------------+ In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except "'n'" and "None"). When doing so, "float()" is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'e'" | Exponent notation. Prints the number in scientific | | | notation using the letter ‘e’ to indicate the exponent. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'E'" | Exponent notation. Same as "'e'" except it uses an upper | | | case ‘E’ as the separator character. | +-----------+------------------------------------------------------------+ | "'f'" | Fixed-point notation. Displays the number as a fixed-point | | | number. The default precision is "6". | +-----------+------------------------------------------------------------+ | "'F'" | Fixed-point notation. Same as "'f'", but converts "nan" to | | | "NAN" and "inf" to "INF". | +-----------+------------------------------------------------------------+ | "'g'" | General format. For a given precision "p >= 1", this | | | rounds the number to "p" significant digits and then | | | formats the result in either fixed-point format or in | | | scientific notation, depending on its magnitude. The | | | precise rules are as follows: suppose that the result | | | formatted with presentation type "'e'" and precision "p-1" | | | would have exponent "exp". Then if "-4 <= exp < p", the | | | number is formatted with presentation type "'f'" and | | | precision "p-1-exp". Otherwise, the number is formatted | | | with presentation type "'e'" and precision "p-1". In both | | | cases insignificant trailing zeros are removed from the | | | significand, and the decimal point is also removed if | | | there are no remaining digits following it. Positive and | | | negative infinity, positive and negative zero, and nans, | | | are formatted as "inf", "-inf", "0", "-0" and "nan" | | | respectively, regardless of the precision. A precision of | | | "0" is treated as equivalent to a precision of "1". The | | | default precision is "6". | +-----------+------------------------------------------------------------+ | "'G'" | General format. Same as "'g'" except switches to "'E'" if | | | the number gets too large. The representations of infinity | | | and NaN are uppercased, too. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'g'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | "'%'" | Percentage. Multiplies the number by 100 and displays in | | | fixed ("'f'") format, followed by a percent sign. | +-----------+------------------------------------------------------------+ | None | Similar to "'g'", except that fixed-point notation, when | | | used, has at least one digit past the decimal point. The | | | default precision is as high as needed to represent the | | | particular value. The overall effect is to match the | | | output of "str()" as altered by the other format | | | modifiers. | +-----------+------------------------------------------------------------+ Format examples =============== This section contains examples of the "str.format()" syntax and comparison with the old "%"-formatting. In most of the cases the syntax is similar to the old "%"-formatting, with the addition of the "{}" and with ":" used instead of "%". For example, "'%03.2f'" can be translated to "'{:03.2f}'". The new format syntax also supports new and different options, shown in the following examples. Accessing arguments by position: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 'abracadabra' Accessing arguments by name: >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 'Coordinates: 37.24N, -115.81W' >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 'Coordinates: 37.24N, -115.81W' Accessing arguments’ attributes: >>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point: ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)' Accessing arguments’ items: >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' Replacing "%s" and "%r": >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') "repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign: >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases: >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma as a thousands separator: >>> '{:,}'.format(1234567890) '1,234,567,890' Expressing a percentage: >>> points = 19 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 86.36%' Using type-specific formatting: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' Nesting arguments and more complex examples: >>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ... 'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right' >>> >>> octets = [192, 168, 0, 1] >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 'C0A80001' >>> int(_, 16) 3232235521 >>> >>> width = 5 >>> for num in range(5,12): ... for base in 'dXob': ... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ') ... print() ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 u[Function definitions ******************** A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. uThe "global" statement ********************** global_stmt ::= "global" identifier ("," identifier)* The "global" statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without "global", although free variables may refer to globals without being declared global. Names listed in a "global" statement must not be used in the same code block textually preceding that "global" statement. Names listed in a "global" statement must not be defined as formal parameters or in a "for" loop control target, "class" definition, function definition, "import" statement, or variable annotation. **CPython implementation detail:** The current implementation does not enforce some of these restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program. **Programmer’s note:** "global" is a directive to the parser. It applies only to code parsed at the same time as the "global" statement. In particular, a "global" statement contained in a string or code object supplied to the built-in "exec()" function does not affect the code block *containing* the function call, and code contained in such a string is unaffected by "global" statements in the code containing the function call. The same applies to the "eval()" and "compile()" functions. uReserved classes of identifiers ******************************* Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). u(Identifiers and keywords ************************ Identifiers (also referred to as *names*) are described by the following lexical definitions. The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also **PEP 3131** for further details. Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters "A" through "Z", the underscore "_" and, except for the first character, the digits "0" through "9". Python 3.0 introduces additional characters from outside the ASCII range (see **PEP 3131**). For these characters, the classification uses the version of the Unicode Character Database as included in the "unicodedata" module. Identifiers are unlimited in length. Case is significant. identifier ::= xid_start xid_continue* id_start ::= id_continue ::= xid_start ::= xid_continue ::= The Unicode category codes mentioned above stand for: * *Lu* - uppercase letters * *Ll* - lowercase letters * *Lt* - titlecase letters * *Lm* - modifier letters * *Lo* - other letters * *Nl* - letter numbers * *Mn* - nonspacing marks * *Mc* - spacing combining marks * *Nd* - decimal numbers * *Pc* - connector punctuations * *Other_ID_Start* - explicit list of characters in PropList.txt to support backwards compatibility * *Other_ID_Continue* - likewise All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC. A non-normative HTML file listing all valid identifier characters for Unicode 4.1 can be found at https://www.dcl.hpi.uni- potsdam.de/home/loewis/table-3131.html. Keywords ======== The following identifiers are used as reserved words, or *keywords* of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Reserved classes of identifiers =============================== Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). a5Imaginary literals ****************** Imaginary literals are described by the following lexical definitions: imagnumber ::= (floatnumber | digitpart) ("j" | "J") An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., "(3+4j)". Some examples of imaginary literals: 3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j u The "import" statement ********************** import_stmt ::= "import" module ["as" identifier] ("," module ["as" identifier])* | "from" relative_module "import" identifier ["as" identifier] ("," identifier ["as" identifier])* | "from" relative_module "import" "(" identifier ["as" identifier] ("," identifier ["as" identifier])* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ The basic import statement (no "from" clause) is executed in two steps: 1. find a module, loading and initializing it if necessary 2. define a name or names in the local namespace for the scope where the "import" statement occurs. When the statement contains multiple clauses (separated by commas) the two steps are carried out separately for each clause, just as though the clauses had been separated out into individual import statements. The details of the first step, finding and loading modules are described in greater detail in the section on the import system, which also describes the various types of packages and modules that can be imported, as well as all the hooks that can be used to customize the import system. Note that failures in this step may indicate either that the module could not be located, *or* that an error occurred while initializing the module, which includes execution of the module’s code. If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways: * If the module name is followed by "as", then the name following "as" is bound directly to the imported module. * If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module * If the module being imported is *not* a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly The "from" form uses a slightly more complex process: 1. find the module specified in the "from" clause, loading and initializing it if necessary; 2. for each of the identifiers specified in the "import" clauses: 1. check if the imported module has an attribute by that name 2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute 3. if the attribute is not found, "ImportError" is raised. 4. otherwise, a reference to that value is stored in the local namespace, using the name in the "as" clause if it is present, otherwise using the attribute name Examples: import foo # foo imported and bound locally import foo.bar.baz # foo.bar.baz imported, foo bound locally import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb from foo.bar import baz # foo.bar.baz imported and bound as baz from foo import attr # foo imported and foo.attr bound as attr If the list of identifiers is replaced by a star ("'*'"), all public names defined in the module are bound in the local namespace for the scope where the "import" statement occurs. The *public names* defined by a module are determined by checking the module’s namespace for a variable named "__all__"; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in "__all__" are all considered public and are required to exist. If "__all__" is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ("'_'"). "__all__" should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The wild card form of import — "from module import *" — is only allowed at the module level. Attempting to use it in class or function definitions will raise a "SyntaxError". When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after "from" you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute "from . import mod" from a module in the "pkg" package then you will end up importing "pkg.mod". If you execute "from ..subpkg2 import mod" from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The specification for relative imports is contained within **PEP 328**. "importlib.import_module()" is provided to support applications that determine dynamically the modules to be loaded. Future statements ================= A *future statement* is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python where the feature becomes standard. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard. future_stmt ::= "from" "__future__" "import" feature ["as" identifier] ("," feature ["as" identifier])* | "from" "__future__" "import" "(" feature ["as" identifier] ("," feature ["as" identifier])* [","] ")" feature ::= identifier A future statement must appear near the top of the module. The only lines that can appear before a future statement are: * the module docstring (if any), * comments, * blank lines, and * other future statements. The features recognized by Python 3.0 are "absolute_import", "division", "generators", "unicode_literals", "print_function", "nested_scopes" and "with_statement". They are all redundant because they are always enabled, and only kept for backwards compatibility. A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future statement contains a feature not known to it. The direct runtime semantics are the same as for any import statement: there is a standard module "__future__", described later, and it will be imported in the usual way at the time the future statement is executed. The interesting runtime semantics depend on the specific feature enabled by the future statement. Note that there is nothing special about the statement: import __future__ [as name] That is not a future statement; it’s an ordinary import statement with no special semantics or syntax restrictions. Code compiled by calls to the built-in functions "exec()" and "compile()" that occur in a module "M" containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can be controlled by optional arguments to "compile()" — see the documentation of that function for details. A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session. If an interpreter is started with the "-i" option, is passed a script name to execute, and the script includes a future statement, it will be in effect in the interactive session started after the script is executed. See also: **PEP 236** - Back to the __future__ The original proposal for the __future__ mechanism. aOMembership test operations ************************** The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". aVInteger literals **************** Integer literals are described by the following lexical definitions: integer ::= decinteger | bininteger | octinteger | hexinteger decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")* bininteger ::= "0" ("b" | "B") (["_"] bindigit)+ octinteger ::= "0" ("o" | "O") (["_"] octdigit)+ hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+ nonzerodigit ::= "1"..."9" digit ::= "0"..."9" bindigit ::= "0" | "1" octdigit ::= "0"..."7" hexdigit ::= digit | "a"..."f" | "A"..."F" There is no limit for the length of integer literals apart from what can be stored in available memory. Underscores are ignored for determining the numeric value of the literal. They can be used to group digits for enhanced readability. One underscore can occur between digits, and after base specifiers like "0x". Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0. Some examples of integer literals: 7 2147483647 0o177 0b100110111 3 79228162514264337593543950336 0o377 0xdeadbeef 100_000_000_000 0b_1110_0101 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. a^Lambdas ******* lambda_expr ::= "lambda" [parameter_list] ":" expression lambda_expr_nocond ::= "lambda" [parameter_list] ":" expression_nocond Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression "lambda parameters: expression" yields a function object. The unnamed object behaves like a function object defined with: def (parameters): return expression See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements or annotations. a/List displays ************* A list display is a possibly empty series of expressions enclosed in square brackets: list_display ::= "[" [starred_list | comprehension] "]" A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma- separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a comprehension is supplied, the list is constructed from the elements resulting from the comprehension. uNaming and binding ****************** Binding of names ================ *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names =================== A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution ================================= **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features ================================= Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aThe "nonlocal" statement ************************ nonlocal_stmt ::= "nonlocal" identifier ("," identifier)* The "nonlocal" statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope. Names listed in a "nonlocal" statement, unlike those listed in a "global" statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously). Names listed in a "nonlocal" statement must not collide with pre- existing bindings in the local scope. See also: **PEP 3104** - Access to Names in Outer Scopes The specification for the "nonlocal" statement. uNumeric literals **************** There are three types of numeric literals: integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator ‘"-"‘ and the literal "1". uEmulating numeric types *********************** The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". u Objects, values and types ************************* *Objects* are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.) Every object has an identity, a type and a value. An object’s *identity* never changes once it has been created; you may think of it as the object’s address in memory. The ‘"is"’ operator compares the identity of two objects; the "id()" function returns an integer representing its identity. **CPython implementation detail:** For CPython, "id(x)" is the memory address where "x" is stored. An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The "type()" function returns an object’s type (which is an object itself). Like its identity, an object’s *type* is also unchangeable. [1] The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable. Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable. **CPython implementation detail:** CPython currently uses a reference- counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the "gc" module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (so you should always close files explicitly). Note that the use of the implementation’s tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a ‘"try"…"except"’ statement may keep objects alive. Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a "close()" method. Programs are strongly recommended to explicitly close such objects. The ‘"try"…"finally"’ statement and the ‘"with"’ statement provide convenient ways to do this. Some objects contain references to other objects; these are called *containers*. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after "a = 1; b = 1", "a" and "b" may or may not refer to the same object with the value one, depending on the implementation, but after "c = []; d = []", "c" and "d" are guaranteed to refer to two different, unique, newly created empty lists. (Note that "c = d = []" assigns the same object to both "c" and "d".) uOperator precedence ******************* The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left). Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section. +-------------------------------------------------+---------------------------------------+ | Operator | Description | +=================================================+=======================================+ | "lambda" | Lambda expression | +-------------------------------------------------+---------------------------------------+ | "if" – "else" | Conditional expression | +-------------------------------------------------+---------------------------------------+ | "or" | Boolean OR | +-------------------------------------------------+---------------------------------------+ | "and" | Boolean AND | +-------------------------------------------------+---------------------------------------+ | "not" "x" | Boolean NOT | +-------------------------------------------------+---------------------------------------+ | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership | | ">=", "!=", "==" | tests and identity tests | +-------------------------------------------------+---------------------------------------+ | "|" | Bitwise OR | +-------------------------------------------------+---------------------------------------+ | "^" | Bitwise XOR | +-------------------------------------------------+---------------------------------------+ | "&" | Bitwise AND | +-------------------------------------------------+---------------------------------------+ | "<<", ">>" | Shifts | +-------------------------------------------------+---------------------------------------+ | "+", "-" | Addition and subtraction | +-------------------------------------------------+---------------------------------------+ | "*", "@", "/", "//", "%" | Multiplication, matrix | | | multiplication, division, floor | | | division, remainder [5] | +-------------------------------------------------+---------------------------------------+ | "+x", "-x", "~x" | Positive, negative, bitwise NOT | +-------------------------------------------------+---------------------------------------+ | "**" | Exponentiation [6] | +-------------------------------------------------+---------------------------------------+ | "await" "x" | Await expression | +-------------------------------------------------+---------------------------------------+ | "x[index]", "x[index:index]", | Subscription, slicing, call, | | "x(arguments...)", "x.attribute" | attribute reference | +-------------------------------------------------+---------------------------------------+ | "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list | | value...}", "{expressions...}" | display, dictionary display, set | | | display | +-------------------------------------------------+---------------------------------------+ -[ Footnotes ]- [1] While "abs(x%y) < abs(y)" is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double- precision number, in order that "-1e-100 % 1e100" have the same sign as "1e100", the computed result is "-1e-100 + 1e100", which is numerically exactly equal to "1e100". The function "math.fmod()" returns a result whose sign matches the sign of the first argument instead, and so returns "-1e-100" in this case. Which approach is more appropriate depends on the application. [2] If x is very close to an exact integer multiple of y, it’s possible for "x//y" to be one larger than "(x-x%y)//y" due to rounding. In such cases, Python returns the latter result, in order to preserve that "divmod(x,y)[0] * y + x % y" be very close to "x". [3] The Unicode standard distinguishes between *code points* (e.g. U+0041) and *abstract characters* (e.g. “LATIN CAPITAL LETTER A”). While most abstract characters in Unicode are only represented using one code point, there is a number of abstract characters that can in addition be represented using a sequence of more than one code point. For example, the abstract character “LATIN CAPITAL LETTER C WITH CEDILLA” can be represented as a single *precomposed character* at code position U+00C7, or as a sequence of a *base character* at code position U+0043 (LATIN CAPITAL LETTER C), followed by a *combining character* at code position U+0327 (COMBINING CEDILLA). The comparison operators on strings compare at the level of Unicode code points. This may be counter-intuitive to humans. For example, ""\u00C7" == "\u0043\u0327"" is "False", even though both strings represent the same abstract character “LATIN CAPITAL LETTER C WITH CEDILLA”. To compare strings at the level of abstract characters (that is, in a way intuitive to humans), use "unicodedata.normalize()". [4] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the "is" operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [5] The "%" operator is also used for string formatting; the same precedence applies. [6] The power operator "**" binds less tightly than an arithmetic or bitwise unary operator on its right, that is, "2**-1" is "0.5". uwThe "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) aThe power operator ****************** The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is: power ::= (await_expr | primary) ["**" u_expr] Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): "-1**2" results in "-1". The power operator has the same semantics as the built-in "pow()" function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type, and the result is of that type. For int operands, the result has the same type as the operands unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, "10**2" returns "100", but "10**-2" returns "0.01". Raising "0.0" to a negative power results in a "ZeroDivisionError". Raising a negative number to a fractional power results in a "complex" number. (In earlier versions it raised a "ValueError".) ul The "raise" statement ********************* raise_stmt ::= "raise" [expression ["from" expression]] If no expressions are present, "raise" re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a "RuntimeError" exception is raised indicating that this is an error. Otherwise, "raise" evaluates the first expression as the exception object. It must be either a subclass or an instance of "BaseException". If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments. The *type* of the exception is the exception instance’s class, the *value* is the instance itself. A traceback object is normally created automatically when an exception is raised and attached to it as the "__traceback__" attribute, which is writable. You can create an exception and set your own traceback in one step using the "with_traceback()" exception method (which returns the same exception instance, with its traceback set to its argument), like so: raise Exception("foo occurred").with_traceback(tracebackobj) The "from" clause is used for exception chaining: if given, the second *expression* must be another exception class or instance, which will then be attached to the raised exception as the "__cause__" attribute (which is writable). If the raised exception is not handled, both exceptions will be printed: >>> try: ... print(1 / 0) ... except Exception as exc: ... raise RuntimeError("Something bad happened") from exc ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened A similar mechanism works implicitly if an exception is raised inside an exception handler or a "finally" clause: the previous exception is then attached as the new exception’s "__context__" attribute: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Exception chaining can be explicitly suppressed by specifying "None" in the "from" clause: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") from None ... Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement. Changed in version 3.3: "None" is now permitted as "Y" in "raise X from Y". New in version 3.3: The "__suppress_context__" attribute to suppress automatic display of the exception context. aThe "return" statement ********************** return_stmt ::= "return" [expression_list] "return" may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else "None" is substituted. "return" leaves the current function call with the expression list (or "None") as return value. When "return" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the function. In a generator function, the "return" statement indicates that the generator is done and will cause "StopIteration" to be raised. The returned value (if any) is used as an argument to construct "StopIteration" and becomes the "StopIteration.value" attribute. In an asynchronous generator function, an empty "return" statement indicates that the asynchronous generator is done and will cause "StopAsyncIteration" to be raised. A non-empty "return" statement is a syntax error in an asynchronous generator function. uEmulating container types ************************* The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. aShifting operations ******************* The shifting operations have lower priority than the arithmetic operations: shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr These operators accept integers as arguments. They shift the first argument to the left or right by the number of bits given by the second argument. A right shift by *n* bits is defined as floor division by "pow(2,n)". A left shift by *n* bits is defined as multiplication with "pow(2,n)". Note: In the current implementation, the right-hand operand is required to be at most "sys.maxsize". If the right-hand operand is larger than "sys.maxsize" an "OverflowError" exception is raised. aSlicings ******** A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or "del" statements. The syntax for a slicing: slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ] lower_bound ::= expression upper_bound ::= expression stride ::= expression There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice). The semantics for a slicing are as follows. The primary is indexed (using the same "__getitem__()" method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose "start", "stop" and "step" attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting "None" for missing expressions. u~Special Attributes ****************** The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the "dir()" built-in function. object.__dict__ A dictionary or other mapping object used to store an object’s (writable) attributes. instance.__class__ The class to which a class instance belongs. class.__bases__ The tuple of base classes of a class object. definition.__name__ The name of the class, function, method, descriptor, or generator instance. definition.__qualname__ The *qualified name* of the class, function, method, descriptor, or generator instance. New in version 3.3. class.__mro__ This attribute is a tuple of classes that are considered when looking for base classes during method resolution. class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in "__mro__". class.__subclasses__() Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example: >>> int.__subclasses__() [] -[ Footnotes ]- [1] Additional information on these special methods may be found in the Python Reference Manual (Basic customization). [2] As a consequence, the list "[1, 2]" is considered equal to "[1.0, 2.0]", and similarly for tuples. [3] They must have since the parser can’t tell the type of the operands. [4] Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase). [5] To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted. u.Special method names ******************** A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to *operator overloading*, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named "__getitem__()", and "x" is an instance of this class, then "x[i]" is roughly equivalent to "type(x).__getitem__(x, i)". Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically "AttributeError" or "TypeError"). Setting a special method to "None" indicates that the corresponding operation is not available. For example, if a class sets "__iter__()" to "None", the class is not iterable, so calling "iter()" on its instances will raise a "TypeError" (without falling back to "__getitem__()"). [2] When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the "NodeList" interface in the W3C’s Document Object Model.) Basic customization =================== object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. Customizing attribute access ============================ The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access ----------------------------------- For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ------------------------ The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors -------------------- In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ --------- *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* ~~~~~~~~~~~~~~~~~~~~~~~~~~ * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". Customizing class creation ========================== Whenever a class inherits from another class, *__init_subclass__* is called on that class. This way, it is possible to write classes which change the behavior of subclasses. This is closely related to class decorators, but where class decorators only affect the specific class they’re applied to, "__init_subclass__" solely applies to future subclasses of the class defining the method. classmethod object.__init_subclass__(cls) This method is called whenever the containing class is subclassed. *cls* is then the new subclass. If defined as a normal instance method, this method is implicitly converted to a class method. Keyword arguments which are given to a new class are passed to the parent’s class "__init_subclass__". For compatibility with other classes using "__init_subclass__", one should take out the needed keyword arguments and pass the others over to the base class, as in: class Philosopher: def __init_subclass__(cls, default_name, **kwargs): super().__init_subclass__(**kwargs) cls.default_name = default_name class AustralianPhilosopher(Philosopher, default_name="Bruce"): pass The default implementation "object.__init_subclass__" does nothing, but raises an error if it is called with any arguments. Note: The metaclass hint "metaclass" is consumed by the rest of the type machinery, and is never passed to "__init_subclass__" implementations. The actual metaclass (rather than the explicit hint) can be accessed as "type(cls)". New in version 3.6. Metaclasses ----------- By default, classes are constructed using "type()". The class body is executed in a new namespace and the class name is bound locally to the result of "type(name, bases, namespace)". The class creation process can be customized by passing the "metaclass" keyword argument in the class definition line, or by inheriting from an existing class that included such an argument. In the following example, both "MyClass" and "MySubclass" are instances of "Meta": class Meta(type): pass class MyClass(metaclass=Meta): pass class MySubclass(MyClass): pass Any other keyword arguments that are specified in the class definition are passed through to all metaclass operations described below. When a class definition is executed, the following steps occur: * the appropriate metaclass is determined * the class namespace is prepared * the class body is executed * the class object is created Determining the appropriate metaclass ------------------------------------- The appropriate metaclass for a class definition is determined as follows: * if no bases and no explicit metaclass are given, then "type()" is used * if an explicit metaclass is given and it is *not* an instance of "type()", then it is used directly as the metaclass * if an instance of "type()" is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used The most derived metaclass is selected from the explicitly specified metaclass (if any) and the metaclasses (i.e. "type(cls)") of all specified base classes. The most derived metaclass is one which is a subtype of *all* of these candidate metaclasses. If none of the candidate metaclasses meets that criterion, then the class definition will fail with "TypeError". Preparing the class namespace ----------------------------- Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a "__prepare__" attribute, it is called as "namespace = metaclass.__prepare__(name, bases, **kwds)" (where the additional keyword arguments, if any, come from the class definition). If the metaclass has no "__prepare__" attribute, then the class namespace is initialised as an empty ordered mapping. See also: **PEP 3115** - Metaclasses in Python 3000 Introduced the "__prepare__" namespace hook Executing the class body ------------------------ The class body is executed (approximately) as "exec(body, globals(), namespace)". The key difference from a normal call to "exec()" is that lexical scoping allows the class body (including any methods) to reference names from the current and outer scopes when the class definition occurs inside a function. However, even when the class definition occurs inside the function, methods defined inside the class still cannot see names defined at the class scope. Class variables must be accessed through the first parameter of instance or class methods, or through the implicit lexically scoped "__class__" reference described in the next section. Creating the class object ------------------------- Once the class namespace has been populated by executing the class body, the class object is created by calling "metaclass(name, bases, namespace, **kwds)" (the additional keywords passed here are the same as those passed to "__prepare__"). This class object is the one that will be referenced by the zero- argument form of "super()". "__class__" is an implicit closure reference created by the compiler if any methods in a class body refer to either "__class__" or "super". This allows the zero argument form of "super()" to correctly identify the class being defined based on lexical scoping, while the class or instance that was used to make the current call is identified based on the first argument passed to the method. **CPython implementation detail:** In CPython 3.6 and later, the "__class__" cell is passed to the metaclass as a "__classcell__" entry in the class namespace. If present, this must be propagated up to the "type.__new__" call in order for the class to be initialised correctly. Failing to do so will result in a "DeprecationWarning" in Python 3.6, and a "RuntimeError" in Python 3.8. When using the default metaclass "type", or any metaclass that ultimately calls "type.__new__", the following additional customisation steps are invoked after creating the class object: * first, "type.__new__" collects all of the descriptors in the class namespace that define a "__set_name__()" method; * second, all of these "__set_name__" methods are called with the class being defined and the assigned name of that particular descriptor; and * finally, the "__init_subclass__()" hook is called on the immediate parent of the new class in its method resolution order. After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. When a new class is created by "type.__new__", the object provided as the namespace parameter is copied to a new ordered mapping and the original object is discarded. The new copy is wrapped in a read-only proxy, which becomes the "__dict__" attribute of the class object. See also: **PEP 3135** - New super Describes the implicit "__class__" closure reference Uses for metaclasses -------------------- The potential uses for metaclasses are boundless. Some ideas that have been explored include enum, logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. Customizing instance and subclass checks ======================================== The following methods are used to override the default behavior of the "isinstance()" and "issubclass()" built-in functions. In particular, the metaclass "abc.ABCMeta" implements these methods in order to allow the addition of Abstract Base Classes (ABCs) as “virtual base classes” to any class or type (including built-in types), including other ABCs. class.__instancecheck__(self, instance) Return true if *instance* should be considered a (direct or indirect) instance of *class*. If defined, called to implement "isinstance(instance, class)". class.__subclasscheck__(self, subclass) Return true if *subclass* should be considered a (direct or indirect) subclass of *class*. If defined, called to implement "issubclass(subclass, class)". Note that these methods are looked up on the type (metaclass) of a class. They cannot be defined as class methods in the actual class. This is consistent with the lookup of special methods that are called on instances, only in this case the instance is itself a class. See also: **PEP 3119** - Introducing Abstract Base Classes Includes the specification for customizing "isinstance()" and "issubclass()" behavior through "__instancecheck__()" and "__subclasscheck__()", with motivation for this functionality in the context of adding Abstract Base Classes (see the "abc" module) to the language. Emulating callable objects ========================== object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". Emulating container types ========================= The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. Emulating numeric types ======================= The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". With Statement Context Managers =============================== A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Special method lookup ===================== For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary. That behaviour is the reason why the following code raises an exception: >>> class C: ... pass ... >>> c = C() >>> c.__len__ = lambda: 5 >>> len(c) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such as "__hash__()" and "__repr__()" that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself: >>> 1 .__hash__() == hash(1) True >>> int.__hash__() == hash(int) Traceback (most recent call last): File "", line 1, in TypeError: descriptor '__hash__' of 'int' object needs an argument Incorrectly attempting to invoke an unbound method of a class in this way is sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing the instance when looking up special methods: >>> type(1).__hash__(1) == hash(1) True >>> type(int).__hash__(int) == hash(int) True In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the "__getattribute__()" method even of the object’s metaclass: >>> class Meta(type): ... def __getattribute__(*args): ... print("Metaclass getattribute invoked") ... return type.__getattribute__(*args) ... >>> class C(object, metaclass=Meta): ... def __len__(self): ... return 10 ... def __getattribute__(*args): ... print("Class getattribute invoked") ... return object.__getattribute__(*args) ... >>> c = C() >>> c.__len__() # Explicit lookup via instance Class getattribute invoked 10 >>> type(c).__len__(c) # Explicit lookup via type Metaclass getattribute invoked 10 >>> len(c) # Implicit lookup 10 Bypassing the "__getattribute__()" machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class object itself in order to be consistently invoked by the interpreter). u=WString Methods ************** Strings implement all of the common sequence operations, along with the additional methods described below. Strings also support two styles of string formatting, one providing a large degree of flexibility and customization (see "str.format()", Format String Syntax and Custom String Formatting) and the other based on C "printf" style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle (printf-style String Formatting). The Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities (including regular expression support in the "re" module). str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. str.casefold() Return a casefolded copy of the string. Casefolded strings may be used for caseless matching. Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter "'ß'" is equivalent to ""ss"". Since it is already lowercase, "lower()" would do nothing to "'ß'"; "casefold()" converts it to ""ss"". The casefolding algorithm is described in section 3.13 of the Unicode Standard. New in version 3.3. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.encode(encoding="utf-8", errors="strict") Return an encoded version of the string as a bytes object. Default encoding is "'utf-8'". *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Error Handlers. For a list of possible encodings, see section Standard Encodings. Changed in version 3.1: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. str.expandtabs(tabsize=8) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. Note: When formatting a number ("int", "float", "complex", "decimal.Decimal" and subclasses) with the "n" type (ex: "'{:n}'.format(1234)"), the function temporarily sets the "LC_CTYPE" locale to the "LC_NUMERIC" locale to decode "decimal_point" and "thousands_sep" fields of "localeconv()" if they are non-ASCII or longer than 1 byte, and the "LC_NUMERIC" locale is different than the "LC_CTYPE" locale. This temporary change affects other threads. Changed in version 3.6.5: When formatting a number with the "n" type, the function sets temporarily the "LC_CTYPE" locale to the "LC_NUMERIC" locale in some cases. str.format_map(mapping) Similar to "str.format(**mapping)", except that "mapping" is used directly and not copied to a "dict". This is useful if for example "mapping" is a dict subclass: >>> class Default(dict): ... def __missing__(self, key): ... return key ... >>> '{name} was born in {country}'.format_map(Default(name='Guido')) 'Guido was born in country' New in version 3.2. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character "c" is alphanumeric if one of the following returns "True": "c.isalpha()", "c.isdecimal()", "c.isdigit()", or "c.isnumeric()". str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard. str.isdecimal() Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal. str.isidentifier() Return true if the string is a valid identifier according to the language definition, section Identifiers and keywords. Use "keyword.iskeyword()" to test for reserved identifiers such as "def" and "class". str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. str.isnumeric() Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric. str.isprintable() Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when "repr()" is invoked on a string. It has no bearing on the handling of strings written to "sys.stdout" or "sys.stderr".) str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as “Other” or “Separator” and those with bidirectional property being one of “WS”, “B”, or “S”. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. The lowercasing algorithm used is described in section 3.13 of the Unicode Standard. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' static str.maketrans(x[, y[, z]]) This static method returns a translation table usable for "str.translate()". If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or "None". Character keys will then be converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to "None" in the result. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. str.rsplit(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' str.split(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". For example: >>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', ''] If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example: >>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3'] str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless *keepends* is given and true. This method splits on the following line boundaries. In particular, the boundaries are a superset of *universal newlines*. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 3.2: "\v" and "\f" added to list of line boundaries. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' The outermost leading and trailing *chars* argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in *chars*. A similar action takes place on the trailing end. For example: >>> comment_string = '#....... Section 3.2.1 Issue #32 .......' >>> comment_string.strip('.#! ') 'Section 3.2.1 Issue #32' str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that "s.swapcase().swapcase() == s". str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. For example: >>> 'Hello world'.title() 'Hello World' The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." str.translate(table) Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via "__getitem__()", typically a *mapping* or *sequence*. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return "None", to delete the character from the return string; or raise a "LookupError" exception, to map the character to itself. You can use "str.maketrans()" to create a translation map from character-to-character mappings in different formats. See also the "codecs" module for a more flexible approach to custom character mappings. str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "s.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase). The uppercasing algorithm used is described in section 3.13 of the Unicode Standard. str.zfill(width) Return a copy of the string left filled with ASCII "'0'" digits to make a string of length *width*. A leading sign prefix ("'+'"/"'-'") is handled by inserting the padding *after* the sign character rather than before. The original string is returned if *width* is less than or equal to "len(s)". For example: >>> "42".zfill(5) '00042' >>> "-42".zfill(5) '-0042' uw String and Bytes literals ************************* String literals are described by the following lexical definitions: stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F" | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | stringescapeseq longstringitem ::= longstringchar | stringescapeseq shortstringchar ::= longstringchar ::= stringescapeseq ::= "\" bytesliteral ::= bytesprefix(shortbytes | longbytes) bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= longbyteschar ::= bytesescapeseq ::= "\" One syntactic restriction not indicated by these productions is that whitespace is not allowed between the "stringprefix" or "bytesprefix" and the rest of the literal. The source character set is defined by the encoding declaration; it is UTF-8 if no encoding declaration is given in the source file; see section Encoding declarations. In plain English: Both types of literals can be enclosed in matching single quotes ("'") or double quotes ("""). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as *triple-quoted strings*). The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. Bytes literals are always prefixed with "'b'" or "'B'"; they produce an instance of the "bytes" type instead of the "str" type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes. Both string and bytes literals may optionally be prefixed with a letter "'r'" or "'R'"; such strings are called *raw strings* and treat backslashes as literal characters. As a result, in string literals, "'\U'" and "'\u'" escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the "'ur'" syntax is not supported. New in version 3.3: The "'rb'" prefix of raw bytes literals has been added as a synonym of "'br'". New in version 3.3: Support for the unicode legacy literal ("u'value'") was reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases. See **PEP 414** for more information. A string literal with "'f'" or "'F'" in its prefix is a *formatted string literal*; see Formatted string literals. The "'f'" may be combined with "'r'", but not with "'b'" or "'u'", therefore raw formatted strings are possible, but formatted bytes literals are not. In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either "'" or """.) Unless an "'r'" or "'R'" prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\newline" | Backslash and newline ignored | | +-------------------+-----------------------------------+---------+ | "\\" | Backslash ("\") | | +-------------------+-----------------------------------+---------+ | "\'" | Single quote ("'") | | +-------------------+-----------------------------------+---------+ | "\"" | Double quote (""") | | +-------------------+-----------------------------------+---------+ | "\a" | ASCII Bell (BEL) | | +-------------------+-----------------------------------+---------+ | "\b" | ASCII Backspace (BS) | | +-------------------+-----------------------------------+---------+ | "\f" | ASCII Formfeed (FF) | | +-------------------+-----------------------------------+---------+ | "\n" | ASCII Linefeed (LF) | | +-------------------+-----------------------------------+---------+ | "\r" | ASCII Carriage Return (CR) | | +-------------------+-----------------------------------+---------+ | "\t" | ASCII Horizontal Tab (TAB) | | +-------------------+-----------------------------------+---------+ | "\v" | ASCII Vertical Tab (VT) | | +-------------------+-----------------------------------+---------+ | "\ooo" | Character with octal value *ooo* | (1,3) | +-------------------+-----------------------------------+---------+ | "\xhh" | Character with hex value *hh* | (2,3) | +-------------------+-----------------------------------+---------+ Escape sequences only recognized in string literals are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\N{name}" | Character named *name* in the | (4) | | | Unicode database | | +-------------------+-----------------------------------+---------+ | "\uxxxx" | Character with 16-bit hex value | (5) | | | *xxxx* | | +-------------------+-----------------------------------+---------+ | "\Uxxxxxxxx" | Character with 32-bit hex value | (6) | | | *xxxxxxxx* | | +-------------------+-----------------------------------+---------+ Notes: 1. As in Standard C, up to three octal digits are accepted. 2. Unlike in Standard C, exactly two hex digits are required. 3. In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value. 4. Changed in version 3.3: Support for name aliases [1] has been added. 5. Exactly four hex digits are required. 6. Any Unicode character can be encoded this way. Exactly eight hex digits are required. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., *the backslash is left in the result*. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences only recognized in string literals fall into the category of unrecognized escapes for bytes literals. Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning. In some future version of Python they will be a SyntaxError. Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, "r"\""" is a valid string literal consisting of two characters: a backslash and a double quote; "r"\"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw literal cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, *not* as a line continuation. uMSubscriptions ************* A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a "__getitem__()" method. For built-in objects, there are two types of objects that support subscription: If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression list must evaluate to an integer or a slice (as discussed in the following section). The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a "__getitem__()" method that interprets negative indices by adding the length of the sequence to the index (so that "x[-1]" selects the last item of "x"). The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing occurs in the object’s "__getitem__()" method, subclasses overriding this method will need to explicitly add that support. A string’s items are characters. A character is not a separate data type but a string of exactly one character. axTruth Value Testing ******************* Any object can be tested for truth value, for use in an "if" or "while" condition or as operand of the Boolean operations below. By default, an object is considered true unless its class defines either a "__bool__()" method that returns "False" or a "__len__()" method that returns zero, when called with the object. [1] Here are most of the built-in objects considered false: * constants defined to be false: "None" and "False". * zero of any numeric type: "0", "0.0", "0j", "Decimal(0)", "Fraction(0, 1)" * empty sequences and collections: "''", "()", "[]", "{}", "set()", "range(0)" Operations and built-in functions that have a Boolean result always return "0" or "False" for false and "1" or "True" for true, unless otherwise stated. (Important exception: the Boolean operations "or" and "and" always return one of their operands.) u=The "try" statement ******************* The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. uThe standard type hierarchy *************************** Below is a list of the types that are built into Python. Extension modules (written in C, Java, or other languages, depending on the implementation) can define additional types. Future versions of Python may add types to the type hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.), although such additions will often be provided via the standard library instead. Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future. None This type has a single value. There is a single object with this value. This object is accessed through the built-in name "None". It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false. NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name "NotImplemented". Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. See Implementing the arithmetic operations for more details. Ellipsis This type has a single value. There is a single object with this value. This object is accessed through the literal "..." or the built-in name "Ellipsis". Its truth value is true. "numbers.Number" These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers. Python distinguishes between integers, floating point numbers, and complex numbers: "numbers.Integral" These represent elements from the mathematical set of integers (positive and negative). There are two types of integers: Integers ("int") These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2’s complement which gives the illusion of an infinite string of sign bits extending to the left. Booleans ("bool") These represent the truth values False and True. The two objects representing the values "False" and "True" are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ""False"" or ""True"" are returned, respectively. The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers. "numbers.Real" ("float") These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single- precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers. "numbers.Complex" ("complex") These represent complex numbers as a pair of machine-level double precision floating point numbers. The same caveats apply as for floating point numbers. The real and imaginary parts of a complex number "z" can be retrieved through the read-only attributes "z.real" and "z.imag". Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function "len()" returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* is selected by "a[i]". Sequences also support slicing: "a[i:j]" selects all items with index *k* such that *i* "<=" *k* "<" *j*. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support “extended slicing” with a third “step” parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*. Sequences are distinguished according to their mutability: Immutable sequences An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.) The following types are immutable sequences: Strings A string is a sequence of values that represent Unicode code points. All the code points in the range "U+0000 - U+10FFFF" can be represented in a string. Python doesn’t have a "char" type; instead, every code point in the string is represented as a string object with length "1". The built-in function "ord()" converts a code point from its string form to an integer in the range "0 - 10FFFF"; "chr()" converts an integer in the range "0 - 10FFFF" to the corresponding length "1" string object. "str.encode()" can be used to convert a "str" to "bytes" using the given text encoding, and "bytes.decode()" can be used to achieve the opposite. Tuples The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses. Bytes A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256. Bytes literals (like "b'abc'") and the built-in "bytes()" constructor can be used to create bytes objects. Also, bytes objects can be decoded to strings via the "decode()" method. Mutable sequences Mutable sequences can be changed after they are created. The subscription and slicing notations can be used as the target of assignment and "del" (delete) statements. There are currently two intrinsic mutable sequence types: Lists The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.) Byte Arrays A bytearray object is a mutable array. They are created by the built-in "bytearray()" constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable "bytes" objects. The extension module "array" provides an additional example of a mutable sequence type, as does the "collections" module. Set types These represent unordered, finite sets of unique, immutable objects. As such, they cannot be indexed by any subscript. However, they can be iterated over, and the built-in function "len()" returns the number of items in a set. Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. For set elements, the same immutability rules apply as for dictionary keys. Note that numeric types obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0"), only one of them can be contained in a set. There are currently two intrinsic set types: Sets These represent a mutable set. They are created by the built-in "set()" constructor and can be modified afterwards by several methods, such as "add()". Frozen sets These represent an immutable set. They are created by the built-in "frozenset()" constructor. As a frozenset is immutable and *hashable*, it can be used again as an element of another set, or as a dictionary key. Mappings These represent finite sets of objects indexed by arbitrary index sets. The subscript notation "a[k]" selects the item indexed by "k" from the mapping "a"; this can be used in expressions and as the target of assignments or "del" statements. The built-in function "len()" returns the number of items in a mapping. There is currently a single intrinsic mapping type: Dictionaries These represent finite sets of objects indexed by nearly arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key’s hash value to remain constant. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. Dictionaries are mutable; they can be created by the "{...}" notation (see section Dictionary displays). The extension modules "dbm.ndbm" and "dbm.gnu" provide additional examples of mapping types, as does the "collections" module. Callable types These are the types to which the function call operation (see section Calls) can be applied: User-defined functions A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function’s formal parameter list. Special attributes: +---------------------------+---------------------------------+-------------+ | Attribute | Meaning | | +===========================+=================================+=============+ | "__doc__" | The function’s documentation | Writable | | | string, or "None" if | | | | unavailable; not inherited by | | | | subclasses | | +---------------------------+---------------------------------+-------------+ | "__name__" | The function’s name | Writable | +---------------------------+---------------------------------+-------------+ | "__qualname__" | The function’s *qualified name* | Writable | | | New in version 3.3. | | +---------------------------+---------------------------------+-------------+ | "__module__" | The name of the module the | Writable | | | function was defined in, or | | | | "None" if unavailable. | | +---------------------------+---------------------------------+-------------+ | "__defaults__" | A tuple containing default | Writable | | | argument values for those | | | | arguments that have defaults, | | | | or "None" if no arguments have | | | | a default value | | +---------------------------+---------------------------------+-------------+ | "__code__" | The code object representing | Writable | | | the compiled function body. | | +---------------------------+---------------------------------+-------------+ | "__globals__" | A reference to the dictionary | Read-only | | | that holds the function’s | | | | global variables — the global | | | | namespace of the module in | | | | which the function was defined. | | +---------------------------+---------------------------------+-------------+ | "__dict__" | The namespace supporting | Writable | | | arbitrary function attributes. | | +---------------------------+---------------------------------+-------------+ | "__closure__" | "None" or a tuple of cells that | Read-only | | | contain bindings for the | | | | function’s free variables. | | +---------------------------+---------------------------------+-------------+ | "__annotations__" | A dict containing annotations | Writable | | | of parameters. The keys of the | | | | dict are the parameter names, | | | | and "'return'" for the return | | | | annotation, if provided. | | +---------------------------+---------------------------------+-------------+ | "__kwdefaults__" | A dict containing defaults for | Writable | | | keyword-only parameters. | | +---------------------------+---------------------------------+-------------+ Most of the attributes labelled “Writable” check the type of the assigned value. Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. *Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.* Additional information about a function’s definition can be retrieved from its code object; see the description of internal types below. Instance methods An instance method object combines a class, a class instance and any callable object (normally a user-defined function). Special read-only attributes: "__self__" is the class instance object, "__func__" is the function object; "__doc__" is the method’s documentation (same as "__func__.__doc__"); "__name__" is the method name (same as "__func__.__name__"); "__module__" is the name of the module the method was defined in, or "None" if unavailable. Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object. User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object. When an instance method object is created by retrieving a user- defined function object from a class via one of its instances, its "__self__" attribute is the instance, and the method object is said to be bound. The new method’s "__func__" attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the "__func__" attribute of the new instance is not the original method object but its "__func__" attribute. When an instance method object is created by retrieving a class method object from a class or instance, its "__self__" attribute is the class itself, and its "__func__" attribute is the function object underlying the class method. When an instance method object is called, the underlying function ("__func__") is called, inserting the class instance ("__self__") in front of the argument list. For instance, when "C" is a class which contains a definition for a function "f()", and "x" is an instance of "C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)". When an instance method object is derived from a class method object, the “class instance” stored in "__self__" will actually be the class itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to calling "f(C,1)" where "f" is the underlying function. Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non- callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this *only* happens when the function is an attribute of the class. Generator functions A function or method which uses the "yield" statement (see section The yield statement) is called a *generator function*. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s "iterator.__next__()" method will cause the function to execute until it provides a value using the "yield" statement. When the function executes a "return" statement or falls off the end, a "StopIteration" exception is raised and the iterator will have reached the end of the set of values to be returned. Coroutine functions A function or method which is defined using "async def" is called a *coroutine function*. Such a function, when called, returns a *coroutine* object. It may contain "await" expressions, as well as "async with" and "async for" statements. See also the Coroutine Objects section. Asynchronous generator functions A function or method which is defined using "async def" and which uses the "yield" statement is called a *asynchronous generator function*. Such a function, when called, returns an asynchronous iterator object which can be used in an "async for" statement to execute the body of the function. Calling the asynchronous iterator’s "aiterator.__anext__()" method will return an *awaitable* which when awaited will execute until it provides a value using the "yield" expression. When the function executes an empty "return" statement or falls off the end, a "StopAsyncIteration" exception is raised and the asynchronous iterator will have reached the end of the set of values to be yielded. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are "len()" and "math.sin()" ("math" is a standard built-in module). The number and type of the arguments are determined by the C function. Special read- only attributes: "__doc__" is the function’s documentation string, or "None" if unavailable; "__name__" is the function’s name; "__self__" is set to "None" (but see the next item); "__module__" is the name of the module the function was defined in or "None" if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is "alist.append()", assuming *alist* is a list object. In this case, the special read-only attribute "__self__" is set to the object denoted by *alist*. Classes Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override "__new__()". The arguments of the call are passed to "__new__()" and, in the typical case, to "__init__()" to initialize the new instance. Class Instances Instances of arbitrary classes can be made callable by defining a "__call__()" method in their class. Modules Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the "import" statement (see "import"), or by calling functions such as "importlib.import_module()" and built-in "__import__()". A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the "__globals__" attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done). Attribute assignment updates the module’s namespace dictionary, e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1". Predefined (writable) attributes: "__name__" is the module’s name; "__doc__" is the module’s documentation string, or "None" if unavailable; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during module body execution; "__file__" is the pathname of the file from which the module was loaded, if it was loaded from a file. The "__file__" attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file. Special read-only attribute: "__dict__" is the module’s namespace as a dictionary object. **CPython implementation detail:** Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly. Custom classes Custom class types are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., "C.x" is translated to "C.__dict__["x"]" (although there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes. This search of the base classes uses the C3 method resolution order which behaves correctly even in the presence of ‘diamond’ inheritance structures where there are multiple inheritance paths leading back to a common ancestor. Additional details on the C3 MRO used by Python can be found in the documentation accompanying the 2.3 release at https://www.python.org/download/releases/2.3/mro/. When a class attribute reference (for class "C", say) would yield a class method object, it is transformed into an instance method object whose "__self__" attribute is "C". When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section Implementing Descriptors for another way in which attributes retrieved from a class may differ from those actually contained in its "__dict__". Class attribute assignments update the class’s dictionary, never the dictionary of a base class. A class object can be called (see above) to yield a class instance (see below). Special attributes: "__name__" is the class name; "__module__" is the module name in which the class was defined; "__dict__" is the dictionary containing the class’s namespace; "__bases__" is a tuple containing the base classes, in the order of their occurrence in the base class list; "__doc__" is the class’s documentation string, or "None" if undefined; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during class body execution. Class instances A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object, it is transformed into an instance method object whose "__self__" attribute is the instance. Static method and class method objects are also transformed; see above under “Classes”. See section Implementing Descriptors for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class’s "__dict__". If no class attribute is found, and the object’s class has a "__getattr__()" method, that is called to satisfy the lookup. Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. If the class has a "__setattr__()" or "__delattr__()" method, this is called instead of updating the instance dictionary directly. Class instances can pretend to be numbers, sequences, or mappings if they have methods with certain special names. See section Special method names. Special attributes: "__dict__" is the attribute dictionary; "__class__" is the instance’s class. I/O objects (also known as file objects) A *file object* represents an open file. Various shortcuts are available to create file objects: the "open()" built-in function, and also "os.popen()", "os.fdopen()", and the "makefile()" method of socket objects (and perhaps by other functions or methods provided by extension modules). The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized to file objects corresponding to the interpreter’s standard input, output and error streams; they are all open in text mode and therefore follow the interface defined by the "io.TextIOBase" abstract class. Internal types A few types used internally by the interpreter are exposed to the user. Their definitions may change with future versions of the interpreter, but they are mentioned here for completeness. Code objects Code objects represent *byte-compiled* executable Python code, or *bytecode*. The difference between a code object and a function object is that the function object contains an explicit reference to the function’s globals (the module in which it was defined), while a code object contains no context; also the default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time). Unlike function objects, code objects are immutable and contain no references (directly or indirectly) to mutable objects. Special read-only attributes: "co_name" gives the function name; "co_argcount" is the number of positional arguments (including arguments with default values); "co_nlocals" is the number of local variables used by the function (including arguments); "co_varnames" is a tuple containing the names of the local variables (starting with the argument names); "co_cellvars" is a tuple containing the names of local variables that are referenced by nested functions; "co_freevars" is a tuple containing the names of free variables; "co_code" is a string representing the sequence of bytecode instructions; "co_consts" is a tuple containing the literals used by the bytecode; "co_names" is a tuple containing the names used by the bytecode; "co_filename" is the filename from which the code was compiled; "co_firstlineno" is the first line number of the function; "co_lnotab" is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); "co_stacksize" is the required stack size (including local variables); "co_flags" is an integer encoding a number of flags for the interpreter. The following flag bits are defined for "co_flags": bit "0x04" is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit "0x08" is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; bit "0x20" is set if the function is a generator. Future feature declarations ("from __future__ import division") also use bits in "co_flags" to indicate whether a code object was compiled with a particular feature enabled: bit "0x2000" is set if the function was compiled with future division enabled; bits "0x10" and "0x1000" were used in earlier versions of Python. Other bits in "co_flags" are reserved for internal use. If a code object represents a function, the first item in "co_consts" is the documentation string of the function, or "None" if undefined. Frame objects Frame objects represent execution frames. They may occur in traceback objects (see below). Special read-only attributes: "f_back" is to the previous stack frame (towards the caller), or "None" if this is the bottom stack frame; "f_code" is the code object being executed in this frame; "f_locals" is the dictionary used to look up local variables; "f_globals" is used for global variables; "f_builtins" is used for built-in (intrinsic) names; "f_lasti" gives the precise instruction (this is an index into the bytecode string of the code object). Special writable attributes: "f_trace", if not "None", is a function called at the start of each source code line (this is used by the debugger); "f_lineno" is the current line number of the frame — writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno. Frame objects support one method: frame.clear() This method clears all references to local variables held by the frame. Also, if the frame belonged to a generator, the generator is finalized. This helps break reference cycles involving frame objects (for example when catching an exception and storing its traceback for later use). "RuntimeError" is raised if the frame is currently executing. New in version 3.4. Traceback objects Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as the third item of the tuple returned by "sys.exc_info()". When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as "sys.last_traceback". Special read-only attributes: "tb_next" is the next level in the stack trace (towards the frame where the exception occurred), or "None" if there is no next level; "tb_frame" points to the execution frame of the current level; "tb_lineno" gives the line number where the exception occurred; "tb_lasti" indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a "try" statement with no matching except clause or with a finally clause. Slice objects Slice objects are used to represent slices for "__getitem__()" methods. They are also created by the built-in "slice()" function. Special read-only attributes: "start" is the lower bound; "stop" is the upper bound; "step" is the step value; each is "None" if omitted. These attributes can have any type. Slice objects support one method: slice.indices(self, length) This method takes a single integer argument *length* and computes information about the slice that the slice object would describe if applied to a sequence of *length* items. It returns a tuple of three integers; respectively these are the *start* and *stop* indices and the *step* or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices. Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in "staticmethod()" constructor. Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under “User-defined methods”. Class method objects are created by the built-in "classmethod()" constructor. aFunctions ********* Function objects are created by function definitions. The only operation on a function object is to call it: "func(argument-list)". There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types. See Function definitions for more information. u8$Mapping Types — "dict" ********************** A *mapping* object maps *hashable* values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the *dictionary*. (For other containers see the built- in "list", "set", and "tuple" classes, and the "collections" module.) A dictionary’s keys are *almost* arbitrary values. Values that are not *hashable*, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) Dictionaries can be created by placing a comma-separated list of "key: value" pairs within braces, for example: "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}", or by the "dict" constructor. class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an *iterable* object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary. If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument. To illustrate, the following examples all return a dictionary equal to "{"one": 1, "two": 2, "three": 3}": >>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. These are the operations that dictionaries support (and therefore, custom mapping types should support too): len(d) Return the number of items in the dictionary *d*. d[key] Return the item of *d* with key *key*. Raises a "KeyError" if *key* is not in the map. If a subclass of dict defines a method "__missing__()" and *key* is not present, the "d[key]" operation calls that method with the key *key* as argument. The "d[key]" operation then returns or raises whatever is returned or raised by the "__missing__(key)" call. No other operations or methods invoke "__missing__()". If "__missing__()" is not defined, "KeyError" is raised. "__missing__()" must be a method; it cannot be an instance variable: >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 The example above shows part of the implementation of "collections.Counter". A different "__missing__" method is used by "collections.defaultdict". d[key] = value Set "d[key]" to *value*. del d[key] Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not in the map. key in d Return "True" if *d* has a key *key*, else "False". key not in d Equivalent to "not key in d". iter(d) Return an iterator over the keys of the dictionary. This is a shortcut for "iter(d.keys())". clear() Remove all items from the dictionary. copy() Return a shallow copy of the dictionary. classmethod fromkeys(seq[, value]) Create a new dictionary with keys from *seq* and values set to *value*. "fromkeys()" is a class method that returns a new dictionary. *value* defaults to "None". get(key[, default]) Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to "None", so that this method never raises a "KeyError". items() Return a new view of the dictionary’s items ("(key, value)" pairs). See the documentation of view objects. keys() Return a new view of the dictionary’s keys. See the documentation of view objects. pop(key[, default]) If *key* is in the dictionary, remove it and return its value, else return *default*. If *default* is not given and *key* is not in the dictionary, a "KeyError" is raised. popitem() Remove and return an arbitrary "(key, value)" pair from the dictionary. "popitem()" is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling "popitem()" raises a "KeyError". setdefault(key[, default]) If *key* is in the dictionary, return its value. If not, insert *key* with a value of *default* and return *default*. *default* defaults to "None". update([other]) Update the dictionary with the key/value pairs from *other*, overwriting existing keys. Return "None". "update()" accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: "d.update(red=1, blue=2)". values() Return a new view of the dictionary’s values. See the documentation of view objects. Dictionaries compare equal if and only if they have the same "(key, value)" pairs. Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise "TypeError". See also: "types.MappingProxyType" can be used to create a read-only view of a "dict". Dictionary view objects ======================= The objects returned by "dict.keys()", "dict.values()" and "dict.items()" are *view objects*. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data, and support membership tests: len(dictview) Return the number of entries in the dictionary. iter(dictview) Return an iterator over the keys, values or items (represented as tuples of "(key, value)") in the dictionary. Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.items()]". Iterating views while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. x in dictview Return "True" if *x* is in the underlying dictionary’s keys, values or items (in the latter case, *x* should be a "(key, value)" tuple). Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that "(key, value)" pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) For set-like views, all of the operations defined for the abstract base class "collections.abc.Set" are available (for example, "==", "<", or "^"). An example of dictionary view usage: >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.keys() >>> values = dishes.values() >>> # iteration >>> n = 0 >>> for val in values: ... n += val >>> print(n) 504 >>> # keys and values are iterated over in the same order >>> list(keys) ['eggs', 'bacon', 'sausage', 'spam'] >>> list(values) [2, 1, 1, 500] >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> del dishes['sausage'] >>> list(keys) ['spam', 'bacon'] >>> # set operations >>> keys & {'eggs', 'bacon', 'salad'} {'bacon'} >>> keys ^ {'sausage', 'juice'} {'juice', 'sausage', 'bacon', 'spam'} aMethods ******* Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as "append()" on lists) and class instance methods. Built-in methods are described with the types that support them. If you access a method (a function defined in a class namespace) through an instance, you get a special object: a *bound method* (also called *instance method*) object. When called, it will add the "self" argument to the argument list. Bound methods have two special read- only attributes: "m.__self__" is the object on which the method operates, and "m.__func__" is the function implementing the method. Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)". Like function objects, bound method objects support getting arbitrary attributes. However, since method attributes are actually stored on the underlying function object ("meth.__func__"), setting method attributes on bound methods is disallowed. Attempting to set an attribute on a method results in an "AttributeError" being raised. In order to set a method attribute, you need to explicitly set it on the underlying function object: >>> class C: ... def method(self): ... pass ... >>> c = C() >>> c.method.whoami = 'my name is method' # can't set on the method Traceback (most recent call last): File "", line 1, in AttributeError: 'method' object has no attribute 'whoami' >>> c.method.__func__.whoami = 'my name is method' >>> c.method.whoami 'my name is method' See The standard type hierarchy for more information. u$Modules ******* The only special operation on a module is attribute access: "m.name", where *m* is a module and *name* accesses a name defined in *m*’s symbol table. Module attributes can be assigned to. (Note that the "import" statement is not, strictly speaking, an operation on a module object; "import foo" does not require a module object named *foo* to exist, rather it requires an (external) *definition* for a module named *foo* somewhere.) A special attribute of every module is "__dict__". This is the dictionary containing the module’s symbol table. Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the "__dict__" attribute is not possible (you can write "m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can’t write "m.__dict__ = {}"). Modifying "__dict__" directly is not recommended. Modules built into the interpreter are written like this: "". If loaded from a file, they are written as "". uYSequence Types — "list", "tuple", "range" ***************************************** There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections. Common Sequence Operations ========================== The operations in the following table are supported by most sequence types, both mutable and immutable. The "collections.abc.Sequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. This table lists the sequence operations sorted in ascending priority. In the table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are integers and *x* is an arbitrary object that meets any type and value restrictions imposed by *s*. The "in" and "not in" operations have the same priorities as the comparison operations. The "+" (concatenation) and "*" (repetition) operations have the same priority as the corresponding numeric operations. [3] +----------------------------+----------------------------------+------------+ | Operation | Result | Notes | +============================+==================================+============+ | "x in s" | "True" if an item of *s* is | (1) | | | equal to *x*, else "False" | | +----------------------------+----------------------------------+------------+ | "x not in s" | "False" if an item of *s* is | (1) | | | equal to *x*, else "True" | | +----------------------------+----------------------------------+------------+ | "s + t" | the concatenation of *s* and *t* | (6)(7) | +----------------------------+----------------------------------+------------+ | "s * n" or "n * s" | equivalent to adding *s* to | (2)(7) | | | itself *n* times | | +----------------------------+----------------------------------+------------+ | "s[i]" | *i*th item of *s*, origin 0 | (3) | +----------------------------+----------------------------------+------------+ | "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) | +----------------------------+----------------------------------+------------+ | "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) | | | with step *k* | | +----------------------------+----------------------------------+------------+ | "len(s)" | length of *s* | | +----------------------------+----------------------------------+------------+ | "min(s)" | smallest item of *s* | | +----------------------------+----------------------------------+------------+ | "max(s)" | largest item of *s* | | +----------------------------+----------------------------------+------------+ | "s.index(x[, i[, j]])" | index of the first occurrence of | (8) | | | *x* in *s* (at or after index | | | | *i* and before index *j*) | | +----------------------------+----------------------------------+------------+ | "s.count(x)" | total number of occurrences of | | | | *x* in *s* | | +----------------------------+----------------------------------+------------+ Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.) Notes: 1. While the "in" and "not in" operations are used only for simple containment testing in the general case, some specialised sequences (such as "str", "bytes" and "bytearray") also use them for subsequence testing: >>> "gg" in "eggs" True 2. Values of *n* less than "0" are treated as "0" (which yields an empty sequence of the same type as *s*). Note that items in the sequence *s* are not copied; they are referenced multiple times. This often haunts new Python programmers; consider: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] What has happened is that "[[]]" is a one-element list containing an empty list, so all three elements of "[[]] * 3" are references to this single empty list. Modifying any of the elements of "lists" modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]] Further explanation is available in the FAQ entry How do I create a multidimensional list?. 3. If *i* or *j* is negative, the index is relative to the end of sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But note that "-0" is still "0". 4. The slice of *s* from *i* to *j* is defined as the sequence of items with index *k* such that "i <= k < j". If *i* or *j* is greater than "len(s)", use "len(s)". If *i* is omitted or "None", use "0". If *j* is omitted or "None", use "len(s)". If *i* is greater than or equal to *j*, the slice is empty. 5. The slice of *s* from *i* to *j* with step *k* is defined as the sequence of items with index "x = i + n*k" such that "0 <= n < (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k", "i+3*k" and so on, stopping when *j* is reached (but never including *j*). When *k* is positive, *i* and *j* are reduced to "len(s)" if they are greater. When *k* is negative, *i* and *j* are reduced to "len(s) - 1" if they are greater. If *i* or *j* are omitted or "None", they become “end” values (which end depends on the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it is treated like "1". 6. Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below: * if concatenating "str" objects, you can build a list and use "str.join()" at the end or else write to an "io.StringIO" instance and retrieve its value when complete * if concatenating "bytes" objects, you can similarly use "bytes.join()" or "io.BytesIO", or you can do in-place concatenation with a "bytearray" object. "bytearray" objects are mutable and have an efficient overallocation mechanism * if concatenating "tuple" objects, extend a "list" instead * for other types, investigate the relevant class documentation 7. Some sequence types (such as "range") only support item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition. 8. "index" raises "ValueError" when *x* is not found in *s*. Not all implementations support passing the additional arguments *i* and *j*. These arguments allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using "s[i:j].index(x)", only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice. Immutable Sequence Types ======================== The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the "hash()" built-in. This support allows immutable sequences, such as "tuple" instances, to be used as "dict" keys and stored in "set" and "frozenset" instances. Attempting to hash an immutable sequence that contains unhashable values will result in "TypeError". Mutable Sequence Types ====================== The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. Lists ===== Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application). class list([iterable]) Lists may be constructed in several ways: * Using a pair of square brackets to denote the empty list: "[]" * Using square brackets, separating items with commas: "[a]", "[a, b, c]" * Using a list comprehension: "[x for x in iterable]" * Using the type constructor: "list()" or "list(iterable)" The constructor builds a list whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a list, a copy is made and returned, similar to "iterable[:]". For example, "list('abc')" returns "['a', 'b', 'c']" and "list( (1, 2, 3) )" returns "[1, 2, 3]". If no argument is given, the constructor creates a new empty list, "[]". Many other operations also produce lists, including the "sorted()" built-in. Lists implement all of the common and mutable sequence operations. Lists also provide the following additional method: sort(*, key=None, reverse=False) This method sorts the list in place, using only "<" comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state). "sort()" accepts two arguments that can only be passed by keyword (keyword-only arguments): *key* specifies a function of one argument that is used to extract a comparison key from each list element (for example, "key=str.lower"). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of "None" means that list items are sorted directly without calculating a separate key value. The "functools.cmp_to_key()" utility is available to convert a 2.x style *cmp* function to a *key* function. *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use "sorted()" to explicitly request a new sorted list instance). The "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. Tuples ====== Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the "enumerate()" built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a "set" or "dict" instance). class tuple([iterable]) Tuples may be constructed in a number of ways: * Using a pair of parentheses to denote the empty tuple: "()" * Using a trailing comma for a singleton tuple: "a," or "(a,)" * Separating items with commas: "a, b, c" or "(a, b, c)" * Using the "tuple()" built-in: "tuple()" or "tuple(iterable)" The constructor builds a tuple whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a tuple, it is returned unchanged. For example, "tuple('abc')" returns "('a', 'b', 'c')" and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument is given, the constructor creates a new empty tuple, "()". Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, "f(a, b, c)" is a function call with three arguments, while "f((a, b, c))" is a function call with a 3-tuple as the sole argument. Tuples implement all of the common sequence operations. For heterogeneous collections of data where access by name is clearer than access by index, "collections.namedtuple()" may be a more appropriate choice than a simple tuple object. Ranges ====== The "range" type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in "for" loops. class range(stop) class range(start, stop[, step]) The arguments to the range constructor must be integers (either built-in "int" or any object that implements the "__index__" special method). If the *step* argument is omitted, it defaults to "1". If the *start* argument is omitted, it defaults to "0". If *step* is zero, "ValueError" is raised. For a positive *step*, the contents of a range "r" are determined by the formula "r[i] = start + step*i" where "i >= 0" and "r[i] < stop". For a negative *step*, the contents of the range are still determined by the formula "r[i] = start + step*i", but the constraints are "i >= 0" and "r[i] > stop". A range object will be empty if "r[0]" does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices. Ranges containing absolute values larger than "sys.maxsize" are permitted but some features (such as "len()") may raise "OverflowError". Range examples: >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1, 11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] >>> list(range(0, 10, 3)) [0, 3, 6, 9] >>> list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> list(range(0)) [] >>> list(range(1, 0)) [] Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern). start The value of the *start* parameter (or "0" if the parameter was not supplied) stop The value of the *stop* parameter step The value of the *step* parameter (or "1" if the parameter was not supplied) The advantage of the "range" type over a regular "list" or "tuple" is that a "range" object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the "start", "stop" and "step" values, calculating individual items and subranges as needed). Range objects implement the "collections.abc.Sequence" ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range): >>> r = range(0, 20, 2) >>> r range(0, 20, 2) >>> 11 in r False >>> 10 in r True >>> r.index(10) 5 >>> r[5] 10 >>> r[:5] range(0, 10, 2) >>> r[-1] 18 Testing range objects for equality with "==" and "!=" compares them as sequences. That is, two range objects are considered equal if they represent the same sequence of values. (Note that two range objects that compare equal might have different "start", "stop" and "step" attributes, for example "range(0) == range(2, 1, 3)" or "range(0, 3, 2) == range(0, 4, 2)".) Changed in version 3.2: Implement the Sequence ABC. Support slicing and negative indices. Test "int" objects for membership in constant time instead of iterating through all items. Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define (instead of comparing based on object identity). New in version 3.3: The "start", "stop" and "step" attributes. See also: * The linspace recipe shows how to implement a lazy version of range suitable for floating point applications. usMutable Sequence Types ********************** The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. a~Unary arithmetic and bitwise operations *************************************** All unary arithmetic and bitwise operations have the same priority: u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr The unary "-" (minus) operator yields the negation of its numeric argument. The unary "+" (plus) operator yields its numeric argument unchanged. The unary "~" (invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of "x" is defined as "-(x+1)". It only applies to integral numbers. In all three cases, if the argument does not have the proper type, a "TypeError" exception is raised. uThe "while" statement ********************* The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. u& The "with" statement ******************** The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. a,The "yield" statement ********************* yield_stmt ::= yield_expression A "yield" statement is semantically equivalent to a yield expression. The yield statement can be used to omit the parentheses that would otherwise be required in the equivalent yield expression statement. For example, the yield statements yield yield from are equivalent to the yield expression statements (yield ) (yield from ) Yield expressions and statements are only used when defining a *generator* function, and are only used in the body of the generator function. Using yield in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. For full details of "yield" semantics, refer to the Yield expressions section. )MassertZ assignmentzatom-identifiersz atom-literalszattribute-accesszattribute-referencesZ augassignZbinaryZbitwisezbltin-code-objectszbltin-ellipsis-objectzbltin-null-objectzbltin-type-objectsZbooleansbreakzcallable-typesZcallsclassZ comparisonsZcompoundzcontext-managerscontinueZ conversionsZ customizationZdebuggerdeldictzdynamic-featureselse exceptionsZ execmodelZ exprlistsZfloatingforZ formatstringsZfunctionglobalz id-classesZ identifiersifZ imaginaryimportinZintegerslambdaZlistsZnamingnonlocalZnumbersz numeric-typesZobjectszoperator-summarypassZpowerraisereturnzsequence-typesZshiftingZslicingsZ specialattrsZ specialnameszstring-methodsZstringsZ subscriptionstruthtrytypesZtypesfunctionsZ typesmappingZ typesmethodsZ typesmodulesZtypesseqztypesseq-mutableZunarywhilewithyieldN)Ztopicsrr)/usr/lib64/python3.6/pydoc_data/topics.pys0't("@X   1 =`u >8,LC%GW*+0$.LA0"i#h3U?f9qg9%> JPKr|0]g9~~)__pycache__/__init__.cpython-36.opt-1.pycnu[3 uj@sdS)Nrrr+/usr/lib64/python3.6/pydoc_data/__init__.pysPKr|0]g9~~)__pycache__/__init__.cpython-36.opt-2.pycnu[3 uj@sdS)Nrrr+/usr/lib64/python3.6/pydoc_data/__init__.pysPKr|0]'__pycache__/topics.cpython-36.opt-1.pycnu[3 \ N@sdddddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&dd'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLMZdMS)NauThe "assert" statement ********************** Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= "assert" expression ["," expression] The simple form, "assert expression", is equivalent to if __debug__: if not expression: raise AssertionError The extended form, "assert expression1, expression2", is equivalent to if __debug__: if not expression1: raise AssertionError(expression2) These equivalences assume that "__debug__" and "AssertionError" refer to the built-in variables with those names. In the current implementation, the built-in variable "__debug__" is "True" under normal circumstances, "False" when optimization is requested (command line option "-O"). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace. Assignments to "__debug__" are illegal. The value for the built-in variable is determined when the interpreter starts. us+Assignment statements ********************* Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: assignment_stmt ::= (target_list "=")+ (starred_expression | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" [target_list] ")" | "[" [target_list] "]" | attributeref | subscription | slicing | "*" target (See section Primaries for the syntax definitions for *attributeref*, *subscription*, and *slicing*.) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy). Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows. * If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target. * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. * If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be an iterable with at least as many items as there are targets in the target list, minus one. The first items of the iterable are assigned, from left to right, to the targets before the starred target. The final items of the iterable are assigned to the targets after the starred target. A list of the remaining items in the iterable is then assigned to the starred target (the list can be empty). * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): * If the name does not occur in a "global" or "nonlocal" statement in the current code block: the name is bound to the object in the current local namespace. * Otherwise: the name is bound to the object in the global namespace or the outer namespace determined by "nonlocal", respectively. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, "TypeError" is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily "AttributeError"). Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression, "a.x" can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS target "a.x" is always set as an instance attribute, creating it if necessary. Thus, the two occurrences of "a.x" do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment: class Cls: x = 3 # class variable inst = Cls() inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3 This description does not necessarily apply to descriptor attributes, such as properties created with "property()". * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list), the subscript must yield an integer. If it is negative, the sequence’s length is added to it. The resulting value must be a nonnegative integer less than the sequence’s length, and the sequence is asked to assign the assigned object to its item with that index. If the index is out of range, "IndexError" is raised (assignment to a subscripted sequence cannot add new items to a list). If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping’s key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed). For user-defined objects, the "__setitem__()" method is called with appropriate arguments. * If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence’s length. The bounds should evaluate to integers. If either bound is negative, the sequence’s length is added to it. The resulting bounds are clipped to lie between zero and the sequence’s length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it. **CPython implementation detail:** In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages. Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example "a, b = b, a" swaps two variables), overlaps *within* the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints "[0, 2]": x = [0, 1] i = 0 i, x[i] = 1, 2 # i is updated, then x[i] is updated print(x) See also: **PEP 3132** - Extended Iterable Unpacking The specification for the "*target" feature. Augmented assignment statements =============================== Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. Annotated assignment statements =============================== Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement: annotated_assignment_stmt ::= augtarget ":" expression ["=" expression] The difference from normal Assignment statements is that only single target and only single right hand side value is allowed. For simple names as assignment targets, if in class or module scope, the annotations are evaluated and stored in a special class or module attribute "__annotations__" that is a dictionary mapping from variable names (mangled if private) to evaluated annotations. This attribute is writable and is automatically created at the start of class or module body execution, if annotations are found statically. For expressions as assignment targets, the annotations are evaluated if in class or module scope, but not stored. If a name is annotated in a function scope, then this name is local for that scope. Annotations are never evaluated and stored in function scopes. If the right hand side is present, an annotated assignment performs the actual assignment before evaluating annotations (where applicable). If the right hand side is not present for an expression target, then the interpreter evaluates the target except for the last "__setitem__()" or "__setattr__()" call. See also: **PEP 526** - Syntax for Variable Annotations The proposal that added syntax for annotating the types of variables (including class variables and instance variables), instead of expressing them through comments. **PEP 484** - Type hints The proposal that added the "typing" module to provide a standard syntax for type annotations that can be used in static analysis tools and IDEs. aIdentifiers (Names) ******************* An identifier occurring as an atom is a name. See section 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. **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. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier "__spam" occurring in a class named "Ham" will be transformed to "_Ham__spam". This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done. u Literals ******** Python supports string and bytes literals and various numeric literals: literal ::= stringliteral | bytesliteral | integer | floatnumber | imagnumber Evaluation of a literal yields an object of the given type (string, bytes, integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section Literals for details. 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. u-Customizing attribute access **************************** The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access =================================== For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ======================== The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors ==================== In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ ========= *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* -------------------------- * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". aAttribute references ******************** An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the "__getattr__()" method. If this attribute is not available, the exception "AttributeError" is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. aAugmented assignment statements ******************************* Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. uj Binary arithmetic operations **************************** The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non- numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr The "*" (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. The "@" (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. New in version 3.5. The "/" (division) and "//" (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the "ZeroDivisionError" exception. The "%" (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the "ZeroDivisionError" exception. The arguments may be floating point numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 + 0.34".) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [1]. The floor division and modulo operators are connected by the following identity: "x == (x//y)*y + (x%y)". Floor division and modulo are also connected with the built-in function "divmod()": "divmod(x, y) == (x//y, x%y)". [2]. In addition to performing the modulo operation on numbers, the "%" operator is also overloaded by string objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section printf-style String Formatting. The floor division operator, the modulo operator, and the "divmod()" function are not defined for complex numbers. Instead, convert to a floating point number using the "abs()" function if appropriate. The "+" (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The "-" (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. a$Binary bitwise operations ************************* Each of the three bitwise operations has a different priority level: and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr The "&" operator yields the bitwise AND of its arguments, which must be integers. The "^" operator yields the bitwise XOR (exclusive OR) of its arguments, which must be integers. The "|" operator yields the bitwise (inclusive) OR of its arguments, which must be integers. uxCode Objects ************ Code objects are used by the implementation to represent “pseudo- compiled” executable Python code such as a function body. They differ from function objects because they don’t contain a reference to their global execution environment. Code objects are returned by the built- in "compile()" function and can be extracted from function objects through their "__code__" attribute. See also the "code" module. A code object can be executed or evaluated by passing it (instead of a source string) to the "exec()" or "eval()" built-in functions. See The standard type hierarchy for more information. a.The Ellipsis Object ******************* This object is commonly used by slicing (see Slicings). It supports no special operations. There is exactly one ellipsis object, named "Ellipsis" (a built-in name). "type(Ellipsis)()" produces the "Ellipsis" singleton. It is written as "Ellipsis" or "...". uThe Null Object *************** This object is returned by functions that don’t explicitly return a value. It supports no special operations. There is exactly one null object, named "None" (a built-in name). "type(None)()" produces the same singleton. It is written as "None". u5Type Objects ************ Type objects represent the various object types. An object’s type is accessed by the built-in function "type()". There are no special operations on types. The standard module "types" defines names for all standard built-in types. Types are written like this: "". aBoolean operations ****************** or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: "False", "None", numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. User-defined objects can customize their truth value by providing a "__bool__()" method. The operator "not" yields "True" if its argument is false, "False" otherwise. The expression "x and y" first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The expression "x or y" first evaluates *x*; if *x* is true, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. Note that neither "and" nor "or" restrict the value and type they return to "False" and "True", but rather return the last evaluated argument. This is sometimes useful, e.g., if "s" is a string that should be replaced by a default value if it is empty, the expression "s or 'foo'" yields the desired value. Because "not" has to create a new value, it returns a boolean value regardless of the type of its argument (for example, "not 'foo'" produces "False" rather than "''".) a$The "break" statement ********************* break_stmt ::= "break" "break" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional "else" clause if the loop has one. If a "for" loop is terminated by "break", the loop control target keeps its current value. When "break" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the loop. uEmulating callable objects ************************** object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". uCCalls ***** A call calls a callable object (e.g., a *function*) with a possibly empty series of *arguments*: call ::= primary "(" [argument_list [","] | comprehension] ")" argument_list ::= positional_arguments ["," starred_and_keywords] ["," keywords_arguments] | starred_and_keywords ["," keywords_arguments] | keywords_arguments positional_arguments ::= ["*"] expression ("," ["*"] expression)* starred_and_keywords ::= ("*" expression | keyword_item) ("," "*" expression | "," keyword_item)* keywords_arguments ::= (keyword_item | "**" expression) ("," keyword_item | "," "**" expression)* keyword_item ::= identifier "=" expression An optional trailing comma may be present after the positional and keyword arguments but does not affect the semantics. The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a "__call__()" method are callable). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal *parameter* lists. If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a "TypeError" exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is "None", it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don’t specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a "TypeError" exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. **CPython implementation detail:** An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use "PyArg_ParseTuple()" to parse their arguments. If there are more positional arguments than there are formal parameter slots, a "TypeError" exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments). If any keyword argument does not correspond to a formal parameter name, a "TypeError" exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments. If the syntax "*expression" appears in the function call, "expression" must evaluate to an *iterable*. Elements from these iterables are treated as if they were additional positional arguments. For the call "f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, *yM*, this is equivalent to a call with M+4 positional arguments *x1*, *x2*, *y1*, …, *yM*, *x3*, *x4*. A consequence of this is that although the "*expression" syntax may appear *after* explicit keyword arguments, it is processed *before* the keyword arguments (and any "**expression" arguments – see below). So: >>> def f(a, b): ... print(a, b) ... >>> f(b=1, *(2,)) 2 1 >>> f(a=1, *(2,)) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'a' >>> f(1, *(2,)) 1 2 It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise. If the syntax "**expression" appears in the function call, "expression" must evaluate to a *mapping*, the contents of which are treated as additional keyword arguments. If a keyword is already present (as an explicit keyword argument, or from another unpacking), a "TypeError" exception is raised. Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Changed in version 3.5: Function calls accept any number of "*" and "**" unpackings, positional arguments may follow iterable unpackings ("*"), and keyword arguments may follow dictionary unpackings ("**"). Originally proposed by **PEP 448**. A call always returns some value, possibly "None", unless it raises an exception. How this value is computed depends on the type of the callable object. If it is— a user-defined function: The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a "return" statement, this specifies the return value of the function call. a built-in function or method: The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods. a class object: A new instance of that class is returned. a class instance method: The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument. a class instance: The class must define a "__call__()" method; the effect is then the same as if that method was called. u Class definitions ***************** A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. u4)Comparisons *********** Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like "a < b < c" have the interpretation that is conventional in mathematics: comparison ::= or_expr (comp_operator or_expr)* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in" Comparisons yield boolean values: "True" or "False". Comparisons can be chained arbitrarily, e.g., "x < y <= z" is equivalent to "x < y and y <= z", except that "y" is evaluated only once (but in both cases "z" is not evaluated at all when "x < y" is found to be false). Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and *op1*, *op2*, …, *opN* are comparison operators, then "a op1 b op2 c ... y opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except that each expression is evaluated at most once. Note that "a op1 b op2 c" doesn’t imply any kind of comparison between *a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though perhaps not pretty). Value comparisons ================= The operators "<", ">", "==", ">=", "<=", and "!=" compare the values of two objects. The objects do not need to have the same type. Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object’s value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation. Because all types are (direct or indirect) subtypes of "object", they inherit the default comparison behavior from "object". Types can customize their comparison behavior by implementing *rich comparison methods* like "__lt__()", described in Basic customization. The default behavior for equality comparison ("==" and "!=") is based on the identity of the objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of instances with different identities results in inequality. A motivation for this default behavior is the desire that all objects should be reflexive (i.e. "x is y" implies "x == y"). A default order comparison ("<", ">", "<=", and ">=") is not provided; an attempt raises "TypeError". A motivation for this default behavior is the lack of a similar invariant as for equality. The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that. The following list describes the comparison behavior of the most important built-in types. * Numbers of built-in numeric types (Numeric Types — int, float, complex) and of the standard library types "fractions.Fraction" and "decimal.Decimal" can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision. The not-a-number values "float('NaN')" and "Decimal('NaN')" are special. They are identical to themselves ("x is x" is true) but are not equal to themselves ("x == x" is false). Additionally, comparing any number to a not-a-number value will return "False". For example, both "3 < float('NaN')" and "float('NaN') < 3" will return "False". * Binary sequences (instances of "bytes" or "bytearray") can be compared within and across their types. They compare lexicographically using the numeric values of their elements. * Strings (instances of "str") compare lexicographically using the numerical Unicode code points (the result of the built-in function "ord()") of their characters. [3] Strings and binary sequences cannot be directly compared. * Sequences (instances of "tuple", "list", or "range") can be compared only within each of their types, with the restriction that ranges do not support order comparison. Equality comparison across these types results in inequality, and ordering comparison across these types raises "TypeError". Sequences compare lexicographically using comparison of corresponding elements, whereby reflexivity of the elements is enforced. In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element "x", "x == x" is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison, and may be surprising: The non-reflexive not-a-number values for example result in the following comparison behavior when used in a list: >>> nan = float('NaN') >>> nan is nan True >>> nan == nan False <-- the defined non-reflexive behavior of NaN >>> [nan] == [nan] True <-- list enforces reflexivity and tests identity first Lexicographical comparison between built-in collections works as follows: * For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, "[1,2] == (1,2)" is false because the type is not the same). * Collections that support order comparison are ordered the same as their first unequal elements (for example, "[1,2,x] <= [1,2,y]" has the same value as "x <= y"). If a corresponding element does not exist, the shorter collection is ordered first (for example, "[1,2] < [1,2,3]" is true). * Mappings (instances of "dict") compare equal if and only if they have equal *(key, value)* pairs. Equality comparison of the keys and values enforces reflexivity. Order comparisons ("<", ">", "<=", and ">=") raise "TypeError". * Sets (instances of "set" or "frozenset") can be compared within and across their types. They define order comparison operators to mean subset and superset tests. Those relations do not define total orderings (for example, the two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one another, nor supersets of one another). Accordingly, sets are not appropriate arguments for functions which depend on total ordering (for example, "min()", "max()", and "sorted()" produce undefined results given a list of sets as inputs). Comparison of sets enforces reflexivity of its elements. * Most other built-in types have no comparison methods implemented, so they inherit the default comparison behavior. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: * Equality comparison should be reflexive. In other words, identical objects should compare equal: "x is y" implies "x == y" * Comparison should be symmetric. In other words, the following expressions should have the same result: "x == y" and "y == x" "x != y" and "y != x" "x < y" and "y > x" "x <= y" and "y >= x" * Comparison should be transitive. The following (non-exhaustive) examples illustrate that: "x > y and y > z" implies "x > z" "x < y and y <= z" implies "x < z" * Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: "x == y" and "not x != y" "x < y" and "not x >= y" (for total ordering) "x > y" and "not x <= y" (for total ordering) The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the "total_ordering()" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. Python does not enforce these consistency rules. In fact, the not-a-number values are an example for not following these rules. Membership test operations ========================== The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. Object identity is determined using the "id()" function. "x is not y" yields the inverse truth value. [4] uxeCompound statements ******************* Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The "if", "while" and "for" statements implement traditional control flow constructs. "try" specifies exception handlers and/or cleanup code for a group of statements, while the "with" statement allows the execution of initialization and finalization code around a block of code. Function and class definitions are also syntactically compound statements. A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of a suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which "if" clause a following "else" clause would belong: if test1: if test2: print(x) Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the "print()" calls are executed: if x < y < z: print(x); print(y); print(z) Summarizing: compound_stmt ::= if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | async_with_stmt | async_for_stmt | async_funcdef suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT statement ::= stmt_list NEWLINE | compound_stmt stmt_list ::= simple_stmt (";" simple_stmt)* [";"] Note that statements always end in a "NEWLINE" possibly followed by a "DEDENT". Also note that optional continuation clauses always begin with a keyword that cannot start a statement, thus there are no ambiguities (the ‘dangling "else"’ problem is solved in Python by requiring nested "if" statements to be indented). The formatting of the grammar rules in the following sections places each clause on a separate line for clarity. The "if" statement ================== The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. The "while" statement ===================== The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. The "for" statement =================== The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) The "try" statement =================== The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. The "with" statement ==================== The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Function definitions ==================== A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. Class definitions ================= A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. Coroutines ========== New in version 3.5. Coroutine function definition ----------------------------- async_funcdef ::= [decorators] "async" "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite Execution of Python coroutines can be suspended and resumed at many points (see *coroutine*). In the body of a coroutine, any "await" and "async" identifiers become reserved keywords; "await" expressions, "async for" and "async with" can only be used in coroutine bodies. Functions defined with "async def" syntax are always coroutine functions, even if they do not contain "await" or "async" keywords. It is a "SyntaxError" to use "yield from" expressions in "async def" coroutines. An example of a coroutine function: async def func(param1, param2): do_stuff() await some_coroutine() The "async for" statement ------------------------- async_for_stmt ::= "async" for_stmt An *asynchronous iterable* is able to call asynchronous code in its *iter* implementation, and *asynchronous iterator* can call asynchronous code in its *next* method. The "async for" statement allows convenient iteration over asynchronous iterators. The following code: async for TARGET in ITER: BLOCK else: BLOCK2 Is semantically equivalent to: iter = (ITER) iter = type(iter).__aiter__(iter) running = True while running: try: TARGET = await type(iter).__anext__(iter) except StopAsyncIteration: running = False else: BLOCK else: BLOCK2 See also "__aiter__()" and "__anext__()" for details. It is a "SyntaxError" to use "async for" statement outside of an "async def" function. The "async with" statement -------------------------- async_with_stmt ::= "async" with_stmt An *asynchronous context manager* is a *context manager* that is able to suspend execution in its *enter* and *exit* methods. The following code: async with EXPR as VAR: BLOCK Is semantically equivalent to: mgr = (EXPR) aexit = type(mgr).__aexit__ aenter = type(mgr).__aenter__(mgr) VAR = await aenter try: BLOCK except: if not await aexit(mgr, *sys.exc_info()): raise else: await aexit(mgr, None, None, None) See also "__aenter__()" and "__aexit__()" for details. It is a "SyntaxError" to use "async with" statement outside of an "async def" function. See also: **PEP 492** - Coroutines with async and await syntax The proposal that made coroutines a proper standalone concept in Python, and added supporting syntax. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] A string literal appearing as the first statement in the function body is transformed into the function’s "__doc__" attribute and therefore the function’s *docstring*. [3] A string literal appearing as the first statement in the class body is transformed into the namespace’s "__doc__" item and therefore the class’s *docstring*. uWith Statement Context Managers ******************************* A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. aThe "continue" statement ************************ continue_stmt ::= "continue" "continue" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition or "finally" clause within that loop. It continues with the next cycle of the nearest enclosing loop. When "continue" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really starting the next loop cycle. uArithmetic conversions ********************** When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type,” this means that the operator implementation for built-in types works as follows: * If either argument is a complex number, the other is converted to complex; * otherwise, if either argument is a floating point number, the other is converted to floating point; * otherwise, both must be integers and no conversion is necessary. Some additional rules apply for certain operators (e.g., a string as a left argument to the ‘%’ operator). Extensions must define their own conversion behavior. u3Basic customization ******************* object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. uiF"pdb" — The Python Debugger *************************** **Source code:** Lib/pdb.py ====================================================================== The module "pdb" defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible – it is actually defined as the class "Pdb". This is currently undocumented but easily understood by reading the source. The extension interface uses the modules "bdb" and "cmd". The debugger’s prompt is "(Pdb)". Typical usage to run a program under control of the debugger is: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') > (0)?() (Pdb) continue > (1)?() (Pdb) continue NameError: 'spam' > (1)?() (Pdb) Changed in version 3.3: Tab-completion via the "readline" module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the "p" command. "pdb.py" can also be invoked as a script to debug other scripts. For example: python3 -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post- mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit. New in version 3.2: "pdb.py" now accepts a "-c" option that executes commands as if given in a ".pdbrc" file, see Debugger Commands. The typical usage to break into the debugger from a running program is to insert import pdb; pdb.set_trace() at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the "continue" command. The typical usage to inspect a crashed program is: >>> import pdb >>> import mymodule >>> mymodule.test() Traceback (most recent call last): File "", line 1, in File "./mymodule.py", line 4, in test test2() File "./mymodule.py", line 3, in test2 print(spam) NameError: spam >>> pdb.pm() > ./mymodule.py(3)test2() -> print(spam) (Pdb) The module defines the following functions; each enters the debugger in a slightly different way: pdb.run(statement, globals=None, locals=None) Execute the *statement* (given as a string or a code object) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type "continue", or you can step through the statement using "step" or "next" (all these commands are explained below). The optional *globals* and *locals* arguments specify the environment in which the code is executed; by default the dictionary of the module "__main__" is used. (See the explanation of the built-in "exec()" or "eval()" functions.) pdb.runeval(expression, globals=None, locals=None) Evaluate the *expression* (given as a string or a code object) under debugger control. When "runeval()" returns, it returns the value of the expression. Otherwise this function is similar to "run()". pdb.runcall(function, *args, **kwds) Call the *function* (a function or method object, not a string) with the given arguments. When "runcall()" returns, it returns whatever the function call returned. The debugger prompt appears as soon as the function is entered. pdb.set_trace() Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). pdb.post_mortem(traceback=None) Enter post-mortem debugging of the given *traceback* object. If no *traceback* is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used). pdb.pm() Enter post-mortem debugging of the traceback found in "sys.last_traceback". The "run*" functions and "set_trace()" are aliases for instantiating the "Pdb" class and calling the method of the same name. If you want to access further features, you have to do this yourself: class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True) "Pdb" is the debugger class. The *completekey*, *stdin* and *stdout* arguments are passed to the underlying "cmd.Cmd" class; see the description there. The *skip* argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] By default, Pdb sets a handler for the SIGINT signal (which is sent when the user presses "Ctrl-C" on the console) when you give a "continue" command. This allows you to break into the debugger again by pressing "Ctrl-C". If you want Pdb not to touch the SIGINT handler, set *nosigint* to true. The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. Example call to enable tracing with *skip*: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 3.1: The *skip* argument. New in version 3.2: The *nosigint* argument. Previously, a SIGINT handler was never set by Pdb. Changed in version 3.6: The *readrc* argument. run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) set_trace() See the documentation for the functions explained above. Debugger Commands ================= The commands recognized by the debugger are listed below. Most commands can be abbreviated to one or two letters as indicated; e.g. "h(elp)" means that either "h" or "help" can be used to enter the help command (but not "he" or "hel", nor "H" or "Help" or "HELP"). Arguments to commands must be separated by whitespace (spaces or tabs). Optional arguments are enclosed in square brackets ("[]") in the command syntax; the square brackets must not be typed. Alternatives in the command syntax are separated by a vertical bar ("|"). Entering a blank line repeats the last command entered. Exception: if the last command was a "list" command, the next 11 lines are listed. Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point ("!"). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debugger’s state is not changed. The debugger supports aliases. Aliases can have parameters which allows one a certain level of adaptability to the context under examination. Multiple commands may be entered on a single line, separated by ";;". (A single ";" is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ";;" pair, even if it is in the middle of a quoted string. If a file ".pdbrc" exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file. Changed in version 3.2: ".pdbrc" can now contain commands that continue debugging, such as "continue" or "next". Previously, these commands had no effect. h(elp) [command] Without argument, print the list of available commands. With a *command* as argument, print help about that command. "help pdb" displays the full documentation (the docstring of the "pdb" module). Since the *command* argument must be an identifier, "help exec" must be entered to get help on the "!" command. w(here) Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands. d(own) [count] Move the current frame *count* (default one) levels down in the stack trace (to a newer frame). u(p) [count] Move the current frame *count* (default one) levels up in the stack trace (to an older frame). b(reak) [([filename:]lineno | function) [, condition]] With a *lineno* argument, set a break there in the current file. With a *function* argument, set a break at the first executable statement within that function. The line number may be prefixed with a filename and a colon, to specify a breakpoint in another file (probably one that hasn’t been loaded yet). The file is searched on "sys.path". Note that each breakpoint is assigned a number to which all the other breakpoint commands refer. If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored. Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any. tbreak [([filename:]lineno | function) [, condition]] Temporary breakpoint, which is removed automatically when it is first hit. The arguments are the same as for "break". cl(ear) [filename:lineno | bpnumber [bpnumber ...]] With a *filename:lineno* argument, clear all the breakpoints at this line. With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). disable [bpnumber [bpnumber ...]] Disable the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled. enable [bpnumber [bpnumber ...]] Enable the breakpoints specified. ignore bpnumber [count] Set the ignore count for the given breakpoint number. If count is omitted, the ignore count is set to 0. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true. condition bpnumber [condition] Set a new *condition* for the breakpoint, an expression which must evaluate to true before the breakpoint is honored. If *condition* is absent, any existing condition is removed; i.e., the breakpoint is made unconditional. commands [bpnumber] Specify a list of commands for breakpoint number *bpnumber*. The commands themselves appear on the following lines. Type a line containing just "end" to terminate the commands. An example: (Pdb) commands 1 (com) p some_variable (com) end (Pdb) To remove all commands from a breakpoint, type commands and follow it immediately with "end"; that is, give no commands. With no *bpnumber* argument, commands refers to the last breakpoint set. You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution. Specifying any command resuming execution (currently continue, step, next, return, jump, quit and their abbreviations) terminates the command list (as if that command was immediately followed by end). This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint—which could have its own command list, leading to ambiguities about which list to execute. If you use the ‘silent’ command in the command list, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the other commands print anything, you see no sign that the breakpoint was reached. s(tep) Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function). n(ext) Continue execution until the next line in the current function is reached or it returns. (The difference between "next" and "step" is that "step" stops inside a called function, while "next" executes called functions at (nearly) full speed, only stopping at the next line in the current function.) unt(il) [lineno] Without argument, continue execution until the line with a number greater than the current one is reached. With a line number, continue execution until a line with a number greater or equal to that is reached. In both cases, also stop when the current frame returns. Changed in version 3.2: Allow giving an explicit line number. r(eturn) Continue execution until the current function returns. c(ont(inue)) Continue execution, only stop when a breakpoint is encountered. j(ump) lineno Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run. It should be noted that not all jumps are allowed – for instance it is not possible to jump into the middle of a "for" loop or out of a "finally" clause. l(ist) [first[, last]] List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With "." as argument, list 11 lines around the current line. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count. The current line in the current frame is indicated by "->". If an exception is being debugged, the line where the exception was originally raised or propagated is indicated by ">>", if it differs from the current line. New in version 3.2: The ">>" marker. ll | longlist List all source code for the current function or frame. Interesting lines are marked as for "list". New in version 3.2. a(rgs) Print the argument list of the current function. p expression Evaluate the *expression* in the current context and print its value. Note: "print()" can also be used, but is not a debugger command — this executes the Python "print()" function. pp expression Like the "p" command, except the value of the expression is pretty- printed using the "pprint" module. whatis expression Print the type of the *expression*. source expression Try to get source code for the given object and display it. New in version 3.2. display [expression] Display the value of the expression if it changed, each time execution stops in the current frame. Without expression, list all display expressions for the current frame. New in version 3.2. undisplay [expression] Do not display the expression any more in the current frame. Without expression, clear all display expressions for the current frame. New in version 3.2. interact Start an interactive interpreter (using the "code" module) whose global namespace contains all the (global and local) names found in the current scope. New in version 3.2. alias [name [command]] Create an alias called *name* that executes *command*. The command must *not* be enclosed in quotes. Replaceable parameters can be indicated by "%1", "%2", and so on, while "%*" is replaced by all the parameters. If no command is given, the current alias for *name* is shown. If no arguments are given, all aliases are listed. Aliases may be nested and can contain anything that can be legally typed at the pdb prompt. Note that internal pdb commands *can* be overridden by aliases. Such a command is then hidden until the alias is removed. Aliasing is recursively applied to the first word of the command line; all other words in the line are left alone. As an example, here are two useful aliases (especially when placed in the ".pdbrc" file): # Print instance variables (usage "pi classInst") alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k]) # Print instance variables in self alias ps pi self unalias name Delete the specified alias. ! statement Execute the (one-line) *statement* in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a "global" statement on the same line, e.g.: (Pdb) global list_options; list_options = ['-l'] (Pdb) run [args ...] restart [args ...] Restart the debugged Python program. If an argument is supplied, it is split with "shlex" and the result is used as the new "sys.argv". History, breakpoints, actions and debugger options are preserved. "restart" is an alias for "run". q(uit) Quit from the debugger. The program being executed is aborted. -[ Footnotes ]- [1] Whether a frame is considered to originate in a certain module is determined by the "__name__" in the frame globals. aThe "del" statement ******************* del_stmt ::= "del" target_list Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints. Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a "global" statement in the same code block. If the name is unbound, a "NameError" exception will be raised. Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object). Changed in version 3.2: Previously it was illegal to delete a name from the local namespace if it occurs as a free variable in a nested block. u Dictionary displays ******************* A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression | "**" or_expr dict_comprehension ::= expression ":" expression comp_for A dictionary display yields a new dictionary object. If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given. A double asterisk "**" denotes *dictionary unpacking*. Its operand must be a *mapping*. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings. New in version 3.5: Unpacking into dictionary displays, originally proposed by **PEP 448**. A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual “for” and “if” clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced. Restrictions on the types of the key values are listed earlier in section The standard type hierarchy. (To summarize, the key type should be *hashable*, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails. aInteraction with dynamic features ********************************* Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aBThe "if" statement ****************** The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. uExceptions ********** Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. u$Execution model *************** Structure of a program ====================== A Python program is constructed from code blocks. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the "-c" option) is a code block. The string argument passed to the built-in functions "eval()" and "exec()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed. Naming and binding ================== Binding of names ---------------- *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names ------------------- A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution --------------------------------- **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features --------------------------------- Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. Exceptions ========== Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. uoExpression lists **************** expression_list ::= expression ("," expression)* [","] starred_list ::= starred_item ("," starred_item)* [","] starred_expression ::= expression | (starred_item ",")* [starred_item] starred_item ::= expression | "*" or_expr Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. An asterisk "*" denotes *iterable unpacking*. Its operand must be an *iterable*. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking. New in version 3.5: Iterable unpacking in expression lists, originally proposed by **PEP 448**. The trailing comma is required only to create a single tuple (a.k.a. a *singleton*); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: "()".) aFloating point literals *********************** Floating point literals are described by the following lexical definitions: floatnumber ::= pointfloat | exponentfloat pointfloat ::= [digitpart] fraction | digitpart "." exponentfloat ::= (digitpart | pointfloat) exponent digitpart ::= digit (["_"] digit)* fraction ::= "." digitpart exponent ::= ("e" | "E") ["+" | "-"] digitpart Note that the integer and exponent parts are always interpreted using radix 10. For example, "077e010" is legal, and denotes the same number as "77e10". The allowed range of floating point literals is implementation-dependent. As in integer literals, underscores are supported for digit grouping. Some examples of floating point literals: 3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. u The "for" statement ******************* The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) u YFormat String Syntax ******************** The "str.format()" method and the "Formatter" class share the same syntax for format strings (although in the case of "Formatter", subclasses can define their own format string syntax). The syntax is related to that of formatted string literals, but there are differences. Format strings contain “replacement fields” surrounded by curly braces "{}". Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: "{{" and "}}". The grammar for a replacement field is as follows: replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | digit+] attribute_name ::= identifier element_index ::= digit+ | index_string index_string ::= + conversion ::= "r" | "s" | "a" format_spec ::= In less formal terms, the replacement field can start with a *field_name* that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is preceded by an exclamation point "'!'", and a *format_spec*, which is preceded by a colon "':'". These specify a non-default format for the replacement value. See also the Format Specification Mini-Language section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings "'10'" or "':-]'") within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form "'.name'" selects the named attribute using "getattr()", while an expression of the form "'[index]'" does an index lookup using "__getitem__()". Changed in version 3.1: The positional argument specifiers can be omitted for "str.format()", so "'{} {}'.format(a, b)" is equivalent to "'{0} {1}'.format(a, b)". Changed in version 3.4: The positional argument specifiers can be omitted for "Formatter". Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the "__format__()" method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling "__format__()", the normal formatting logic is bypassed. Three conversion flags are currently supported: "'!s'" which calls "str()" on the value, "'!r'" which calls "repr()" and "'!a'" which calls "ascii()". Some examples: "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own “formatting mini-language” or interpretation of the *format_spec*. Most built-in types support a common formatting mini-language, which is described in the next section. A *format_spec* field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the *format_spec* string is interpreted. This allows the formatting of a value to be dynamically specified. See the Format examples section for some examples. Format Specification Mini-Language ================================== “Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax and Formatted string literals). They can also be passed directly to the built-in "format()" function. Each formattable type may define how the format specification is to be interpreted. Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types. A general convention is that an empty format string ("""") produces the same result as if you had called "str()" on the value. A non-empty format string typically modifies the result. The general form of a *standard format specifier* is: format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type] fill ::= align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= digit+ grouping_option ::= "_" | "," precision ::= digit+ type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" If a valid *align* value is specified, it can be preceded by a *fill* character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (“"{"” or “"}"”) as the *fill* character in a formatted string literal or when using the "str.format()" method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the "format()" function. The meaning of the various alignment options is as follows: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'<'" | Forces the field to be left-aligned within the available | | | space (this is the default for most objects). | +-----------+------------------------------------------------------------+ | "'>'" | Forces the field to be right-aligned within the available | | | space (this is the default for numbers). | +-----------+------------------------------------------------------------+ | "'='" | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form ‘+000000120’. This alignment option is only | | | valid for numeric types. It becomes the default when ‘0’ | | | immediately precedes the field width. | +-----------+------------------------------------------------------------+ | "'^'" | Forces the field to be centered within the available | | | space. | +-----------+------------------------------------------------------------+ Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The *sign* option is only valid for number types, and can be one of the following: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'+'" | indicates that a sign should be used for both positive as | | | well as negative numbers. | +-----------+------------------------------------------------------------+ | "'-'" | indicates that a sign should be used only for negative | | | numbers (this is the default behavior). | +-----------+------------------------------------------------------------+ | space | indicates that a leading space should be used on positive | | | numbers, and a minus sign on negative numbers. | +-----------+------------------------------------------------------------+ The "'#'" option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and Decimal types. For integers, when binary, octal, or hexadecimal output is used, this option adds the prefix respective "'0b'", "'0o'", or "'0x'" to the output value. For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for "'g'" and "'G'" conversions, trailing zeros are not removed from the result. The "','" option signals the use of a comma for a thousands separator. For a locale aware separator, use the "'n'" integer presentation type instead. Changed in version 3.1: Added the "','" option (see also **PEP 378**). The "'_'" option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type "'d'". For integer presentation types "'b'", "'o'", "'x'", and "'X'", underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error. Changed in version 3.6: Added the "'_'" option (see also **PEP 515**). *width* is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the *width* field by a zero ("'0'") character enables sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of "'0'" with an *alignment* type of "'='". The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with "'f'" and "'F'", or before and after the decimal point for a floating point value formatted with "'g'" or "'G'". For non- number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The *precision* is not allowed for integer values. Finally, the *type* determines how the data should be presented. The available string presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'s'" | String format. This is the default type for strings and | | | may be omitted. | +-----------+------------------------------------------------------------+ | None | The same as "'s'". | +-----------+------------------------------------------------------------+ The available integer presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'b'" | Binary format. Outputs the number in base 2. | +-----------+------------------------------------------------------------+ | "'c'" | Character. Converts the integer to the corresponding | | | unicode character before printing. | +-----------+------------------------------------------------------------+ | "'d'" | Decimal Integer. Outputs the number in base 10. | +-----------+------------------------------------------------------------+ | "'o'" | Octal format. Outputs the number in base 8. | +-----------+------------------------------------------------------------+ | "'x'" | Hex format. Outputs the number in base 16, using lower- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'X'" | Hex format. Outputs the number in base 16, using upper- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'d'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | None | The same as "'d'". | +-----------+------------------------------------------------------------+ In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except "'n'" and "None"). When doing so, "float()" is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'e'" | Exponent notation. Prints the number in scientific | | | notation using the letter ‘e’ to indicate the exponent. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'E'" | Exponent notation. Same as "'e'" except it uses an upper | | | case ‘E’ as the separator character. | +-----------+------------------------------------------------------------+ | "'f'" | Fixed-point notation. Displays the number as a fixed-point | | | number. The default precision is "6". | +-----------+------------------------------------------------------------+ | "'F'" | Fixed-point notation. Same as "'f'", but converts "nan" to | | | "NAN" and "inf" to "INF". | +-----------+------------------------------------------------------------+ | "'g'" | General format. For a given precision "p >= 1", this | | | rounds the number to "p" significant digits and then | | | formats the result in either fixed-point format or in | | | scientific notation, depending on its magnitude. The | | | precise rules are as follows: suppose that the result | | | formatted with presentation type "'e'" and precision "p-1" | | | would have exponent "exp". Then if "-4 <= exp < p", the | | | number is formatted with presentation type "'f'" and | | | precision "p-1-exp". Otherwise, the number is formatted | | | with presentation type "'e'" and precision "p-1". In both | | | cases insignificant trailing zeros are removed from the | | | significand, and the decimal point is also removed if | | | there are no remaining digits following it. Positive and | | | negative infinity, positive and negative zero, and nans, | | | are formatted as "inf", "-inf", "0", "-0" and "nan" | | | respectively, regardless of the precision. A precision of | | | "0" is treated as equivalent to a precision of "1". The | | | default precision is "6". | +-----------+------------------------------------------------------------+ | "'G'" | General format. Same as "'g'" except switches to "'E'" if | | | the number gets too large. The representations of infinity | | | and NaN are uppercased, too. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'g'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | "'%'" | Percentage. Multiplies the number by 100 and displays in | | | fixed ("'f'") format, followed by a percent sign. | +-----------+------------------------------------------------------------+ | None | Similar to "'g'", except that fixed-point notation, when | | | used, has at least one digit past the decimal point. The | | | default precision is as high as needed to represent the | | | particular value. The overall effect is to match the | | | output of "str()" as altered by the other format | | | modifiers. | +-----------+------------------------------------------------------------+ Format examples =============== This section contains examples of the "str.format()" syntax and comparison with the old "%"-formatting. In most of the cases the syntax is similar to the old "%"-formatting, with the addition of the "{}" and with ":" used instead of "%". For example, "'%03.2f'" can be translated to "'{:03.2f}'". The new format syntax also supports new and different options, shown in the following examples. Accessing arguments by position: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 'abracadabra' Accessing arguments by name: >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 'Coordinates: 37.24N, -115.81W' >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 'Coordinates: 37.24N, -115.81W' Accessing arguments’ attributes: >>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point: ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)' Accessing arguments’ items: >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' Replacing "%s" and "%r": >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') "repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign: >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases: >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma as a thousands separator: >>> '{:,}'.format(1234567890) '1,234,567,890' Expressing a percentage: >>> points = 19 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 86.36%' Using type-specific formatting: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' Nesting arguments and more complex examples: >>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ... 'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right' >>> >>> octets = [192, 168, 0, 1] >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 'C0A80001' >>> int(_, 16) 3232235521 >>> >>> width = 5 >>> for num in range(5,12): ... for base in 'dXob': ... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ') ... print() ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 u[Function definitions ******************** A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. uThe "global" statement ********************** global_stmt ::= "global" identifier ("," identifier)* The "global" statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without "global", although free variables may refer to globals without being declared global. Names listed in a "global" statement must not be used in the same code block textually preceding that "global" statement. Names listed in a "global" statement must not be defined as formal parameters or in a "for" loop control target, "class" definition, function definition, "import" statement, or variable annotation. **CPython implementation detail:** The current implementation does not enforce some of these restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program. **Programmer’s note:** "global" is a directive to the parser. It applies only to code parsed at the same time as the "global" statement. In particular, a "global" statement contained in a string or code object supplied to the built-in "exec()" function does not affect the code block *containing* the function call, and code contained in such a string is unaffected by "global" statements in the code containing the function call. The same applies to the "eval()" and "compile()" functions. uReserved classes of identifiers ******************************* Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). u(Identifiers and keywords ************************ Identifiers (also referred to as *names*) are described by the following lexical definitions. The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also **PEP 3131** for further details. Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters "A" through "Z", the underscore "_" and, except for the first character, the digits "0" through "9". Python 3.0 introduces additional characters from outside the ASCII range (see **PEP 3131**). For these characters, the classification uses the version of the Unicode Character Database as included in the "unicodedata" module. Identifiers are unlimited in length. Case is significant. identifier ::= xid_start xid_continue* id_start ::= id_continue ::= xid_start ::= xid_continue ::= The Unicode category codes mentioned above stand for: * *Lu* - uppercase letters * *Ll* - lowercase letters * *Lt* - titlecase letters * *Lm* - modifier letters * *Lo* - other letters * *Nl* - letter numbers * *Mn* - nonspacing marks * *Mc* - spacing combining marks * *Nd* - decimal numbers * *Pc* - connector punctuations * *Other_ID_Start* - explicit list of characters in PropList.txt to support backwards compatibility * *Other_ID_Continue* - likewise All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC. A non-normative HTML file listing all valid identifier characters for Unicode 4.1 can be found at https://www.dcl.hpi.uni- potsdam.de/home/loewis/table-3131.html. Keywords ======== The following identifiers are used as reserved words, or *keywords* of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Reserved classes of identifiers =============================== Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). a5Imaginary literals ****************** Imaginary literals are described by the following lexical definitions: imagnumber ::= (floatnumber | digitpart) ("j" | "J") An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., "(3+4j)". Some examples of imaginary literals: 3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j u The "import" statement ********************** import_stmt ::= "import" module ["as" identifier] ("," module ["as" identifier])* | "from" relative_module "import" identifier ["as" identifier] ("," identifier ["as" identifier])* | "from" relative_module "import" "(" identifier ["as" identifier] ("," identifier ["as" identifier])* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ The basic import statement (no "from" clause) is executed in two steps: 1. find a module, loading and initializing it if necessary 2. define a name or names in the local namespace for the scope where the "import" statement occurs. When the statement contains multiple clauses (separated by commas) the two steps are carried out separately for each clause, just as though the clauses had been separated out into individual import statements. The details of the first step, finding and loading modules are described in greater detail in the section on the import system, which also describes the various types of packages and modules that can be imported, as well as all the hooks that can be used to customize the import system. Note that failures in this step may indicate either that the module could not be located, *or* that an error occurred while initializing the module, which includes execution of the module’s code. If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways: * If the module name is followed by "as", then the name following "as" is bound directly to the imported module. * If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module * If the module being imported is *not* a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly The "from" form uses a slightly more complex process: 1. find the module specified in the "from" clause, loading and initializing it if necessary; 2. for each of the identifiers specified in the "import" clauses: 1. check if the imported module has an attribute by that name 2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute 3. if the attribute is not found, "ImportError" is raised. 4. otherwise, a reference to that value is stored in the local namespace, using the name in the "as" clause if it is present, otherwise using the attribute name Examples: import foo # foo imported and bound locally import foo.bar.baz # foo.bar.baz imported, foo bound locally import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb from foo.bar import baz # foo.bar.baz imported and bound as baz from foo import attr # foo imported and foo.attr bound as attr If the list of identifiers is replaced by a star ("'*'"), all public names defined in the module are bound in the local namespace for the scope where the "import" statement occurs. The *public names* defined by a module are determined by checking the module’s namespace for a variable named "__all__"; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in "__all__" are all considered public and are required to exist. If "__all__" is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ("'_'"). "__all__" should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The wild card form of import — "from module import *" — is only allowed at the module level. Attempting to use it in class or function definitions will raise a "SyntaxError". When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after "from" you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute "from . import mod" from a module in the "pkg" package then you will end up importing "pkg.mod". If you execute "from ..subpkg2 import mod" from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The specification for relative imports is contained within **PEP 328**. "importlib.import_module()" is provided to support applications that determine dynamically the modules to be loaded. Future statements ================= A *future statement* is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python where the feature becomes standard. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard. future_stmt ::= "from" "__future__" "import" feature ["as" identifier] ("," feature ["as" identifier])* | "from" "__future__" "import" "(" feature ["as" identifier] ("," feature ["as" identifier])* [","] ")" feature ::= identifier A future statement must appear near the top of the module. The only lines that can appear before a future statement are: * the module docstring (if any), * comments, * blank lines, and * other future statements. The features recognized by Python 3.0 are "absolute_import", "division", "generators", "unicode_literals", "print_function", "nested_scopes" and "with_statement". They are all redundant because they are always enabled, and only kept for backwards compatibility. A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future statement contains a feature not known to it. The direct runtime semantics are the same as for any import statement: there is a standard module "__future__", described later, and it will be imported in the usual way at the time the future statement is executed. The interesting runtime semantics depend on the specific feature enabled by the future statement. Note that there is nothing special about the statement: import __future__ [as name] That is not a future statement; it’s an ordinary import statement with no special semantics or syntax restrictions. Code compiled by calls to the built-in functions "exec()" and "compile()" that occur in a module "M" containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can be controlled by optional arguments to "compile()" — see the documentation of that function for details. A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session. If an interpreter is started with the "-i" option, is passed a script name to execute, and the script includes a future statement, it will be in effect in the interactive session started after the script is executed. See also: **PEP 236** - Back to the __future__ The original proposal for the __future__ mechanism. aOMembership test operations ************************** The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". aVInteger literals **************** Integer literals are described by the following lexical definitions: integer ::= decinteger | bininteger | octinteger | hexinteger decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")* bininteger ::= "0" ("b" | "B") (["_"] bindigit)+ octinteger ::= "0" ("o" | "O") (["_"] octdigit)+ hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+ nonzerodigit ::= "1"..."9" digit ::= "0"..."9" bindigit ::= "0" | "1" octdigit ::= "0"..."7" hexdigit ::= digit | "a"..."f" | "A"..."F" There is no limit for the length of integer literals apart from what can be stored in available memory. Underscores are ignored for determining the numeric value of the literal. They can be used to group digits for enhanced readability. One underscore can occur between digits, and after base specifiers like "0x". Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0. Some examples of integer literals: 7 2147483647 0o177 0b100110111 3 79228162514264337593543950336 0o377 0xdeadbeef 100_000_000_000 0b_1110_0101 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. a^Lambdas ******* lambda_expr ::= "lambda" [parameter_list] ":" expression lambda_expr_nocond ::= "lambda" [parameter_list] ":" expression_nocond Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression "lambda parameters: expression" yields a function object. The unnamed object behaves like a function object defined with: def (parameters): return expression See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements or annotations. a/List displays ************* A list display is a possibly empty series of expressions enclosed in square brackets: list_display ::= "[" [starred_list | comprehension] "]" A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma- separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a comprehension is supplied, the list is constructed from the elements resulting from the comprehension. uNaming and binding ****************** Binding of names ================ *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names =================== A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution ================================= **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features ================================= Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aThe "nonlocal" statement ************************ nonlocal_stmt ::= "nonlocal" identifier ("," identifier)* The "nonlocal" statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope. Names listed in a "nonlocal" statement, unlike those listed in a "global" statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously). Names listed in a "nonlocal" statement must not collide with pre- existing bindings in the local scope. See also: **PEP 3104** - Access to Names in Outer Scopes The specification for the "nonlocal" statement. uNumeric literals **************** There are three types of numeric literals: integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator ‘"-"‘ and the literal "1". uEmulating numeric types *********************** The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". u Objects, values and types ************************* *Objects* are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.) Every object has an identity, a type and a value. An object’s *identity* never changes once it has been created; you may think of it as the object’s address in memory. The ‘"is"’ operator compares the identity of two objects; the "id()" function returns an integer representing its identity. **CPython implementation detail:** For CPython, "id(x)" is the memory address where "x" is stored. An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The "type()" function returns an object’s type (which is an object itself). Like its identity, an object’s *type* is also unchangeable. [1] The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable. Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable. **CPython implementation detail:** CPython currently uses a reference- counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the "gc" module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (so you should always close files explicitly). Note that the use of the implementation’s tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a ‘"try"…"except"’ statement may keep objects alive. Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a "close()" method. Programs are strongly recommended to explicitly close such objects. The ‘"try"…"finally"’ statement and the ‘"with"’ statement provide convenient ways to do this. Some objects contain references to other objects; these are called *containers*. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after "a = 1; b = 1", "a" and "b" may or may not refer to the same object with the value one, depending on the implementation, but after "c = []; d = []", "c" and "d" are guaranteed to refer to two different, unique, newly created empty lists. (Note that "c = d = []" assigns the same object to both "c" and "d".) uOperator precedence ******************* The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left). Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section. +-------------------------------------------------+---------------------------------------+ | Operator | Description | +=================================================+=======================================+ | "lambda" | Lambda expression | +-------------------------------------------------+---------------------------------------+ | "if" – "else" | Conditional expression | +-------------------------------------------------+---------------------------------------+ | "or" | Boolean OR | +-------------------------------------------------+---------------------------------------+ | "and" | Boolean AND | +-------------------------------------------------+---------------------------------------+ | "not" "x" | Boolean NOT | +-------------------------------------------------+---------------------------------------+ | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership | | ">=", "!=", "==" | tests and identity tests | +-------------------------------------------------+---------------------------------------+ | "|" | Bitwise OR | +-------------------------------------------------+---------------------------------------+ | "^" | Bitwise XOR | +-------------------------------------------------+---------------------------------------+ | "&" | Bitwise AND | +-------------------------------------------------+---------------------------------------+ | "<<", ">>" | Shifts | +-------------------------------------------------+---------------------------------------+ | "+", "-" | Addition and subtraction | +-------------------------------------------------+---------------------------------------+ | "*", "@", "/", "//", "%" | Multiplication, matrix | | | multiplication, division, floor | | | division, remainder [5] | +-------------------------------------------------+---------------------------------------+ | "+x", "-x", "~x" | Positive, negative, bitwise NOT | +-------------------------------------------------+---------------------------------------+ | "**" | Exponentiation [6] | +-------------------------------------------------+---------------------------------------+ | "await" "x" | Await expression | +-------------------------------------------------+---------------------------------------+ | "x[index]", "x[index:index]", | Subscription, slicing, call, | | "x(arguments...)", "x.attribute" | attribute reference | +-------------------------------------------------+---------------------------------------+ | "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list | | value...}", "{expressions...}" | display, dictionary display, set | | | display | +-------------------------------------------------+---------------------------------------+ -[ Footnotes ]- [1] While "abs(x%y) < abs(y)" is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double- precision number, in order that "-1e-100 % 1e100" have the same sign as "1e100", the computed result is "-1e-100 + 1e100", which is numerically exactly equal to "1e100". The function "math.fmod()" returns a result whose sign matches the sign of the first argument instead, and so returns "-1e-100" in this case. Which approach is more appropriate depends on the application. [2] If x is very close to an exact integer multiple of y, it’s possible for "x//y" to be one larger than "(x-x%y)//y" due to rounding. In such cases, Python returns the latter result, in order to preserve that "divmod(x,y)[0] * y + x % y" be very close to "x". [3] The Unicode standard distinguishes between *code points* (e.g. U+0041) and *abstract characters* (e.g. “LATIN CAPITAL LETTER A”). While most abstract characters in Unicode are only represented using one code point, there is a number of abstract characters that can in addition be represented using a sequence of more than one code point. For example, the abstract character “LATIN CAPITAL LETTER C WITH CEDILLA” can be represented as a single *precomposed character* at code position U+00C7, or as a sequence of a *base character* at code position U+0043 (LATIN CAPITAL LETTER C), followed by a *combining character* at code position U+0327 (COMBINING CEDILLA). The comparison operators on strings compare at the level of Unicode code points. This may be counter-intuitive to humans. For example, ""\u00C7" == "\u0043\u0327"" is "False", even though both strings represent the same abstract character “LATIN CAPITAL LETTER C WITH CEDILLA”. To compare strings at the level of abstract characters (that is, in a way intuitive to humans), use "unicodedata.normalize()". [4] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the "is" operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [5] The "%" operator is also used for string formatting; the same precedence applies. [6] The power operator "**" binds less tightly than an arithmetic or bitwise unary operator on its right, that is, "2**-1" is "0.5". uwThe "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) aThe power operator ****************** The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is: power ::= (await_expr | primary) ["**" u_expr] Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): "-1**2" results in "-1". The power operator has the same semantics as the built-in "pow()" function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type, and the result is of that type. For int operands, the result has the same type as the operands unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, "10**2" returns "100", but "10**-2" returns "0.01". Raising "0.0" to a negative power results in a "ZeroDivisionError". Raising a negative number to a fractional power results in a "complex" number. (In earlier versions it raised a "ValueError".) ul The "raise" statement ********************* raise_stmt ::= "raise" [expression ["from" expression]] If no expressions are present, "raise" re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a "RuntimeError" exception is raised indicating that this is an error. Otherwise, "raise" evaluates the first expression as the exception object. It must be either a subclass or an instance of "BaseException". If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments. The *type* of the exception is the exception instance’s class, the *value* is the instance itself. A traceback object is normally created automatically when an exception is raised and attached to it as the "__traceback__" attribute, which is writable. You can create an exception and set your own traceback in one step using the "with_traceback()" exception method (which returns the same exception instance, with its traceback set to its argument), like so: raise Exception("foo occurred").with_traceback(tracebackobj) The "from" clause is used for exception chaining: if given, the second *expression* must be another exception class or instance, which will then be attached to the raised exception as the "__cause__" attribute (which is writable). If the raised exception is not handled, both exceptions will be printed: >>> try: ... print(1 / 0) ... except Exception as exc: ... raise RuntimeError("Something bad happened") from exc ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened A similar mechanism works implicitly if an exception is raised inside an exception handler or a "finally" clause: the previous exception is then attached as the new exception’s "__context__" attribute: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Exception chaining can be explicitly suppressed by specifying "None" in the "from" clause: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") from None ... Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement. Changed in version 3.3: "None" is now permitted as "Y" in "raise X from Y". New in version 3.3: The "__suppress_context__" attribute to suppress automatic display of the exception context. aThe "return" statement ********************** return_stmt ::= "return" [expression_list] "return" may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else "None" is substituted. "return" leaves the current function call with the expression list (or "None") as return value. When "return" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the function. In a generator function, the "return" statement indicates that the generator is done and will cause "StopIteration" to be raised. The returned value (if any) is used as an argument to construct "StopIteration" and becomes the "StopIteration.value" attribute. In an asynchronous generator function, an empty "return" statement indicates that the asynchronous generator is done and will cause "StopAsyncIteration" to be raised. A non-empty "return" statement is a syntax error in an asynchronous generator function. uEmulating container types ************************* The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. aShifting operations ******************* The shifting operations have lower priority than the arithmetic operations: shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr These operators accept integers as arguments. They shift the first argument to the left or right by the number of bits given by the second argument. A right shift by *n* bits is defined as floor division by "pow(2,n)". A left shift by *n* bits is defined as multiplication with "pow(2,n)". Note: In the current implementation, the right-hand operand is required to be at most "sys.maxsize". If the right-hand operand is larger than "sys.maxsize" an "OverflowError" exception is raised. aSlicings ******** A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or "del" statements. The syntax for a slicing: slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ] lower_bound ::= expression upper_bound ::= expression stride ::= expression There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice). The semantics for a slicing are as follows. The primary is indexed (using the same "__getitem__()" method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose "start", "stop" and "step" attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting "None" for missing expressions. u~Special Attributes ****************** The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the "dir()" built-in function. object.__dict__ A dictionary or other mapping object used to store an object’s (writable) attributes. instance.__class__ The class to which a class instance belongs. class.__bases__ The tuple of base classes of a class object. definition.__name__ The name of the class, function, method, descriptor, or generator instance. definition.__qualname__ The *qualified name* of the class, function, method, descriptor, or generator instance. New in version 3.3. class.__mro__ This attribute is a tuple of classes that are considered when looking for base classes during method resolution. class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in "__mro__". class.__subclasses__() Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example: >>> int.__subclasses__() [] -[ Footnotes ]- [1] Additional information on these special methods may be found in the Python Reference Manual (Basic customization). [2] As a consequence, the list "[1, 2]" is considered equal to "[1.0, 2.0]", and similarly for tuples. [3] They must have since the parser can’t tell the type of the operands. [4] Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase). [5] To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted. u.Special method names ******************** A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to *operator overloading*, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named "__getitem__()", and "x" is an instance of this class, then "x[i]" is roughly equivalent to "type(x).__getitem__(x, i)". Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically "AttributeError" or "TypeError"). Setting a special method to "None" indicates that the corresponding operation is not available. For example, if a class sets "__iter__()" to "None", the class is not iterable, so calling "iter()" on its instances will raise a "TypeError" (without falling back to "__getitem__()"). [2] When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the "NodeList" interface in the W3C’s Document Object Model.) Basic customization =================== object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. Customizing attribute access ============================ The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access ----------------------------------- For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ------------------------ The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors -------------------- In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ --------- *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* ~~~~~~~~~~~~~~~~~~~~~~~~~~ * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". Customizing class creation ========================== Whenever a class inherits from another class, *__init_subclass__* is called on that class. This way, it is possible to write classes which change the behavior of subclasses. This is closely related to class decorators, but where class decorators only affect the specific class they’re applied to, "__init_subclass__" solely applies to future subclasses of the class defining the method. classmethod object.__init_subclass__(cls) This method is called whenever the containing class is subclassed. *cls* is then the new subclass. If defined as a normal instance method, this method is implicitly converted to a class method. Keyword arguments which are given to a new class are passed to the parent’s class "__init_subclass__". For compatibility with other classes using "__init_subclass__", one should take out the needed keyword arguments and pass the others over to the base class, as in: class Philosopher: def __init_subclass__(cls, default_name, **kwargs): super().__init_subclass__(**kwargs) cls.default_name = default_name class AustralianPhilosopher(Philosopher, default_name="Bruce"): pass The default implementation "object.__init_subclass__" does nothing, but raises an error if it is called with any arguments. Note: The metaclass hint "metaclass" is consumed by the rest of the type machinery, and is never passed to "__init_subclass__" implementations. The actual metaclass (rather than the explicit hint) can be accessed as "type(cls)". New in version 3.6. Metaclasses ----------- By default, classes are constructed using "type()". The class body is executed in a new namespace and the class name is bound locally to the result of "type(name, bases, namespace)". The class creation process can be customized by passing the "metaclass" keyword argument in the class definition line, or by inheriting from an existing class that included such an argument. In the following example, both "MyClass" and "MySubclass" are instances of "Meta": class Meta(type): pass class MyClass(metaclass=Meta): pass class MySubclass(MyClass): pass Any other keyword arguments that are specified in the class definition are passed through to all metaclass operations described below. When a class definition is executed, the following steps occur: * the appropriate metaclass is determined * the class namespace is prepared * the class body is executed * the class object is created Determining the appropriate metaclass ------------------------------------- The appropriate metaclass for a class definition is determined as follows: * if no bases and no explicit metaclass are given, then "type()" is used * if an explicit metaclass is given and it is *not* an instance of "type()", then it is used directly as the metaclass * if an instance of "type()" is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used The most derived metaclass is selected from the explicitly specified metaclass (if any) and the metaclasses (i.e. "type(cls)") of all specified base classes. The most derived metaclass is one which is a subtype of *all* of these candidate metaclasses. If none of the candidate metaclasses meets that criterion, then the class definition will fail with "TypeError". Preparing the class namespace ----------------------------- Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a "__prepare__" attribute, it is called as "namespace = metaclass.__prepare__(name, bases, **kwds)" (where the additional keyword arguments, if any, come from the class definition). If the metaclass has no "__prepare__" attribute, then the class namespace is initialised as an empty ordered mapping. See also: **PEP 3115** - Metaclasses in Python 3000 Introduced the "__prepare__" namespace hook Executing the class body ------------------------ The class body is executed (approximately) as "exec(body, globals(), namespace)". The key difference from a normal call to "exec()" is that lexical scoping allows the class body (including any methods) to reference names from the current and outer scopes when the class definition occurs inside a function. However, even when the class definition occurs inside the function, methods defined inside the class still cannot see names defined at the class scope. Class variables must be accessed through the first parameter of instance or class methods, or through the implicit lexically scoped "__class__" reference described in the next section. Creating the class object ------------------------- Once the class namespace has been populated by executing the class body, the class object is created by calling "metaclass(name, bases, namespace, **kwds)" (the additional keywords passed here are the same as those passed to "__prepare__"). This class object is the one that will be referenced by the zero- argument form of "super()". "__class__" is an implicit closure reference created by the compiler if any methods in a class body refer to either "__class__" or "super". This allows the zero argument form of "super()" to correctly identify the class being defined based on lexical scoping, while the class or instance that was used to make the current call is identified based on the first argument passed to the method. **CPython implementation detail:** In CPython 3.6 and later, the "__class__" cell is passed to the metaclass as a "__classcell__" entry in the class namespace. If present, this must be propagated up to the "type.__new__" call in order for the class to be initialised correctly. Failing to do so will result in a "DeprecationWarning" in Python 3.6, and a "RuntimeError" in Python 3.8. When using the default metaclass "type", or any metaclass that ultimately calls "type.__new__", the following additional customisation steps are invoked after creating the class object: * first, "type.__new__" collects all of the descriptors in the class namespace that define a "__set_name__()" method; * second, all of these "__set_name__" methods are called with the class being defined and the assigned name of that particular descriptor; and * finally, the "__init_subclass__()" hook is called on the immediate parent of the new class in its method resolution order. After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. When a new class is created by "type.__new__", the object provided as the namespace parameter is copied to a new ordered mapping and the original object is discarded. The new copy is wrapped in a read-only proxy, which becomes the "__dict__" attribute of the class object. See also: **PEP 3135** - New super Describes the implicit "__class__" closure reference Uses for metaclasses -------------------- The potential uses for metaclasses are boundless. Some ideas that have been explored include enum, logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. Customizing instance and subclass checks ======================================== The following methods are used to override the default behavior of the "isinstance()" and "issubclass()" built-in functions. In particular, the metaclass "abc.ABCMeta" implements these methods in order to allow the addition of Abstract Base Classes (ABCs) as “virtual base classes” to any class or type (including built-in types), including other ABCs. class.__instancecheck__(self, instance) Return true if *instance* should be considered a (direct or indirect) instance of *class*. If defined, called to implement "isinstance(instance, class)". class.__subclasscheck__(self, subclass) Return true if *subclass* should be considered a (direct or indirect) subclass of *class*. If defined, called to implement "issubclass(subclass, class)". Note that these methods are looked up on the type (metaclass) of a class. They cannot be defined as class methods in the actual class. This is consistent with the lookup of special methods that are called on instances, only in this case the instance is itself a class. See also: **PEP 3119** - Introducing Abstract Base Classes Includes the specification for customizing "isinstance()" and "issubclass()" behavior through "__instancecheck__()" and "__subclasscheck__()", with motivation for this functionality in the context of adding Abstract Base Classes (see the "abc" module) to the language. Emulating callable objects ========================== object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". Emulating container types ========================= The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. Emulating numeric types ======================= The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". With Statement Context Managers =============================== A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Special method lookup ===================== For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary. That behaviour is the reason why the following code raises an exception: >>> class C: ... pass ... >>> c = C() >>> c.__len__ = lambda: 5 >>> len(c) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such as "__hash__()" and "__repr__()" that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself: >>> 1 .__hash__() == hash(1) True >>> int.__hash__() == hash(int) Traceback (most recent call last): File "", line 1, in TypeError: descriptor '__hash__' of 'int' object needs an argument Incorrectly attempting to invoke an unbound method of a class in this way is sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing the instance when looking up special methods: >>> type(1).__hash__(1) == hash(1) True >>> type(int).__hash__(int) == hash(int) True In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the "__getattribute__()" method even of the object’s metaclass: >>> class Meta(type): ... def __getattribute__(*args): ... print("Metaclass getattribute invoked") ... return type.__getattribute__(*args) ... >>> class C(object, metaclass=Meta): ... def __len__(self): ... return 10 ... def __getattribute__(*args): ... print("Class getattribute invoked") ... return object.__getattribute__(*args) ... >>> c = C() >>> c.__len__() # Explicit lookup via instance Class getattribute invoked 10 >>> type(c).__len__(c) # Explicit lookup via type Metaclass getattribute invoked 10 >>> len(c) # Implicit lookup 10 Bypassing the "__getattribute__()" machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class object itself in order to be consistently invoked by the interpreter). u=WString Methods ************** Strings implement all of the common sequence operations, along with the additional methods described below. Strings also support two styles of string formatting, one providing a large degree of flexibility and customization (see "str.format()", Format String Syntax and Custom String Formatting) and the other based on C "printf" style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle (printf-style String Formatting). The Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities (including regular expression support in the "re" module). str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. str.casefold() Return a casefolded copy of the string. Casefolded strings may be used for caseless matching. Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter "'ß'" is equivalent to ""ss"". Since it is already lowercase, "lower()" would do nothing to "'ß'"; "casefold()" converts it to ""ss"". The casefolding algorithm is described in section 3.13 of the Unicode Standard. New in version 3.3. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.encode(encoding="utf-8", errors="strict") Return an encoded version of the string as a bytes object. Default encoding is "'utf-8'". *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Error Handlers. For a list of possible encodings, see section Standard Encodings. Changed in version 3.1: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. str.expandtabs(tabsize=8) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. Note: When formatting a number ("int", "float", "complex", "decimal.Decimal" and subclasses) with the "n" type (ex: "'{:n}'.format(1234)"), the function temporarily sets the "LC_CTYPE" locale to the "LC_NUMERIC" locale to decode "decimal_point" and "thousands_sep" fields of "localeconv()" if they are non-ASCII or longer than 1 byte, and the "LC_NUMERIC" locale is different than the "LC_CTYPE" locale. This temporary change affects other threads. Changed in version 3.6.5: When formatting a number with the "n" type, the function sets temporarily the "LC_CTYPE" locale to the "LC_NUMERIC" locale in some cases. str.format_map(mapping) Similar to "str.format(**mapping)", except that "mapping" is used directly and not copied to a "dict". This is useful if for example "mapping" is a dict subclass: >>> class Default(dict): ... def __missing__(self, key): ... return key ... >>> '{name} was born in {country}'.format_map(Default(name='Guido')) 'Guido was born in country' New in version 3.2. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character "c" is alphanumeric if one of the following returns "True": "c.isalpha()", "c.isdecimal()", "c.isdigit()", or "c.isnumeric()". str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard. str.isdecimal() Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal. str.isidentifier() Return true if the string is a valid identifier according to the language definition, section Identifiers and keywords. Use "keyword.iskeyword()" to test for reserved identifiers such as "def" and "class". str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. str.isnumeric() Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric. str.isprintable() Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when "repr()" is invoked on a string. It has no bearing on the handling of strings written to "sys.stdout" or "sys.stderr".) str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as “Other” or “Separator” and those with bidirectional property being one of “WS”, “B”, or “S”. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. The lowercasing algorithm used is described in section 3.13 of the Unicode Standard. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' static str.maketrans(x[, y[, z]]) This static method returns a translation table usable for "str.translate()". If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or "None". Character keys will then be converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to "None" in the result. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. str.rsplit(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' str.split(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". For example: >>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', ''] If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example: >>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3'] str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless *keepends* is given and true. This method splits on the following line boundaries. In particular, the boundaries are a superset of *universal newlines*. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 3.2: "\v" and "\f" added to list of line boundaries. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' The outermost leading and trailing *chars* argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in *chars*. A similar action takes place on the trailing end. For example: >>> comment_string = '#....... Section 3.2.1 Issue #32 .......' >>> comment_string.strip('.#! ') 'Section 3.2.1 Issue #32' str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that "s.swapcase().swapcase() == s". str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. For example: >>> 'Hello world'.title() 'Hello World' The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." str.translate(table) Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via "__getitem__()", typically a *mapping* or *sequence*. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return "None", to delete the character from the return string; or raise a "LookupError" exception, to map the character to itself. You can use "str.maketrans()" to create a translation map from character-to-character mappings in different formats. See also the "codecs" module for a more flexible approach to custom character mappings. str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "s.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase). The uppercasing algorithm used is described in section 3.13 of the Unicode Standard. str.zfill(width) Return a copy of the string left filled with ASCII "'0'" digits to make a string of length *width*. A leading sign prefix ("'+'"/"'-'") is handled by inserting the padding *after* the sign character rather than before. The original string is returned if *width* is less than or equal to "len(s)". For example: >>> "42".zfill(5) '00042' >>> "-42".zfill(5) '-0042' uw String and Bytes literals ************************* String literals are described by the following lexical definitions: stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F" | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | stringescapeseq longstringitem ::= longstringchar | stringescapeseq shortstringchar ::= longstringchar ::= stringescapeseq ::= "\" bytesliteral ::= bytesprefix(shortbytes | longbytes) bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= longbyteschar ::= bytesescapeseq ::= "\" One syntactic restriction not indicated by these productions is that whitespace is not allowed between the "stringprefix" or "bytesprefix" and the rest of the literal. The source character set is defined by the encoding declaration; it is UTF-8 if no encoding declaration is given in the source file; see section Encoding declarations. In plain English: Both types of literals can be enclosed in matching single quotes ("'") or double quotes ("""). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as *triple-quoted strings*). The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. Bytes literals are always prefixed with "'b'" or "'B'"; they produce an instance of the "bytes" type instead of the "str" type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes. Both string and bytes literals may optionally be prefixed with a letter "'r'" or "'R'"; such strings are called *raw strings* and treat backslashes as literal characters. As a result, in string literals, "'\U'" and "'\u'" escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the "'ur'" syntax is not supported. New in version 3.3: The "'rb'" prefix of raw bytes literals has been added as a synonym of "'br'". New in version 3.3: Support for the unicode legacy literal ("u'value'") was reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases. See **PEP 414** for more information. A string literal with "'f'" or "'F'" in its prefix is a *formatted string literal*; see Formatted string literals. The "'f'" may be combined with "'r'", but not with "'b'" or "'u'", therefore raw formatted strings are possible, but formatted bytes literals are not. In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either "'" or """.) Unless an "'r'" or "'R'" prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\newline" | Backslash and newline ignored | | +-------------------+-----------------------------------+---------+ | "\\" | Backslash ("\") | | +-------------------+-----------------------------------+---------+ | "\'" | Single quote ("'") | | +-------------------+-----------------------------------+---------+ | "\"" | Double quote (""") | | +-------------------+-----------------------------------+---------+ | "\a" | ASCII Bell (BEL) | | +-------------------+-----------------------------------+---------+ | "\b" | ASCII Backspace (BS) | | +-------------------+-----------------------------------+---------+ | "\f" | ASCII Formfeed (FF) | | +-------------------+-----------------------------------+---------+ | "\n" | ASCII Linefeed (LF) | | +-------------------+-----------------------------------+---------+ | "\r" | ASCII Carriage Return (CR) | | +-------------------+-----------------------------------+---------+ | "\t" | ASCII Horizontal Tab (TAB) | | +-------------------+-----------------------------------+---------+ | "\v" | ASCII Vertical Tab (VT) | | +-------------------+-----------------------------------+---------+ | "\ooo" | Character with octal value *ooo* | (1,3) | +-------------------+-----------------------------------+---------+ | "\xhh" | Character with hex value *hh* | (2,3) | +-------------------+-----------------------------------+---------+ Escape sequences only recognized in string literals are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\N{name}" | Character named *name* in the | (4) | | | Unicode database | | +-------------------+-----------------------------------+---------+ | "\uxxxx" | Character with 16-bit hex value | (5) | | | *xxxx* | | +-------------------+-----------------------------------+---------+ | "\Uxxxxxxxx" | Character with 32-bit hex value | (6) | | | *xxxxxxxx* | | +-------------------+-----------------------------------+---------+ Notes: 1. As in Standard C, up to three octal digits are accepted. 2. Unlike in Standard C, exactly two hex digits are required. 3. In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value. 4. Changed in version 3.3: Support for name aliases [1] has been added. 5. Exactly four hex digits are required. 6. Any Unicode character can be encoded this way. Exactly eight hex digits are required. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., *the backslash is left in the result*. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences only recognized in string literals fall into the category of unrecognized escapes for bytes literals. Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning. In some future version of Python they will be a SyntaxError. Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, "r"\""" is a valid string literal consisting of two characters: a backslash and a double quote; "r"\"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw literal cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, *not* as a line continuation. uMSubscriptions ************* A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a "__getitem__()" method. For built-in objects, there are two types of objects that support subscription: If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression list must evaluate to an integer or a slice (as discussed in the following section). The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a "__getitem__()" method that interprets negative indices by adding the length of the sequence to the index (so that "x[-1]" selects the last item of "x"). The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing occurs in the object’s "__getitem__()" method, subclasses overriding this method will need to explicitly add that support. A string’s items are characters. A character is not a separate data type but a string of exactly one character. axTruth Value Testing ******************* Any object can be tested for truth value, for use in an "if" or "while" condition or as operand of the Boolean operations below. By default, an object is considered true unless its class defines either a "__bool__()" method that returns "False" or a "__len__()" method that returns zero, when called with the object. [1] Here are most of the built-in objects considered false: * constants defined to be false: "None" and "False". * zero of any numeric type: "0", "0.0", "0j", "Decimal(0)", "Fraction(0, 1)" * empty sequences and collections: "''", "()", "[]", "{}", "set()", "range(0)" Operations and built-in functions that have a Boolean result always return "0" or "False" for false and "1" or "True" for true, unless otherwise stated. (Important exception: the Boolean operations "or" and "and" always return one of their operands.) u=The "try" statement ******************* The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. uThe standard type hierarchy *************************** Below is a list of the types that are built into Python. Extension modules (written in C, Java, or other languages, depending on the implementation) can define additional types. Future versions of Python may add types to the type hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.), although such additions will often be provided via the standard library instead. Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future. None This type has a single value. There is a single object with this value. This object is accessed through the built-in name "None". It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false. NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name "NotImplemented". Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. See Implementing the arithmetic operations for more details. Ellipsis This type has a single value. There is a single object with this value. This object is accessed through the literal "..." or the built-in name "Ellipsis". Its truth value is true. "numbers.Number" These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers. Python distinguishes between integers, floating point numbers, and complex numbers: "numbers.Integral" These represent elements from the mathematical set of integers (positive and negative). There are two types of integers: Integers ("int") These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2’s complement which gives the illusion of an infinite string of sign bits extending to the left. Booleans ("bool") These represent the truth values False and True. The two objects representing the values "False" and "True" are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ""False"" or ""True"" are returned, respectively. The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers. "numbers.Real" ("float") These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single- precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers. "numbers.Complex" ("complex") These represent complex numbers as a pair of machine-level double precision floating point numbers. The same caveats apply as for floating point numbers. The real and imaginary parts of a complex number "z" can be retrieved through the read-only attributes "z.real" and "z.imag". Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function "len()" returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* is selected by "a[i]". Sequences also support slicing: "a[i:j]" selects all items with index *k* such that *i* "<=" *k* "<" *j*. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support “extended slicing” with a third “step” parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*. Sequences are distinguished according to their mutability: Immutable sequences An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.) The following types are immutable sequences: Strings A string is a sequence of values that represent Unicode code points. All the code points in the range "U+0000 - U+10FFFF" can be represented in a string. Python doesn’t have a "char" type; instead, every code point in the string is represented as a string object with length "1". The built-in function "ord()" converts a code point from its string form to an integer in the range "0 - 10FFFF"; "chr()" converts an integer in the range "0 - 10FFFF" to the corresponding length "1" string object. "str.encode()" can be used to convert a "str" to "bytes" using the given text encoding, and "bytes.decode()" can be used to achieve the opposite. Tuples The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses. Bytes A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256. Bytes literals (like "b'abc'") and the built-in "bytes()" constructor can be used to create bytes objects. Also, bytes objects can be decoded to strings via the "decode()" method. Mutable sequences Mutable sequences can be changed after they are created. The subscription and slicing notations can be used as the target of assignment and "del" (delete) statements. There are currently two intrinsic mutable sequence types: Lists The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.) Byte Arrays A bytearray object is a mutable array. They are created by the built-in "bytearray()" constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable "bytes" objects. The extension module "array" provides an additional example of a mutable sequence type, as does the "collections" module. Set types These represent unordered, finite sets of unique, immutable objects. As such, they cannot be indexed by any subscript. However, they can be iterated over, and the built-in function "len()" returns the number of items in a set. Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. For set elements, the same immutability rules apply as for dictionary keys. Note that numeric types obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0"), only one of them can be contained in a set. There are currently two intrinsic set types: Sets These represent a mutable set. They are created by the built-in "set()" constructor and can be modified afterwards by several methods, such as "add()". Frozen sets These represent an immutable set. They are created by the built-in "frozenset()" constructor. As a frozenset is immutable and *hashable*, it can be used again as an element of another set, or as a dictionary key. Mappings These represent finite sets of objects indexed by arbitrary index sets. The subscript notation "a[k]" selects the item indexed by "k" from the mapping "a"; this can be used in expressions and as the target of assignments or "del" statements. The built-in function "len()" returns the number of items in a mapping. There is currently a single intrinsic mapping type: Dictionaries These represent finite sets of objects indexed by nearly arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key’s hash value to remain constant. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. Dictionaries are mutable; they can be created by the "{...}" notation (see section Dictionary displays). The extension modules "dbm.ndbm" and "dbm.gnu" provide additional examples of mapping types, as does the "collections" module. Callable types These are the types to which the function call operation (see section Calls) can be applied: User-defined functions A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function’s formal parameter list. Special attributes: +---------------------------+---------------------------------+-------------+ | Attribute | Meaning | | +===========================+=================================+=============+ | "__doc__" | The function’s documentation | Writable | | | string, or "None" if | | | | unavailable; not inherited by | | | | subclasses | | +---------------------------+---------------------------------+-------------+ | "__name__" | The function’s name | Writable | +---------------------------+---------------------------------+-------------+ | "__qualname__" | The function’s *qualified name* | Writable | | | New in version 3.3. | | +---------------------------+---------------------------------+-------------+ | "__module__" | The name of the module the | Writable | | | function was defined in, or | | | | "None" if unavailable. | | +---------------------------+---------------------------------+-------------+ | "__defaults__" | A tuple containing default | Writable | | | argument values for those | | | | arguments that have defaults, | | | | or "None" if no arguments have | | | | a default value | | +---------------------------+---------------------------------+-------------+ | "__code__" | The code object representing | Writable | | | the compiled function body. | | +---------------------------+---------------------------------+-------------+ | "__globals__" | A reference to the dictionary | Read-only | | | that holds the function’s | | | | global variables — the global | | | | namespace of the module in | | | | which the function was defined. | | +---------------------------+---------------------------------+-------------+ | "__dict__" | The namespace supporting | Writable | | | arbitrary function attributes. | | +---------------------------+---------------------------------+-------------+ | "__closure__" | "None" or a tuple of cells that | Read-only | | | contain bindings for the | | | | function’s free variables. | | +---------------------------+---------------------------------+-------------+ | "__annotations__" | A dict containing annotations | Writable | | | of parameters. The keys of the | | | | dict are the parameter names, | | | | and "'return'" for the return | | | | annotation, if provided. | | +---------------------------+---------------------------------+-------------+ | "__kwdefaults__" | A dict containing defaults for | Writable | | | keyword-only parameters. | | +---------------------------+---------------------------------+-------------+ Most of the attributes labelled “Writable” check the type of the assigned value. Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. *Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.* Additional information about a function’s definition can be retrieved from its code object; see the description of internal types below. Instance methods An instance method object combines a class, a class instance and any callable object (normally a user-defined function). Special read-only attributes: "__self__" is the class instance object, "__func__" is the function object; "__doc__" is the method’s documentation (same as "__func__.__doc__"); "__name__" is the method name (same as "__func__.__name__"); "__module__" is the name of the module the method was defined in, or "None" if unavailable. Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object. User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object. When an instance method object is created by retrieving a user- defined function object from a class via one of its instances, its "__self__" attribute is the instance, and the method object is said to be bound. The new method’s "__func__" attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the "__func__" attribute of the new instance is not the original method object but its "__func__" attribute. When an instance method object is created by retrieving a class method object from a class or instance, its "__self__" attribute is the class itself, and its "__func__" attribute is the function object underlying the class method. When an instance method object is called, the underlying function ("__func__") is called, inserting the class instance ("__self__") in front of the argument list. For instance, when "C" is a class which contains a definition for a function "f()", and "x" is an instance of "C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)". When an instance method object is derived from a class method object, the “class instance” stored in "__self__" will actually be the class itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to calling "f(C,1)" where "f" is the underlying function. Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non- callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this *only* happens when the function is an attribute of the class. Generator functions A function or method which uses the "yield" statement (see section The yield statement) is called a *generator function*. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s "iterator.__next__()" method will cause the function to execute until it provides a value using the "yield" statement. When the function executes a "return" statement or falls off the end, a "StopIteration" exception is raised and the iterator will have reached the end of the set of values to be returned. Coroutine functions A function or method which is defined using "async def" is called a *coroutine function*. Such a function, when called, returns a *coroutine* object. It may contain "await" expressions, as well as "async with" and "async for" statements. See also the Coroutine Objects section. Asynchronous generator functions A function or method which is defined using "async def" and which uses the "yield" statement is called a *asynchronous generator function*. Such a function, when called, returns an asynchronous iterator object which can be used in an "async for" statement to execute the body of the function. Calling the asynchronous iterator’s "aiterator.__anext__()" method will return an *awaitable* which when awaited will execute until it provides a value using the "yield" expression. When the function executes an empty "return" statement or falls off the end, a "StopAsyncIteration" exception is raised and the asynchronous iterator will have reached the end of the set of values to be yielded. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are "len()" and "math.sin()" ("math" is a standard built-in module). The number and type of the arguments are determined by the C function. Special read- only attributes: "__doc__" is the function’s documentation string, or "None" if unavailable; "__name__" is the function’s name; "__self__" is set to "None" (but see the next item); "__module__" is the name of the module the function was defined in or "None" if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is "alist.append()", assuming *alist* is a list object. In this case, the special read-only attribute "__self__" is set to the object denoted by *alist*. Classes Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override "__new__()". The arguments of the call are passed to "__new__()" and, in the typical case, to "__init__()" to initialize the new instance. Class Instances Instances of arbitrary classes can be made callable by defining a "__call__()" method in their class. Modules Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the "import" statement (see "import"), or by calling functions such as "importlib.import_module()" and built-in "__import__()". A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the "__globals__" attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done). Attribute assignment updates the module’s namespace dictionary, e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1". Predefined (writable) attributes: "__name__" is the module’s name; "__doc__" is the module’s documentation string, or "None" if unavailable; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during module body execution; "__file__" is the pathname of the file from which the module was loaded, if it was loaded from a file. The "__file__" attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file. Special read-only attribute: "__dict__" is the module’s namespace as a dictionary object. **CPython implementation detail:** Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly. Custom classes Custom class types are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., "C.x" is translated to "C.__dict__["x"]" (although there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes. This search of the base classes uses the C3 method resolution order which behaves correctly even in the presence of ‘diamond’ inheritance structures where there are multiple inheritance paths leading back to a common ancestor. Additional details on the C3 MRO used by Python can be found in the documentation accompanying the 2.3 release at https://www.python.org/download/releases/2.3/mro/. When a class attribute reference (for class "C", say) would yield a class method object, it is transformed into an instance method object whose "__self__" attribute is "C". When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section Implementing Descriptors for another way in which attributes retrieved from a class may differ from those actually contained in its "__dict__". Class attribute assignments update the class’s dictionary, never the dictionary of a base class. A class object can be called (see above) to yield a class instance (see below). Special attributes: "__name__" is the class name; "__module__" is the module name in which the class was defined; "__dict__" is the dictionary containing the class’s namespace; "__bases__" is a tuple containing the base classes, in the order of their occurrence in the base class list; "__doc__" is the class’s documentation string, or "None" if undefined; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during class body execution. Class instances A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object, it is transformed into an instance method object whose "__self__" attribute is the instance. Static method and class method objects are also transformed; see above under “Classes”. See section Implementing Descriptors for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class’s "__dict__". If no class attribute is found, and the object’s class has a "__getattr__()" method, that is called to satisfy the lookup. Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. If the class has a "__setattr__()" or "__delattr__()" method, this is called instead of updating the instance dictionary directly. Class instances can pretend to be numbers, sequences, or mappings if they have methods with certain special names. See section Special method names. Special attributes: "__dict__" is the attribute dictionary; "__class__" is the instance’s class. I/O objects (also known as file objects) A *file object* represents an open file. Various shortcuts are available to create file objects: the "open()" built-in function, and also "os.popen()", "os.fdopen()", and the "makefile()" method of socket objects (and perhaps by other functions or methods provided by extension modules). The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized to file objects corresponding to the interpreter’s standard input, output and error streams; they are all open in text mode and therefore follow the interface defined by the "io.TextIOBase" abstract class. Internal types A few types used internally by the interpreter are exposed to the user. Their definitions may change with future versions of the interpreter, but they are mentioned here for completeness. Code objects Code objects represent *byte-compiled* executable Python code, or *bytecode*. The difference between a code object and a function object is that the function object contains an explicit reference to the function’s globals (the module in which it was defined), while a code object contains no context; also the default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time). Unlike function objects, code objects are immutable and contain no references (directly or indirectly) to mutable objects. Special read-only attributes: "co_name" gives the function name; "co_argcount" is the number of positional arguments (including arguments with default values); "co_nlocals" is the number of local variables used by the function (including arguments); "co_varnames" is a tuple containing the names of the local variables (starting with the argument names); "co_cellvars" is a tuple containing the names of local variables that are referenced by nested functions; "co_freevars" is a tuple containing the names of free variables; "co_code" is a string representing the sequence of bytecode instructions; "co_consts" is a tuple containing the literals used by the bytecode; "co_names" is a tuple containing the names used by the bytecode; "co_filename" is the filename from which the code was compiled; "co_firstlineno" is the first line number of the function; "co_lnotab" is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); "co_stacksize" is the required stack size (including local variables); "co_flags" is an integer encoding a number of flags for the interpreter. The following flag bits are defined for "co_flags": bit "0x04" is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit "0x08" is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; bit "0x20" is set if the function is a generator. Future feature declarations ("from __future__ import division") also use bits in "co_flags" to indicate whether a code object was compiled with a particular feature enabled: bit "0x2000" is set if the function was compiled with future division enabled; bits "0x10" and "0x1000" were used in earlier versions of Python. Other bits in "co_flags" are reserved for internal use. If a code object represents a function, the first item in "co_consts" is the documentation string of the function, or "None" if undefined. Frame objects Frame objects represent execution frames. They may occur in traceback objects (see below). Special read-only attributes: "f_back" is to the previous stack frame (towards the caller), or "None" if this is the bottom stack frame; "f_code" is the code object being executed in this frame; "f_locals" is the dictionary used to look up local variables; "f_globals" is used for global variables; "f_builtins" is used for built-in (intrinsic) names; "f_lasti" gives the precise instruction (this is an index into the bytecode string of the code object). Special writable attributes: "f_trace", if not "None", is a function called at the start of each source code line (this is used by the debugger); "f_lineno" is the current line number of the frame — writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno. Frame objects support one method: frame.clear() This method clears all references to local variables held by the frame. Also, if the frame belonged to a generator, the generator is finalized. This helps break reference cycles involving frame objects (for example when catching an exception and storing its traceback for later use). "RuntimeError" is raised if the frame is currently executing. New in version 3.4. Traceback objects Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as the third item of the tuple returned by "sys.exc_info()". When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as "sys.last_traceback". Special read-only attributes: "tb_next" is the next level in the stack trace (towards the frame where the exception occurred), or "None" if there is no next level; "tb_frame" points to the execution frame of the current level; "tb_lineno" gives the line number where the exception occurred; "tb_lasti" indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a "try" statement with no matching except clause or with a finally clause. Slice objects Slice objects are used to represent slices for "__getitem__()" methods. They are also created by the built-in "slice()" function. Special read-only attributes: "start" is the lower bound; "stop" is the upper bound; "step" is the step value; each is "None" if omitted. These attributes can have any type. Slice objects support one method: slice.indices(self, length) This method takes a single integer argument *length* and computes information about the slice that the slice object would describe if applied to a sequence of *length* items. It returns a tuple of three integers; respectively these are the *start* and *stop* indices and the *step* or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices. Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in "staticmethod()" constructor. Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under “User-defined methods”. Class method objects are created by the built-in "classmethod()" constructor. aFunctions ********* Function objects are created by function definitions. The only operation on a function object is to call it: "func(argument-list)". There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types. See Function definitions for more information. u8$Mapping Types — "dict" ********************** A *mapping* object maps *hashable* values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the *dictionary*. (For other containers see the built- in "list", "set", and "tuple" classes, and the "collections" module.) A dictionary’s keys are *almost* arbitrary values. Values that are not *hashable*, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) Dictionaries can be created by placing a comma-separated list of "key: value" pairs within braces, for example: "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}", or by the "dict" constructor. class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an *iterable* object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary. If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument. To illustrate, the following examples all return a dictionary equal to "{"one": 1, "two": 2, "three": 3}": >>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. These are the operations that dictionaries support (and therefore, custom mapping types should support too): len(d) Return the number of items in the dictionary *d*. d[key] Return the item of *d* with key *key*. Raises a "KeyError" if *key* is not in the map. If a subclass of dict defines a method "__missing__()" and *key* is not present, the "d[key]" operation calls that method with the key *key* as argument. The "d[key]" operation then returns or raises whatever is returned or raised by the "__missing__(key)" call. No other operations or methods invoke "__missing__()". If "__missing__()" is not defined, "KeyError" is raised. "__missing__()" must be a method; it cannot be an instance variable: >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 The example above shows part of the implementation of "collections.Counter". A different "__missing__" method is used by "collections.defaultdict". d[key] = value Set "d[key]" to *value*. del d[key] Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not in the map. key in d Return "True" if *d* has a key *key*, else "False". key not in d Equivalent to "not key in d". iter(d) Return an iterator over the keys of the dictionary. This is a shortcut for "iter(d.keys())". clear() Remove all items from the dictionary. copy() Return a shallow copy of the dictionary. classmethod fromkeys(seq[, value]) Create a new dictionary with keys from *seq* and values set to *value*. "fromkeys()" is a class method that returns a new dictionary. *value* defaults to "None". get(key[, default]) Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to "None", so that this method never raises a "KeyError". items() Return a new view of the dictionary’s items ("(key, value)" pairs). See the documentation of view objects. keys() Return a new view of the dictionary’s keys. See the documentation of view objects. pop(key[, default]) If *key* is in the dictionary, remove it and return its value, else return *default*. If *default* is not given and *key* is not in the dictionary, a "KeyError" is raised. popitem() Remove and return an arbitrary "(key, value)" pair from the dictionary. "popitem()" is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling "popitem()" raises a "KeyError". setdefault(key[, default]) If *key* is in the dictionary, return its value. If not, insert *key* with a value of *default* and return *default*. *default* defaults to "None". update([other]) Update the dictionary with the key/value pairs from *other*, overwriting existing keys. Return "None". "update()" accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: "d.update(red=1, blue=2)". values() Return a new view of the dictionary’s values. See the documentation of view objects. Dictionaries compare equal if and only if they have the same "(key, value)" pairs. Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise "TypeError". See also: "types.MappingProxyType" can be used to create a read-only view of a "dict". Dictionary view objects ======================= The objects returned by "dict.keys()", "dict.values()" and "dict.items()" are *view objects*. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data, and support membership tests: len(dictview) Return the number of entries in the dictionary. iter(dictview) Return an iterator over the keys, values or items (represented as tuples of "(key, value)") in the dictionary. Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.items()]". Iterating views while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. x in dictview Return "True" if *x* is in the underlying dictionary’s keys, values or items (in the latter case, *x* should be a "(key, value)" tuple). Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that "(key, value)" pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) For set-like views, all of the operations defined for the abstract base class "collections.abc.Set" are available (for example, "==", "<", or "^"). An example of dictionary view usage: >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.keys() >>> values = dishes.values() >>> # iteration >>> n = 0 >>> for val in values: ... n += val >>> print(n) 504 >>> # keys and values are iterated over in the same order >>> list(keys) ['eggs', 'bacon', 'sausage', 'spam'] >>> list(values) [2, 1, 1, 500] >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> del dishes['sausage'] >>> list(keys) ['spam', 'bacon'] >>> # set operations >>> keys & {'eggs', 'bacon', 'salad'} {'bacon'} >>> keys ^ {'sausage', 'juice'} {'juice', 'sausage', 'bacon', 'spam'} aMethods ******* Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as "append()" on lists) and class instance methods. Built-in methods are described with the types that support them. If you access a method (a function defined in a class namespace) through an instance, you get a special object: a *bound method* (also called *instance method*) object. When called, it will add the "self" argument to the argument list. Bound methods have two special read- only attributes: "m.__self__" is the object on which the method operates, and "m.__func__" is the function implementing the method. Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)". Like function objects, bound method objects support getting arbitrary attributes. However, since method attributes are actually stored on the underlying function object ("meth.__func__"), setting method attributes on bound methods is disallowed. Attempting to set an attribute on a method results in an "AttributeError" being raised. In order to set a method attribute, you need to explicitly set it on the underlying function object: >>> class C: ... def method(self): ... pass ... >>> c = C() >>> c.method.whoami = 'my name is method' # can't set on the method Traceback (most recent call last): File "", line 1, in AttributeError: 'method' object has no attribute 'whoami' >>> c.method.__func__.whoami = 'my name is method' >>> c.method.whoami 'my name is method' See The standard type hierarchy for more information. u$Modules ******* The only special operation on a module is attribute access: "m.name", where *m* is a module and *name* accesses a name defined in *m*’s symbol table. Module attributes can be assigned to. (Note that the "import" statement is not, strictly speaking, an operation on a module object; "import foo" does not require a module object named *foo* to exist, rather it requires an (external) *definition* for a module named *foo* somewhere.) A special attribute of every module is "__dict__". This is the dictionary containing the module’s symbol table. Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the "__dict__" attribute is not possible (you can write "m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can’t write "m.__dict__ = {}"). Modifying "__dict__" directly is not recommended. Modules built into the interpreter are written like this: "". If loaded from a file, they are written as "". uYSequence Types — "list", "tuple", "range" ***************************************** There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections. Common Sequence Operations ========================== The operations in the following table are supported by most sequence types, both mutable and immutable. The "collections.abc.Sequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. This table lists the sequence operations sorted in ascending priority. In the table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are integers and *x* is an arbitrary object that meets any type and value restrictions imposed by *s*. The "in" and "not in" operations have the same priorities as the comparison operations. The "+" (concatenation) and "*" (repetition) operations have the same priority as the corresponding numeric operations. [3] +----------------------------+----------------------------------+------------+ | Operation | Result | Notes | +============================+==================================+============+ | "x in s" | "True" if an item of *s* is | (1) | | | equal to *x*, else "False" | | +----------------------------+----------------------------------+------------+ | "x not in s" | "False" if an item of *s* is | (1) | | | equal to *x*, else "True" | | +----------------------------+----------------------------------+------------+ | "s + t" | the concatenation of *s* and *t* | (6)(7) | +----------------------------+----------------------------------+------------+ | "s * n" or "n * s" | equivalent to adding *s* to | (2)(7) | | | itself *n* times | | +----------------------------+----------------------------------+------------+ | "s[i]" | *i*th item of *s*, origin 0 | (3) | +----------------------------+----------------------------------+------------+ | "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) | +----------------------------+----------------------------------+------------+ | "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) | | | with step *k* | | +----------------------------+----------------------------------+------------+ | "len(s)" | length of *s* | | +----------------------------+----------------------------------+------------+ | "min(s)" | smallest item of *s* | | +----------------------------+----------------------------------+------------+ | "max(s)" | largest item of *s* | | +----------------------------+----------------------------------+------------+ | "s.index(x[, i[, j]])" | index of the first occurrence of | (8) | | | *x* in *s* (at or after index | | | | *i* and before index *j*) | | +----------------------------+----------------------------------+------------+ | "s.count(x)" | total number of occurrences of | | | | *x* in *s* | | +----------------------------+----------------------------------+------------+ Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.) Notes: 1. While the "in" and "not in" operations are used only for simple containment testing in the general case, some specialised sequences (such as "str", "bytes" and "bytearray") also use them for subsequence testing: >>> "gg" in "eggs" True 2. Values of *n* less than "0" are treated as "0" (which yields an empty sequence of the same type as *s*). Note that items in the sequence *s* are not copied; they are referenced multiple times. This often haunts new Python programmers; consider: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] What has happened is that "[[]]" is a one-element list containing an empty list, so all three elements of "[[]] * 3" are references to this single empty list. Modifying any of the elements of "lists" modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]] Further explanation is available in the FAQ entry How do I create a multidimensional list?. 3. If *i* or *j* is negative, the index is relative to the end of sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But note that "-0" is still "0". 4. The slice of *s* from *i* to *j* is defined as the sequence of items with index *k* such that "i <= k < j". If *i* or *j* is greater than "len(s)", use "len(s)". If *i* is omitted or "None", use "0". If *j* is omitted or "None", use "len(s)". If *i* is greater than or equal to *j*, the slice is empty. 5. The slice of *s* from *i* to *j* with step *k* is defined as the sequence of items with index "x = i + n*k" such that "0 <= n < (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k", "i+3*k" and so on, stopping when *j* is reached (but never including *j*). When *k* is positive, *i* and *j* are reduced to "len(s)" if they are greater. When *k* is negative, *i* and *j* are reduced to "len(s) - 1" if they are greater. If *i* or *j* are omitted or "None", they become “end” values (which end depends on the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it is treated like "1". 6. Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below: * if concatenating "str" objects, you can build a list and use "str.join()" at the end or else write to an "io.StringIO" instance and retrieve its value when complete * if concatenating "bytes" objects, you can similarly use "bytes.join()" or "io.BytesIO", or you can do in-place concatenation with a "bytearray" object. "bytearray" objects are mutable and have an efficient overallocation mechanism * if concatenating "tuple" objects, extend a "list" instead * for other types, investigate the relevant class documentation 7. Some sequence types (such as "range") only support item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition. 8. "index" raises "ValueError" when *x* is not found in *s*. Not all implementations support passing the additional arguments *i* and *j*. These arguments allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using "s[i:j].index(x)", only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice. Immutable Sequence Types ======================== The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the "hash()" built-in. This support allows immutable sequences, such as "tuple" instances, to be used as "dict" keys and stored in "set" and "frozenset" instances. Attempting to hash an immutable sequence that contains unhashable values will result in "TypeError". Mutable Sequence Types ====================== The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. Lists ===== Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application). class list([iterable]) Lists may be constructed in several ways: * Using a pair of square brackets to denote the empty list: "[]" * Using square brackets, separating items with commas: "[a]", "[a, b, c]" * Using a list comprehension: "[x for x in iterable]" * Using the type constructor: "list()" or "list(iterable)" The constructor builds a list whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a list, a copy is made and returned, similar to "iterable[:]". For example, "list('abc')" returns "['a', 'b', 'c']" and "list( (1, 2, 3) )" returns "[1, 2, 3]". If no argument is given, the constructor creates a new empty list, "[]". Many other operations also produce lists, including the "sorted()" built-in. Lists implement all of the common and mutable sequence operations. Lists also provide the following additional method: sort(*, key=None, reverse=False) This method sorts the list in place, using only "<" comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state). "sort()" accepts two arguments that can only be passed by keyword (keyword-only arguments): *key* specifies a function of one argument that is used to extract a comparison key from each list element (for example, "key=str.lower"). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of "None" means that list items are sorted directly without calculating a separate key value. The "functools.cmp_to_key()" utility is available to convert a 2.x style *cmp* function to a *key* function. *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use "sorted()" to explicitly request a new sorted list instance). The "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. Tuples ====== Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the "enumerate()" built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a "set" or "dict" instance). class tuple([iterable]) Tuples may be constructed in a number of ways: * Using a pair of parentheses to denote the empty tuple: "()" * Using a trailing comma for a singleton tuple: "a," or "(a,)" * Separating items with commas: "a, b, c" or "(a, b, c)" * Using the "tuple()" built-in: "tuple()" or "tuple(iterable)" The constructor builds a tuple whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a tuple, it is returned unchanged. For example, "tuple('abc')" returns "('a', 'b', 'c')" and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument is given, the constructor creates a new empty tuple, "()". Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, "f(a, b, c)" is a function call with three arguments, while "f((a, b, c))" is a function call with a 3-tuple as the sole argument. Tuples implement all of the common sequence operations. For heterogeneous collections of data where access by name is clearer than access by index, "collections.namedtuple()" may be a more appropriate choice than a simple tuple object. Ranges ====== The "range" type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in "for" loops. class range(stop) class range(start, stop[, step]) The arguments to the range constructor must be integers (either built-in "int" or any object that implements the "__index__" special method). If the *step* argument is omitted, it defaults to "1". If the *start* argument is omitted, it defaults to "0". If *step* is zero, "ValueError" is raised. For a positive *step*, the contents of a range "r" are determined by the formula "r[i] = start + step*i" where "i >= 0" and "r[i] < stop". For a negative *step*, the contents of the range are still determined by the formula "r[i] = start + step*i", but the constraints are "i >= 0" and "r[i] > stop". A range object will be empty if "r[0]" does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices. Ranges containing absolute values larger than "sys.maxsize" are permitted but some features (such as "len()") may raise "OverflowError". Range examples: >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1, 11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] >>> list(range(0, 10, 3)) [0, 3, 6, 9] >>> list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> list(range(0)) [] >>> list(range(1, 0)) [] Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern). start The value of the *start* parameter (or "0" if the parameter was not supplied) stop The value of the *stop* parameter step The value of the *step* parameter (or "1" if the parameter was not supplied) The advantage of the "range" type over a regular "list" or "tuple" is that a "range" object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the "start", "stop" and "step" values, calculating individual items and subranges as needed). Range objects implement the "collections.abc.Sequence" ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range): >>> r = range(0, 20, 2) >>> r range(0, 20, 2) >>> 11 in r False >>> 10 in r True >>> r.index(10) 5 >>> r[5] 10 >>> r[:5] range(0, 10, 2) >>> r[-1] 18 Testing range objects for equality with "==" and "!=" compares them as sequences. That is, two range objects are considered equal if they represent the same sequence of values. (Note that two range objects that compare equal might have different "start", "stop" and "step" attributes, for example "range(0) == range(2, 1, 3)" or "range(0, 3, 2) == range(0, 4, 2)".) Changed in version 3.2: Implement the Sequence ABC. Support slicing and negative indices. Test "int" objects for membership in constant time instead of iterating through all items. Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define (instead of comparing based on object identity). New in version 3.3: The "start", "stop" and "step" attributes. See also: * The linspace recipe shows how to implement a lazy version of range suitable for floating point applications. usMutable Sequence Types ********************** The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. a~Unary arithmetic and bitwise operations *************************************** All unary arithmetic and bitwise operations have the same priority: u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr The unary "-" (minus) operator yields the negation of its numeric argument. The unary "+" (plus) operator yields its numeric argument unchanged. The unary "~" (invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of "x" is defined as "-(x+1)". It only applies to integral numbers. In all three cases, if the argument does not have the proper type, a "TypeError" exception is raised. uThe "while" statement ********************* The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. u& The "with" statement ******************** The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. a,The "yield" statement ********************* yield_stmt ::= yield_expression A "yield" statement is semantically equivalent to a yield expression. The yield statement can be used to omit the parentheses that would otherwise be required in the equivalent yield expression statement. For example, the yield statements yield yield from are equivalent to the yield expression statements (yield ) (yield from ) Yield expressions and statements are only used when defining a *generator* function, and are only used in the body of the generator function. Using yield in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. For full details of "yield" semantics, refer to the Yield expressions section. )MassertZ assignmentzatom-identifiersz atom-literalszattribute-accesszattribute-referencesZ augassignZbinaryZbitwisezbltin-code-objectszbltin-ellipsis-objectzbltin-null-objectzbltin-type-objectsZbooleansbreakzcallable-typesZcallsclassZ comparisonsZcompoundzcontext-managerscontinueZ conversionsZ customizationZdebuggerdeldictzdynamic-featureselse exceptionsZ execmodelZ exprlistsZfloatingforZ formatstringsZfunctionglobalz id-classesZ identifiersifZ imaginaryimportinZintegerslambdaZlistsZnamingnonlocalZnumbersz numeric-typesZobjectszoperator-summarypassZpowerraisereturnzsequence-typesZshiftingZslicingsZ specialattrsZ specialnameszstring-methodsZstringsZ subscriptionstruthtrytypesZtypesfunctionsZ typesmappingZ typesmethodsZ typesmodulesZtypesseqztypesseq-mutableZunarywhilewithyieldN)Ztopicsrr)/usr/lib64/python3.6/pydoc_data/topics.pys0't("@X   1 =`u >8,LC%GW*+0$.LA0"i#h3U?f9qg9%> JPKr|0]'__pycache__/topics.cpython-36.opt-2.pycnu[3 \ N@sdddddddddd d d d d ddddddddddddddddddd d!d"d#d$d%d&dd'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLMZdMS)NauThe "assert" statement ********************** Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= "assert" expression ["," expression] The simple form, "assert expression", is equivalent to if __debug__: if not expression: raise AssertionError The extended form, "assert expression1, expression2", is equivalent to if __debug__: if not expression1: raise AssertionError(expression2) These equivalences assume that "__debug__" and "AssertionError" refer to the built-in variables with those names. In the current implementation, the built-in variable "__debug__" is "True" under normal circumstances, "False" when optimization is requested (command line option "-O"). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace. Assignments to "__debug__" are illegal. The value for the built-in variable is determined when the interpreter starts. us+Assignment statements ********************* Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: assignment_stmt ::= (target_list "=")+ (starred_expression | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" [target_list] ")" | "[" [target_list] "]" | attributeref | subscription | slicing | "*" target (See section Primaries for the syntax definitions for *attributeref*, *subscription*, and *slicing*.) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy). Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows. * If the target list is a single target with no trailing comma, optionally in parentheses, the object is assigned to that target. * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. * If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be an iterable with at least as many items as there are targets in the target list, minus one. The first items of the iterable are assigned, from left to right, to the targets before the starred target. The final items of the iterable are assigned to the targets after the starred target. A list of the remaining items in the iterable is then assigned to the starred target (the list can be empty). * Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): * If the name does not occur in a "global" or "nonlocal" statement in the current code block: the name is bound to the object in the current local namespace. * Otherwise: the name is bound to the object in the global namespace or the outer namespace determined by "nonlocal", respectively. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, "TypeError" is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily "AttributeError"). Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression, "a.x" can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS target "a.x" is always set as an instance attribute, creating it if necessary. Thus, the two occurrences of "a.x" do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment: class Cls: x = 3 # class variable inst = Cls() inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3 This description does not necessarily apply to descriptor attributes, such as properties created with "property()". * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list), the subscript must yield an integer. If it is negative, the sequence’s length is added to it. The resulting value must be a nonnegative integer less than the sequence’s length, and the sequence is asked to assign the assigned object to its item with that index. If the index is out of range, "IndexError" is raised (assignment to a subscripted sequence cannot add new items to a list). If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping’s key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed). For user-defined objects, the "__setitem__()" method is called with appropriate arguments. * If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence’s length. The bounds should evaluate to integers. If either bound is negative, the sequence’s length is added to it. The resulting bounds are clipped to lie between zero and the sequence’s length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it. **CPython implementation detail:** In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages. Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example "a, b = b, a" swaps two variables), overlaps *within* the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints "[0, 2]": x = [0, 1] i = 0 i, x[i] = 1, 2 # i is updated, then x[i] is updated print(x) See also: **PEP 3132** - Extended Iterable Unpacking The specification for the "*target" feature. Augmented assignment statements =============================== Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. Annotated assignment statements =============================== Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement: annotated_assignment_stmt ::= augtarget ":" expression ["=" expression] The difference from normal Assignment statements is that only single target and only single right hand side value is allowed. For simple names as assignment targets, if in class or module scope, the annotations are evaluated and stored in a special class or module attribute "__annotations__" that is a dictionary mapping from variable names (mangled if private) to evaluated annotations. This attribute is writable and is automatically created at the start of class or module body execution, if annotations are found statically. For expressions as assignment targets, the annotations are evaluated if in class or module scope, but not stored. If a name is annotated in a function scope, then this name is local for that scope. Annotations are never evaluated and stored in function scopes. If the right hand side is present, an annotated assignment performs the actual assignment before evaluating annotations (where applicable). If the right hand side is not present for an expression target, then the interpreter evaluates the target except for the last "__setitem__()" or "__setattr__()" call. See also: **PEP 526** - Syntax for Variable Annotations The proposal that added syntax for annotating the types of variables (including class variables and instance variables), instead of expressing them through comments. **PEP 484** - Type hints The proposal that added the "typing" module to provide a standard syntax for type annotations that can be used in static analysis tools and IDEs. aIdentifiers (Names) ******************* An identifier occurring as an atom is a name. See section 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. **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. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier "__spam" occurring in a class named "Ham" will be transformed to "_Ham__spam". This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done. u Literals ******** Python supports string and bytes literals and various numeric literals: literal ::= stringliteral | bytesliteral | integer | floatnumber | imagnumber Evaluation of a literal yields an object of the given type (string, bytes, integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section Literals for details. 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. u-Customizing attribute access **************************** The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access =================================== For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ======================== The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors ==================== In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ ========= *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* -------------------------- * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". aAttribute references ******************** An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the "__getattr__()" method. If this attribute is not available, the exception "AttributeError" is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. aAugmented assignment statements ******************************* Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions of the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. Unlike normal assignments, augmented assignments evaluate the left- hand side *before* evaluating the right-hand side. For example, "a[i] += f(x)" first looks-up "a[i]", then it evaluates "f(x)" and performs the addition, and lastly, it writes the result back to "a[i]". With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. uj Binary arithmetic operations **************************** The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non- numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr The "*" (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. The "@" (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. New in version 3.5. The "/" (division) and "//" (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the "ZeroDivisionError" exception. The "%" (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the "ZeroDivisionError" exception. The arguments may be floating point numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 + 0.34".) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [1]. The floor division and modulo operators are connected by the following identity: "x == (x//y)*y + (x%y)". Floor division and modulo are also connected with the built-in function "divmod()": "divmod(x, y) == (x//y, x%y)". [2]. In addition to performing the modulo operation on numbers, the "%" operator is also overloaded by string objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section printf-style String Formatting. The floor division operator, the modulo operator, and the "divmod()" function are not defined for complex numbers. Instead, convert to a floating point number using the "abs()" function if appropriate. The "+" (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both be sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The "-" (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. a$Binary bitwise operations ************************* Each of the three bitwise operations has a different priority level: and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr The "&" operator yields the bitwise AND of its arguments, which must be integers. The "^" operator yields the bitwise XOR (exclusive OR) of its arguments, which must be integers. The "|" operator yields the bitwise (inclusive) OR of its arguments, which must be integers. uxCode Objects ************ Code objects are used by the implementation to represent “pseudo- compiled” executable Python code such as a function body. They differ from function objects because they don’t contain a reference to their global execution environment. Code objects are returned by the built- in "compile()" function and can be extracted from function objects through their "__code__" attribute. See also the "code" module. A code object can be executed or evaluated by passing it (instead of a source string) to the "exec()" or "eval()" built-in functions. See The standard type hierarchy for more information. a.The Ellipsis Object ******************* This object is commonly used by slicing (see Slicings). It supports no special operations. There is exactly one ellipsis object, named "Ellipsis" (a built-in name). "type(Ellipsis)()" produces the "Ellipsis" singleton. It is written as "Ellipsis" or "...". uThe Null Object *************** This object is returned by functions that don’t explicitly return a value. It supports no special operations. There is exactly one null object, named "None" (a built-in name). "type(None)()" produces the same singleton. It is written as "None". u5Type Objects ************ Type objects represent the various object types. An object’s type is accessed by the built-in function "type()". There are no special operations on types. The standard module "types" defines names for all standard built-in types. Types are written like this: "". aBoolean operations ****************** or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: "False", "None", numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. User-defined objects can customize their truth value by providing a "__bool__()" method. The operator "not" yields "True" if its argument is false, "False" otherwise. The expression "x and y" first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The expression "x or y" first evaluates *x*; if *x* is true, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. Note that neither "and" nor "or" restrict the value and type they return to "False" and "True", but rather return the last evaluated argument. This is sometimes useful, e.g., if "s" is a string that should be replaced by a default value if it is empty, the expression "s or 'foo'" yields the desired value. Because "not" has to create a new value, it returns a boolean value regardless of the type of its argument (for example, "not 'foo'" produces "False" rather than "''".) a$The "break" statement ********************* break_stmt ::= "break" "break" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional "else" clause if the loop has one. If a "for" loop is terminated by "break", the loop control target keeps its current value. When "break" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the loop. uEmulating callable objects ************************** object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". uCCalls ***** A call calls a callable object (e.g., a *function*) with a possibly empty series of *arguments*: call ::= primary "(" [argument_list [","] | comprehension] ")" argument_list ::= positional_arguments ["," starred_and_keywords] ["," keywords_arguments] | starred_and_keywords ["," keywords_arguments] | keywords_arguments positional_arguments ::= ["*"] expression ("," ["*"] expression)* starred_and_keywords ::= ("*" expression | keyword_item) ("," "*" expression | "," keyword_item)* keywords_arguments ::= (keyword_item | "**" expression) ("," keyword_item | "," "**" expression)* keyword_item ::= identifier "=" expression An optional trailing comma may be present after the positional and keyword arguments but does not affect the semantics. The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a "__call__()" method are callable). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal *parameter* lists. If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a "TypeError" exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is "None", it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don’t specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a "TypeError" exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. **CPython implementation detail:** An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use "PyArg_ParseTuple()" to parse their arguments. If there are more positional arguments than there are formal parameter slots, a "TypeError" exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments). If any keyword argument does not correspond to a formal parameter name, a "TypeError" exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments. If the syntax "*expression" appears in the function call, "expression" must evaluate to an *iterable*. Elements from these iterables are treated as if they were additional positional arguments. For the call "f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, *yM*, this is equivalent to a call with M+4 positional arguments *x1*, *x2*, *y1*, …, *yM*, *x3*, *x4*. A consequence of this is that although the "*expression" syntax may appear *after* explicit keyword arguments, it is processed *before* the keyword arguments (and any "**expression" arguments – see below). So: >>> def f(a, b): ... print(a, b) ... >>> f(b=1, *(2,)) 2 1 >>> f(a=1, *(2,)) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'a' >>> f(1, *(2,)) 1 2 It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise. If the syntax "**expression" appears in the function call, "expression" must evaluate to a *mapping*, the contents of which are treated as additional keyword arguments. If a keyword is already present (as an explicit keyword argument, or from another unpacking), a "TypeError" exception is raised. Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Changed in version 3.5: Function calls accept any number of "*" and "**" unpackings, positional arguments may follow iterable unpackings ("*"), and keyword arguments may follow dictionary unpackings ("**"). Originally proposed by **PEP 448**. A call always returns some value, possibly "None", unless it raises an exception. How this value is computed depends on the type of the callable object. If it is— a user-defined function: The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a "return" statement, this specifies the return value of the function call. a built-in function or method: The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods. a class object: A new instance of that class is returned. a class instance method: The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument. a class instance: The class must define a "__call__()" method; the effect is then the same as if that method was called. u Class definitions ***************** A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. u4)Comparisons *********** Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like "a < b < c" have the interpretation that is conventional in mathematics: comparison ::= or_expr (comp_operator or_expr)* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in" Comparisons yield boolean values: "True" or "False". Comparisons can be chained arbitrarily, e.g., "x < y <= z" is equivalent to "x < y and y <= z", except that "y" is evaluated only once (but in both cases "z" is not evaluated at all when "x < y" is found to be false). Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and *op1*, *op2*, …, *opN* are comparison operators, then "a op1 b op2 c ... y opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except that each expression is evaluated at most once. Note that "a op1 b op2 c" doesn’t imply any kind of comparison between *a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though perhaps not pretty). Value comparisons ================= The operators "<", ">", "==", ">=", "<=", and "!=" compare the values of two objects. The objects do not need to have the same type. Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object’s value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation. Because all types are (direct or indirect) subtypes of "object", they inherit the default comparison behavior from "object". Types can customize their comparison behavior by implementing *rich comparison methods* like "__lt__()", described in Basic customization. The default behavior for equality comparison ("==" and "!=") is based on the identity of the objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of instances with different identities results in inequality. A motivation for this default behavior is the desire that all objects should be reflexive (i.e. "x is y" implies "x == y"). A default order comparison ("<", ">", "<=", and ">=") is not provided; an attempt raises "TypeError". A motivation for this default behavior is the lack of a similar invariant as for equality. The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that. The following list describes the comparison behavior of the most important built-in types. * Numbers of built-in numeric types (Numeric Types — int, float, complex) and of the standard library types "fractions.Fraction" and "decimal.Decimal" can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision. The not-a-number values "float('NaN')" and "Decimal('NaN')" are special. They are identical to themselves ("x is x" is true) but are not equal to themselves ("x == x" is false). Additionally, comparing any number to a not-a-number value will return "False". For example, both "3 < float('NaN')" and "float('NaN') < 3" will return "False". * Binary sequences (instances of "bytes" or "bytearray") can be compared within and across their types. They compare lexicographically using the numeric values of their elements. * Strings (instances of "str") compare lexicographically using the numerical Unicode code points (the result of the built-in function "ord()") of their characters. [3] Strings and binary sequences cannot be directly compared. * Sequences (instances of "tuple", "list", or "range") can be compared only within each of their types, with the restriction that ranges do not support order comparison. Equality comparison across these types results in inequality, and ordering comparison across these types raises "TypeError". Sequences compare lexicographically using comparison of corresponding elements, whereby reflexivity of the elements is enforced. In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element "x", "x == x" is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison, and may be surprising: The non-reflexive not-a-number values for example result in the following comparison behavior when used in a list: >>> nan = float('NaN') >>> nan is nan True >>> nan == nan False <-- the defined non-reflexive behavior of NaN >>> [nan] == [nan] True <-- list enforces reflexivity and tests identity first Lexicographical comparison between built-in collections works as follows: * For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, "[1,2] == (1,2)" is false because the type is not the same). * Collections that support order comparison are ordered the same as their first unequal elements (for example, "[1,2,x] <= [1,2,y]" has the same value as "x <= y"). If a corresponding element does not exist, the shorter collection is ordered first (for example, "[1,2] < [1,2,3]" is true). * Mappings (instances of "dict") compare equal if and only if they have equal *(key, value)* pairs. Equality comparison of the keys and values enforces reflexivity. Order comparisons ("<", ">", "<=", and ">=") raise "TypeError". * Sets (instances of "set" or "frozenset") can be compared within and across their types. They define order comparison operators to mean subset and superset tests. Those relations do not define total orderings (for example, the two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one another, nor supersets of one another). Accordingly, sets are not appropriate arguments for functions which depend on total ordering (for example, "min()", "max()", and "sorted()" produce undefined results given a list of sets as inputs). Comparison of sets enforces reflexivity of its elements. * Most other built-in types have no comparison methods implemented, so they inherit the default comparison behavior. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: * Equality comparison should be reflexive. In other words, identical objects should compare equal: "x is y" implies "x == y" * Comparison should be symmetric. In other words, the following expressions should have the same result: "x == y" and "y == x" "x != y" and "y != x" "x < y" and "y > x" "x <= y" and "y >= x" * Comparison should be transitive. The following (non-exhaustive) examples illustrate that: "x > y and y > z" implies "x > z" "x < y and y <= z" implies "x < z" * Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: "x == y" and "not x != y" "x < y" and "not x >= y" (for total ordering) "x > y" and "not x <= y" (for total ordering) The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the "total_ordering()" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. Python does not enforce these consistency rules. In fact, the not-a-number values are an example for not following these rules. Membership test operations ========================== The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. Object identity is determined using the "id()" function. "x is not y" yields the inverse truth value. [4] uxeCompound statements ******************* Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The "if", "while" and "for" statements implement traditional control flow constructs. "try" specifies exception handlers and/or cleanup code for a group of statements, while the "with" statement allows the execution of initialization and finalization code around a block of code. Function and class definitions are also syntactically compound statements. A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. Only the latter form of a suite can contain nested compound statements; the following is illegal, mostly because it wouldn’t be clear to which "if" clause a following "else" clause would belong: if test1: if test2: print(x) Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the "print()" calls are executed: if x < y < z: print(x); print(y); print(z) Summarizing: compound_stmt ::= if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | async_with_stmt | async_for_stmt | async_funcdef suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT statement ::= stmt_list NEWLINE | compound_stmt stmt_list ::= simple_stmt (";" simple_stmt)* [";"] Note that statements always end in a "NEWLINE" possibly followed by a "DEDENT". Also note that optional continuation clauses always begin with a keyword that cannot start a statement, thus there are no ambiguities (the ‘dangling "else"’ problem is solved in Python by requiring nested "if" statements to be indented). The formatting of the grammar rules in the following sections places each clause on a separate line for clarity. The "if" statement ================== The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. The "while" statement ===================== The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. The "for" statement =================== The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) The "try" statement =================== The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. The "with" statement ==================== The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Function definitions ==================== A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. Class definitions ================= A class definition defines a class object (see section The standard type hierarchy): classdef ::= [decorators] "class" classname [inheritance] ":" suite inheritance ::= "(" [argument_list] ")" classname ::= identifier A class definition is an executable statement. The inheritance list usually gives a list of base classes (see Metaclasses for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class "object"; hence, class Foo: pass is equivalent to class Foo(object): pass The class’s suite is then executed in a new execution frame (see Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains mostly function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [3] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. The order in which attributes are defined in the class body is preserved in the new class’s "__dict__". Note that this is reliable only right after the class is created and only for classes that were defined using the definition syntax. Class creation can be customized heavily using metaclasses. Classes can also be decorated: just like when decorating functions, @f1(arg) @f2 class Foo: pass is roughly equivalent to class Foo: pass Foo = f1(arg)(f2(Foo)) The evaluation rules for the decorator expressions are the same as for function decorators. The result is then bound to the class name. **Programmer’s note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with "self.name = value". Both class and instance attributes are accessible through the notation “"self.name"”, and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. Descriptors can be used to create instance variables with different implementation details. See also: **PEP 3115** - Metaclasses in Python 3000 The proposal that changed the declaration of metaclasses to the current syntax, and the semantics for how classes with metaclasses are constructed. **PEP 3129** - Class Decorators The proposal that added class decorators. Function and method decorators were introduced in **PEP 318**. Coroutines ========== New in version 3.5. Coroutine function definition ----------------------------- async_funcdef ::= [decorators] "async" "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite Execution of Python coroutines can be suspended and resumed at many points (see *coroutine*). In the body of a coroutine, any "await" and "async" identifiers become reserved keywords; "await" expressions, "async for" and "async with" can only be used in coroutine bodies. Functions defined with "async def" syntax are always coroutine functions, even if they do not contain "await" or "async" keywords. It is a "SyntaxError" to use "yield from" expressions in "async def" coroutines. An example of a coroutine function: async def func(param1, param2): do_stuff() await some_coroutine() The "async for" statement ------------------------- async_for_stmt ::= "async" for_stmt An *asynchronous iterable* is able to call asynchronous code in its *iter* implementation, and *asynchronous iterator* can call asynchronous code in its *next* method. The "async for" statement allows convenient iteration over asynchronous iterators. The following code: async for TARGET in ITER: BLOCK else: BLOCK2 Is semantically equivalent to: iter = (ITER) iter = type(iter).__aiter__(iter) running = True while running: try: TARGET = await type(iter).__anext__(iter) except StopAsyncIteration: running = False else: BLOCK else: BLOCK2 See also "__aiter__()" and "__anext__()" for details. It is a "SyntaxError" to use "async for" statement outside of an "async def" function. The "async with" statement -------------------------- async_with_stmt ::= "async" with_stmt An *asynchronous context manager* is a *context manager* that is able to suspend execution in its *enter* and *exit* methods. The following code: async with EXPR as VAR: BLOCK Is semantically equivalent to: mgr = (EXPR) aexit = type(mgr).__aexit__ aenter = type(mgr).__aenter__(mgr) VAR = await aenter try: BLOCK except: if not await aexit(mgr, *sys.exc_info()): raise else: await aexit(mgr, None, None, None) See also "__aenter__()" and "__aexit__()" for details. It is a "SyntaxError" to use "async with" statement outside of an "async def" function. See also: **PEP 492** - Coroutines with async and await syntax The proposal that made coroutines a proper standalone concept in Python, and added supporting syntax. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] A string literal appearing as the first statement in the function body is transformed into the function’s "__doc__" attribute and therefore the function’s *docstring*. [3] A string literal appearing as the first statement in the class body is transformed into the namespace’s "__doc__" item and therefore the class’s *docstring*. uWith Statement Context Managers ******************************* A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. aThe "continue" statement ************************ continue_stmt ::= "continue" "continue" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition or "finally" clause within that loop. It continues with the next cycle of the nearest enclosing loop. When "continue" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really starting the next loop cycle. uArithmetic conversions ********************** When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type,” this means that the operator implementation for built-in types works as follows: * If either argument is a complex number, the other is converted to complex; * otherwise, if either argument is a floating point number, the other is converted to floating point; * otherwise, both must be integers and no conversion is necessary. Some additional rules apply for certain operators (e.g., a string as a left argument to the ‘%’ operator). Extensions must define their own conversion behavior. u3Basic customization ******************* object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. uiF"pdb" — The Python Debugger *************************** **Source code:** Lib/pdb.py ====================================================================== The module "pdb" defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible – it is actually defined as the class "Pdb". This is currently undocumented but easily understood by reading the source. The extension interface uses the modules "bdb" and "cmd". The debugger’s prompt is "(Pdb)". Typical usage to run a program under control of the debugger is: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') > (0)?() (Pdb) continue > (1)?() (Pdb) continue NameError: 'spam' > (1)?() (Pdb) Changed in version 3.3: Tab-completion via the "readline" module is available for commands and command arguments, e.g. the current global and local names are offered as arguments of the "p" command. "pdb.py" can also be invoked as a script to debug other scripts. For example: python3 -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post- mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit. New in version 3.2: "pdb.py" now accepts a "-c" option that executes commands as if given in a ".pdbrc" file, see Debugger Commands. The typical usage to break into the debugger from a running program is to insert import pdb; pdb.set_trace() at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the "continue" command. The typical usage to inspect a crashed program is: >>> import pdb >>> import mymodule >>> mymodule.test() Traceback (most recent call last): File "", line 1, in File "./mymodule.py", line 4, in test test2() File "./mymodule.py", line 3, in test2 print(spam) NameError: spam >>> pdb.pm() > ./mymodule.py(3)test2() -> print(spam) (Pdb) The module defines the following functions; each enters the debugger in a slightly different way: pdb.run(statement, globals=None, locals=None) Execute the *statement* (given as a string or a code object) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type "continue", or you can step through the statement using "step" or "next" (all these commands are explained below). The optional *globals* and *locals* arguments specify the environment in which the code is executed; by default the dictionary of the module "__main__" is used. (See the explanation of the built-in "exec()" or "eval()" functions.) pdb.runeval(expression, globals=None, locals=None) Evaluate the *expression* (given as a string or a code object) under debugger control. When "runeval()" returns, it returns the value of the expression. Otherwise this function is similar to "run()". pdb.runcall(function, *args, **kwds) Call the *function* (a function or method object, not a string) with the given arguments. When "runcall()" returns, it returns whatever the function call returned. The debugger prompt appears as soon as the function is entered. pdb.set_trace() Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). pdb.post_mortem(traceback=None) Enter post-mortem debugging of the given *traceback* object. If no *traceback* is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used). pdb.pm() Enter post-mortem debugging of the traceback found in "sys.last_traceback". The "run*" functions and "set_trace()" are aliases for instantiating the "Pdb" class and calling the method of the same name. If you want to access further features, you have to do this yourself: class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True) "Pdb" is the debugger class. The *completekey*, *stdin* and *stdout* arguments are passed to the underlying "cmd.Cmd" class; see the description there. The *skip* argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] By default, Pdb sets a handler for the SIGINT signal (which is sent when the user presses "Ctrl-C" on the console) when you give a "continue" command. This allows you to break into the debugger again by pressing "Ctrl-C". If you want Pdb not to touch the SIGINT handler, set *nosigint* to true. The *readrc* argument defaults to true and controls whether Pdb will load .pdbrc files from the filesystem. Example call to enable tracing with *skip*: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 3.1: The *skip* argument. New in version 3.2: The *nosigint* argument. Previously, a SIGINT handler was never set by Pdb. Changed in version 3.6: The *readrc* argument. run(statement, globals=None, locals=None) runeval(expression, globals=None, locals=None) runcall(function, *args, **kwds) set_trace() See the documentation for the functions explained above. Debugger Commands ================= The commands recognized by the debugger are listed below. Most commands can be abbreviated to one or two letters as indicated; e.g. "h(elp)" means that either "h" or "help" can be used to enter the help command (but not "he" or "hel", nor "H" or "Help" or "HELP"). Arguments to commands must be separated by whitespace (spaces or tabs). Optional arguments are enclosed in square brackets ("[]") in the command syntax; the square brackets must not be typed. Alternatives in the command syntax are separated by a vertical bar ("|"). Entering a blank line repeats the last command entered. Exception: if the last command was a "list" command, the next 11 lines are listed. Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point ("!"). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debugger’s state is not changed. The debugger supports aliases. Aliases can have parameters which allows one a certain level of adaptability to the context under examination. Multiple commands may be entered on a single line, separated by ";;". (A single ";" is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ";;" pair, even if it is in the middle of a quoted string. If a file ".pdbrc" exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file. Changed in version 3.2: ".pdbrc" can now contain commands that continue debugging, such as "continue" or "next". Previously, these commands had no effect. h(elp) [command] Without argument, print the list of available commands. With a *command* as argument, print help about that command. "help pdb" displays the full documentation (the docstring of the "pdb" module). Since the *command* argument must be an identifier, "help exec" must be entered to get help on the "!" command. w(here) Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands. d(own) [count] Move the current frame *count* (default one) levels down in the stack trace (to a newer frame). u(p) [count] Move the current frame *count* (default one) levels up in the stack trace (to an older frame). b(reak) [([filename:]lineno | function) [, condition]] With a *lineno* argument, set a break there in the current file. With a *function* argument, set a break at the first executable statement within that function. The line number may be prefixed with a filename and a colon, to specify a breakpoint in another file (probably one that hasn’t been loaded yet). The file is searched on "sys.path". Note that each breakpoint is assigned a number to which all the other breakpoint commands refer. If a second argument is present, it is an expression which must evaluate to true before the breakpoint is honored. Without argument, list all breaks, including for each breakpoint, the number of times that breakpoint has been hit, the current ignore count, and the associated condition if any. tbreak [([filename:]lineno | function) [, condition]] Temporary breakpoint, which is removed automatically when it is first hit. The arguments are the same as for "break". cl(ear) [filename:lineno | bpnumber [bpnumber ...]] With a *filename:lineno* argument, clear all the breakpoints at this line. With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). disable [bpnumber [bpnumber ...]] Disable the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled. enable [bpnumber [bpnumber ...]] Enable the breakpoints specified. ignore bpnumber [count] Set the ignore count for the given breakpoint number. If count is omitted, the ignore count is set to 0. A breakpoint becomes active when the ignore count is zero. When non-zero, the count is decremented each time the breakpoint is reached and the breakpoint is not disabled and any associated condition evaluates to true. condition bpnumber [condition] Set a new *condition* for the breakpoint, an expression which must evaluate to true before the breakpoint is honored. If *condition* is absent, any existing condition is removed; i.e., the breakpoint is made unconditional. commands [bpnumber] Specify a list of commands for breakpoint number *bpnumber*. The commands themselves appear on the following lines. Type a line containing just "end" to terminate the commands. An example: (Pdb) commands 1 (com) p some_variable (com) end (Pdb) To remove all commands from a breakpoint, type commands and follow it immediately with "end"; that is, give no commands. With no *bpnumber* argument, commands refers to the last breakpoint set. You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution. Specifying any command resuming execution (currently continue, step, next, return, jump, quit and their abbreviations) terminates the command list (as if that command was immediately followed by end). This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint—which could have its own command list, leading to ambiguities about which list to execute. If you use the ‘silent’ command in the command list, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the other commands print anything, you see no sign that the breakpoint was reached. s(tep) Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function). n(ext) Continue execution until the next line in the current function is reached or it returns. (The difference between "next" and "step" is that "step" stops inside a called function, while "next" executes called functions at (nearly) full speed, only stopping at the next line in the current function.) unt(il) [lineno] Without argument, continue execution until the line with a number greater than the current one is reached. With a line number, continue execution until a line with a number greater or equal to that is reached. In both cases, also stop when the current frame returns. Changed in version 3.2: Allow giving an explicit line number. r(eturn) Continue execution until the current function returns. c(ont(inue)) Continue execution, only stop when a breakpoint is encountered. j(ump) lineno Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run. It should be noted that not all jumps are allowed – for instance it is not possible to jump into the middle of a "for" loop or out of a "finally" clause. l(ist) [first[, last]] List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With "." as argument, list 11 lines around the current line. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count. The current line in the current frame is indicated by "->". If an exception is being debugged, the line where the exception was originally raised or propagated is indicated by ">>", if it differs from the current line. New in version 3.2: The ">>" marker. ll | longlist List all source code for the current function or frame. Interesting lines are marked as for "list". New in version 3.2. a(rgs) Print the argument list of the current function. p expression Evaluate the *expression* in the current context and print its value. Note: "print()" can also be used, but is not a debugger command — this executes the Python "print()" function. pp expression Like the "p" command, except the value of the expression is pretty- printed using the "pprint" module. whatis expression Print the type of the *expression*. source expression Try to get source code for the given object and display it. New in version 3.2. display [expression] Display the value of the expression if it changed, each time execution stops in the current frame. Without expression, list all display expressions for the current frame. New in version 3.2. undisplay [expression] Do not display the expression any more in the current frame. Without expression, clear all display expressions for the current frame. New in version 3.2. interact Start an interactive interpreter (using the "code" module) whose global namespace contains all the (global and local) names found in the current scope. New in version 3.2. alias [name [command]] Create an alias called *name* that executes *command*. The command must *not* be enclosed in quotes. Replaceable parameters can be indicated by "%1", "%2", and so on, while "%*" is replaced by all the parameters. If no command is given, the current alias for *name* is shown. If no arguments are given, all aliases are listed. Aliases may be nested and can contain anything that can be legally typed at the pdb prompt. Note that internal pdb commands *can* be overridden by aliases. Such a command is then hidden until the alias is removed. Aliasing is recursively applied to the first word of the command line; all other words in the line are left alone. As an example, here are two useful aliases (especially when placed in the ".pdbrc" file): # Print instance variables (usage "pi classInst") alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k]) # Print instance variables in self alias ps pi self unalias name Delete the specified alias. ! statement Execute the (one-line) *statement* in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a "global" statement on the same line, e.g.: (Pdb) global list_options; list_options = ['-l'] (Pdb) run [args ...] restart [args ...] Restart the debugged Python program. If an argument is supplied, it is split with "shlex" and the result is used as the new "sys.argv". History, breakpoints, actions and debugger options are preserved. "restart" is an alias for "run". q(uit) Quit from the debugger. The program being executed is aborted. -[ Footnotes ]- [1] Whether a frame is considered to originate in a certain module is determined by the "__name__" in the frame globals. aThe "del" statement ******************* del_stmt ::= "del" target_list Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints. Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a "global" statement in the same code block. If the name is unbound, a "NameError" exception will be raised. Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object). Changed in version 3.2: Previously it was illegal to delete a name from the local namespace if it occurs as a free variable in a nested block. u Dictionary displays ******************* A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression | "**" or_expr dict_comprehension ::= expression ":" expression comp_for A dictionary display yields a new dictionary object. If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given. A double asterisk "**" denotes *dictionary unpacking*. Its operand must be a *mapping*. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings. New in version 3.5: Unpacking into dictionary displays, originally proposed by **PEP 448**. A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual “for” and “if” clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced. Restrictions on the types of the key values are listed earlier in section The standard type hierarchy. (To summarize, the key type should be *hashable*, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails. aInteraction with dynamic features ********************************* Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aBThe "if" statement ****************** The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ("elif" expression ":" suite)* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. uExceptions ********** Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. u$Execution model *************** Structure of a program ====================== A Python program is constructed from code blocks. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the "-c" option) is a code block. The string argument passed to the built-in functions "eval()" and "exec()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed. Naming and binding ================== Binding of names ---------------- *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names ------------------- A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution --------------------------------- **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features --------------------------------- Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. Exceptions ========== Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" … "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. uoExpression lists **************** expression_list ::= expression ("," expression)* [","] starred_list ::= starred_item ("," starred_item)* [","] starred_expression ::= expression | (starred_item ",")* [starred_item] starred_item ::= expression | "*" or_expr Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. An asterisk "*" denotes *iterable unpacking*. Its operand must be an *iterable*. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking. New in version 3.5: Iterable unpacking in expression lists, originally proposed by **PEP 448**. The trailing comma is required only to create a single tuple (a.k.a. a *singleton*); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: "()".) aFloating point literals *********************** Floating point literals are described by the following lexical definitions: floatnumber ::= pointfloat | exponentfloat pointfloat ::= [digitpart] fraction | digitpart "." exponentfloat ::= (digitpart | pointfloat) exponent digitpart ::= digit (["_"] digit)* fraction ::= "." digitpart exponent ::= ("e" | "E") ["+" | "-"] digitpart Note that the integer and exponent parts are always interpreted using radix 10. For example, "077e010" is legal, and denotes the same number as "77e10". The allowed range of floating point literals is implementation-dependent. As in integer literals, underscores are supported for digit grouping. Some examples of floating point literals: 3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. u The "for" statement ******************* The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a "StopIteration" exception), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there is no next item. The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop: for i in range(10): print(i) i = 5 # this will not affect the for-loop # because i will be overwritten with the next # index in the range Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns an iterator of integers suitable to emulate the effect of Pascal’s "for i := a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) u YFormat String Syntax ******************** The "str.format()" method and the "Formatter" class share the same syntax for format strings (although in the case of "Formatter", subclasses can define their own format string syntax). The syntax is related to that of formatted string literals, but there are differences. Format strings contain “replacement fields” surrounded by curly braces "{}". Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: "{{" and "}}". The grammar for a replacement field is as follows: replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | digit+] attribute_name ::= identifier element_index ::= digit+ | index_string index_string ::= + conversion ::= "r" | "s" | "a" format_spec ::= In less formal terms, the replacement field can start with a *field_name* that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is preceded by an exclamation point "'!'", and a *format_spec*, which is preceded by a colon "':'". These specify a non-default format for the replacement value. See also the Format Specification Mini-Language section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings "'10'" or "':-]'") within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form "'.name'" selects the named attribute using "getattr()", while an expression of the form "'[index]'" does an index lookup using "__getitem__()". Changed in version 3.1: The positional argument specifiers can be omitted for "str.format()", so "'{} {}'.format(a, b)" is equivalent to "'{0} {1}'.format(a, b)". Changed in version 3.4: The positional argument specifiers can be omitted for "Formatter". Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the "__format__()" method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling "__format__()", the normal formatting logic is bypassed. Three conversion flags are currently supported: "'!s'" which calls "str()" on the value, "'!r'" which calls "repr()" and "'!a'" which calls "ascii()". Some examples: "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own “formatting mini-language” or interpretation of the *format_spec*. Most built-in types support a common formatting mini-language, which is described in the next section. A *format_spec* field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the *format_spec* string is interpreted. This allows the formatting of a value to be dynamically specified. See the Format examples section for some examples. Format Specification Mini-Language ================================== “Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax and Formatted string literals). They can also be passed directly to the built-in "format()" function. Each formattable type may define how the format specification is to be interpreted. Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types. A general convention is that an empty format string ("""") produces the same result as if you had called "str()" on the value. A non-empty format string typically modifies the result. The general form of a *standard format specifier* is: format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type] fill ::= align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= digit+ grouping_option ::= "_" | "," precision ::= digit+ type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" If a valid *align* value is specified, it can be preceded by a *fill* character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (“"{"” or “"}"”) as the *fill* character in a formatted string literal or when using the "str.format()" method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the "format()" function. The meaning of the various alignment options is as follows: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'<'" | Forces the field to be left-aligned within the available | | | space (this is the default for most objects). | +-----------+------------------------------------------------------------+ | "'>'" | Forces the field to be right-aligned within the available | | | space (this is the default for numbers). | +-----------+------------------------------------------------------------+ | "'='" | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form ‘+000000120’. This alignment option is only | | | valid for numeric types. It becomes the default when ‘0’ | | | immediately precedes the field width. | +-----------+------------------------------------------------------------+ | "'^'" | Forces the field to be centered within the available | | | space. | +-----------+------------------------------------------------------------+ Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The *sign* option is only valid for number types, and can be one of the following: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'+'" | indicates that a sign should be used for both positive as | | | well as negative numbers. | +-----------+------------------------------------------------------------+ | "'-'" | indicates that a sign should be used only for negative | | | numbers (this is the default behavior). | +-----------+------------------------------------------------------------+ | space | indicates that a leading space should be used on positive | | | numbers, and a minus sign on negative numbers. | +-----------+------------------------------------------------------------+ The "'#'" option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and Decimal types. For integers, when binary, octal, or hexadecimal output is used, this option adds the prefix respective "'0b'", "'0o'", or "'0x'" to the output value. For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for "'g'" and "'G'" conversions, trailing zeros are not removed from the result. The "','" option signals the use of a comma for a thousands separator. For a locale aware separator, use the "'n'" integer presentation type instead. Changed in version 3.1: Added the "','" option (see also **PEP 378**). The "'_'" option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type "'d'". For integer presentation types "'b'", "'o'", "'x'", and "'X'", underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error. Changed in version 3.6: Added the "'_'" option (see also **PEP 515**). *width* is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the *width* field by a zero ("'0'") character enables sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of "'0'" with an *alignment* type of "'='". The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with "'f'" and "'F'", or before and after the decimal point for a floating point value formatted with "'g'" or "'G'". For non- number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The *precision* is not allowed for integer values. Finally, the *type* determines how the data should be presented. The available string presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'s'" | String format. This is the default type for strings and | | | may be omitted. | +-----------+------------------------------------------------------------+ | None | The same as "'s'". | +-----------+------------------------------------------------------------+ The available integer presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'b'" | Binary format. Outputs the number in base 2. | +-----------+------------------------------------------------------------+ | "'c'" | Character. Converts the integer to the corresponding | | | unicode character before printing. | +-----------+------------------------------------------------------------+ | "'d'" | Decimal Integer. Outputs the number in base 10. | +-----------+------------------------------------------------------------+ | "'o'" | Octal format. Outputs the number in base 8. | +-----------+------------------------------------------------------------+ | "'x'" | Hex format. Outputs the number in base 16, using lower- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'X'" | Hex format. Outputs the number in base 16, using upper- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'d'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | None | The same as "'d'". | +-----------+------------------------------------------------------------+ In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except "'n'" and "None"). When doing so, "float()" is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'e'" | Exponent notation. Prints the number in scientific | | | notation using the letter ‘e’ to indicate the exponent. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'E'" | Exponent notation. Same as "'e'" except it uses an upper | | | case ‘E’ as the separator character. | +-----------+------------------------------------------------------------+ | "'f'" | Fixed-point notation. Displays the number as a fixed-point | | | number. The default precision is "6". | +-----------+------------------------------------------------------------+ | "'F'" | Fixed-point notation. Same as "'f'", but converts "nan" to | | | "NAN" and "inf" to "INF". | +-----------+------------------------------------------------------------+ | "'g'" | General format. For a given precision "p >= 1", this | | | rounds the number to "p" significant digits and then | | | formats the result in either fixed-point format or in | | | scientific notation, depending on its magnitude. The | | | precise rules are as follows: suppose that the result | | | formatted with presentation type "'e'" and precision "p-1" | | | would have exponent "exp". Then if "-4 <= exp < p", the | | | number is formatted with presentation type "'f'" and | | | precision "p-1-exp". Otherwise, the number is formatted | | | with presentation type "'e'" and precision "p-1". In both | | | cases insignificant trailing zeros are removed from the | | | significand, and the decimal point is also removed if | | | there are no remaining digits following it. Positive and | | | negative infinity, positive and negative zero, and nans, | | | are formatted as "inf", "-inf", "0", "-0" and "nan" | | | respectively, regardless of the precision. A precision of | | | "0" is treated as equivalent to a precision of "1". The | | | default precision is "6". | +-----------+------------------------------------------------------------+ | "'G'" | General format. Same as "'g'" except switches to "'E'" if | | | the number gets too large. The representations of infinity | | | and NaN are uppercased, too. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'g'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | "'%'" | Percentage. Multiplies the number by 100 and displays in | | | fixed ("'f'") format, followed by a percent sign. | +-----------+------------------------------------------------------------+ | None | Similar to "'g'", except that fixed-point notation, when | | | used, has at least one digit past the decimal point. The | | | default precision is as high as needed to represent the | | | particular value. The overall effect is to match the | | | output of "str()" as altered by the other format | | | modifiers. | +-----------+------------------------------------------------------------+ Format examples =============== This section contains examples of the "str.format()" syntax and comparison with the old "%"-formatting. In most of the cases the syntax is similar to the old "%"-formatting, with the addition of the "{}" and with ":" used instead of "%". For example, "'%03.2f'" can be translated to "'{:03.2f}'". The new format syntax also supports new and different options, shown in the following examples. Accessing arguments by position: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 'abracadabra' Accessing arguments by name: >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 'Coordinates: 37.24N, -115.81W' >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 'Coordinates: 37.24N, -115.81W' Accessing arguments’ attributes: >>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point: ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)' Accessing arguments’ items: >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' Replacing "%s" and "%r": >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') "repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign: >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases: >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma as a thousands separator: >>> '{:,}'.format(1234567890) '1,234,567,890' Expressing a percentage: >>> points = 19 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 86.36%' Using type-specific formatting: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' Nesting arguments and more complex examples: >>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ... 'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right' >>> >>> octets = [192, 168, 0, 1] >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 'C0A80001' >>> int(_, 16) 3232235521 >>> >>> width = 5 >>> for num in range(5,12): ... for base in 'dXob': ... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ') ... print() ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 u[Function definitions ******************** A function definition defines a user-defined function object (see section The standard type hierarchy): funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* parameter_list ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]] | parameter_list_starargs parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","] parameter ::= identifier [":" expression] defparameter ::= parameter ["=" expression] funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [2] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code @f1(arg) @f2 def func(): pass is roughly equivalent to def func(): pass func = f1(arg)(f2(func)) except that the original function is not temporarily bound to the name "func". When one or more *parameters* have the form *parameter* "=" *expression*, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “"*"” must also have a default value — this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same “pre- computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form “"*identifier"” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “"**identifier"” is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after “"*"” or “"*identifier"” are keyword-only parameters and may only be passed used keyword arguments. Parameters may have annotations of the form “": expression"” following the parameter name. Any parameter may have an annotation even those of the form "*identifier" or "**identifier". Functions may have “return” annotation of the form “"-> expression"” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the "__annotations__" attribute of the function object. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “"def"” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “"def"” form is actually more powerful since it allows the execution of multiple statements and annotations. **Programmer’s note:** Functions are first-class objects. A “"def"” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. See also: **PEP 3107** - Function Annotations The original specification for function annotations. uThe "global" statement ********************** global_stmt ::= "global" identifier ("," identifier)* The "global" statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without "global", although free variables may refer to globals without being declared global. Names listed in a "global" statement must not be used in the same code block textually preceding that "global" statement. Names listed in a "global" statement must not be defined as formal parameters or in a "for" loop control target, "class" definition, function definition, "import" statement, or variable annotation. **CPython implementation detail:** The current implementation does not enforce some of these restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program. **Programmer’s note:** "global" is a directive to the parser. It applies only to code parsed at the same time as the "global" statement. In particular, a "global" statement contained in a string or code object supplied to the built-in "exec()" function does not affect the code block *containing* the function call, and code contained in such a string is unaffected by "global" statements in the code containing the function call. The same applies to the "eval()" and "compile()" functions. uReserved classes of identifiers ******************************* Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). u(Identifiers and keywords ************************ Identifiers (also referred to as *names*) are described by the following lexical definitions. The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also **PEP 3131** for further details. Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters "A" through "Z", the underscore "_" and, except for the first character, the digits "0" through "9". Python 3.0 introduces additional characters from outside the ASCII range (see **PEP 3131**). For these characters, the classification uses the version of the Unicode Character Database as included in the "unicodedata" module. Identifiers are unlimited in length. Case is significant. identifier ::= xid_start xid_continue* id_start ::= id_continue ::= xid_start ::= xid_continue ::= The Unicode category codes mentioned above stand for: * *Lu* - uppercase letters * *Ll* - lowercase letters * *Lt* - titlecase letters * *Lm* - modifier letters * *Lo* - other letters * *Nl* - letter numbers * *Mn* - nonspacing marks * *Mc* - spacing combining marks * *Nd* - decimal numbers * *Pc* - connector punctuations * *Other_ID_Start* - explicit list of characters in PropList.txt to support backwards compatibility * *Other_ID_Continue* - likewise All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC. A non-normative HTML file listing all valid identifier characters for Unicode 4.1 can be found at https://www.dcl.hpi.uni- potsdam.de/home/loewis/table-3131.html. Keywords ======== The following identifiers are used as reserved words, or *keywords* of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here: False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise Reserved classes of identifiers =============================== Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "builtins" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes. See section Identifiers (Names). a5Imaginary literals ****************** Imaginary literals are described by the following lexical definitions: imagnumber ::= (floatnumber | digitpart) ("j" | "J") An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., "(3+4j)". Some examples of imaginary literals: 3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j u The "import" statement ********************** import_stmt ::= "import" module ["as" identifier] ("," module ["as" identifier])* | "from" relative_module "import" identifier ["as" identifier] ("," identifier ["as" identifier])* | "from" relative_module "import" "(" identifier ["as" identifier] ("," identifier ["as" identifier])* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ The basic import statement (no "from" clause) is executed in two steps: 1. find a module, loading and initializing it if necessary 2. define a name or names in the local namespace for the scope where the "import" statement occurs. When the statement contains multiple clauses (separated by commas) the two steps are carried out separately for each clause, just as though the clauses had been separated out into individual import statements. The details of the first step, finding and loading modules are described in greater detail in the section on the import system, which also describes the various types of packages and modules that can be imported, as well as all the hooks that can be used to customize the import system. Note that failures in this step may indicate either that the module could not be located, *or* that an error occurred while initializing the module, which includes execution of the module’s code. If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways: * If the module name is followed by "as", then the name following "as" is bound directly to the imported module. * If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module * If the module being imported is *not* a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly The "from" form uses a slightly more complex process: 1. find the module specified in the "from" clause, loading and initializing it if necessary; 2. for each of the identifiers specified in the "import" clauses: 1. check if the imported module has an attribute by that name 2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute 3. if the attribute is not found, "ImportError" is raised. 4. otherwise, a reference to that value is stored in the local namespace, using the name in the "as" clause if it is present, otherwise using the attribute name Examples: import foo # foo imported and bound locally import foo.bar.baz # foo.bar.baz imported, foo bound locally import foo.bar.baz as fbb # foo.bar.baz imported and bound as fbb from foo.bar import baz # foo.bar.baz imported and bound as baz from foo import attr # foo imported and foo.attr bound as attr If the list of identifiers is replaced by a star ("'*'"), all public names defined in the module are bound in the local namespace for the scope where the "import" statement occurs. The *public names* defined by a module are determined by checking the module’s namespace for a variable named "__all__"; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in "__all__" are all considered public and are required to exist. If "__all__" is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ("'_'"). "__all__" should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The wild card form of import — "from module import *" — is only allowed at the module level. Attempting to use it in class or function definitions will raise a "SyntaxError". When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after "from" you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute "from . import mod" from a module in the "pkg" package then you will end up importing "pkg.mod". If you execute "from ..subpkg2 import mod" from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The specification for relative imports is contained within **PEP 328**. "importlib.import_module()" is provided to support applications that determine dynamically the modules to be loaded. Future statements ================= A *future statement* is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python where the feature becomes standard. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard. future_stmt ::= "from" "__future__" "import" feature ["as" identifier] ("," feature ["as" identifier])* | "from" "__future__" "import" "(" feature ["as" identifier] ("," feature ["as" identifier])* [","] ")" feature ::= identifier A future statement must appear near the top of the module. The only lines that can appear before a future statement are: * the module docstring (if any), * comments, * blank lines, and * other future statements. The features recognized by Python 3.0 are "absolute_import", "division", "generators", "unicode_literals", "print_function", "nested_scopes" and "with_statement". They are all redundant because they are always enabled, and only kept for backwards compatibility. A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future statement contains a feature not known to it. The direct runtime semantics are the same as for any import statement: there is a standard module "__future__", described later, and it will be imported in the usual way at the time the future statement is executed. The interesting runtime semantics depend on the specific feature enabled by the future statement. Note that there is nothing special about the statement: import __future__ [as name] That is not a future statement; it’s an ordinary import statement with no special semantics or syntax restrictions. Code compiled by calls to the built-in functions "exec()" and "compile()" that occur in a module "M" containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can be controlled by optional arguments to "compile()" — see the documentation of that function for details. A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session. If an interpreter is started with the "-i" option, is passed a script name to execute, and the script includes a future statement, it will be in effect in the interactive session started after the script is executed. See also: **PEP 236** - Back to the __future__ The original proposal for the __future__ mechanism. aOMembership test operations ************************** The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". aVInteger literals **************** Integer literals are described by the following lexical definitions: integer ::= decinteger | bininteger | octinteger | hexinteger decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")* bininteger ::= "0" ("b" | "B") (["_"] bindigit)+ octinteger ::= "0" ("o" | "O") (["_"] octdigit)+ hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+ nonzerodigit ::= "1"..."9" digit ::= "0"..."9" bindigit ::= "0" | "1" octdigit ::= "0"..."7" hexdigit ::= digit | "a"..."f" | "A"..."F" There is no limit for the length of integer literals apart from what can be stored in available memory. Underscores are ignored for determining the numeric value of the literal. They can be used to group digits for enhanced readability. One underscore can occur between digits, and after base specifiers like "0x". Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0. Some examples of integer literals: 7 2147483647 0o177 0b100110111 3 79228162514264337593543950336 0o377 0xdeadbeef 100_000_000_000 0b_1110_0101 Changed in version 3.6: Underscores are now allowed for grouping purposes in literals. a^Lambdas ******* lambda_expr ::= "lambda" [parameter_list] ":" expression lambda_expr_nocond ::= "lambda" [parameter_list] ":" expression_nocond Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression "lambda parameters: expression" yields a function object. The unnamed object behaves like a function object defined with: def (parameters): return expression See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements or annotations. a/List displays ************* A list display is a possibly empty series of expressions enclosed in square brackets: list_display ::= "[" [starred_list | comprehension] "]" A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma- separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a comprehension is supplied, the list is constructed from the elements resulting from the comprehension. uNaming and binding ****************** Binding of names ================ *Names* refer to objects. Names are introduced by name binding operations. The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, or after "as" in a "with" statement or "except" clause. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name is bound in a block, it is a local variable of that block, unless declared as "nonlocal" or "global". If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. Each occurrence of a name in the program text refers to the *binding* of that name established by the following name resolution rules. Resolution of names =================== A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s *environment*. When a name is not found at all, a "NameError" exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the "global" statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "builtins". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The "global" statement must precede all uses of the name. The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. The "nonlocal" statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. "SyntaxError" is raised at compile time if the given name does not exist in any enclosing function scope. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". Class definition blocks and arguments to "exec()" and "eval()" are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) Builtins and restricted execution ================================= **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "builtins" module and modify its attributes appropriately. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module’s dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "builtins"; when in any other module, "__builtins__" is an alias for the dictionary of the "builtins" module itself. Interaction with dynamic features ================================= Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i = 10 def f(): print(i) i = 42 f() The "eval()" and "exec()" functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec()" and "eval()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. aThe "nonlocal" statement ************************ nonlocal_stmt ::= "nonlocal" identifier ("," identifier)* The "nonlocal" statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope. Names listed in a "nonlocal" statement, unlike those listed in a "global" statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously). Names listed in a "nonlocal" statement must not collide with pre- existing bindings in the local scope. See also: **PEP 3104** - Access to Names in Outer Scopes The specification for the "nonlocal" statement. uNumeric literals **************** There are three types of numeric literals: integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator ‘"-"‘ and the literal "1". uEmulating numeric types *********************** The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". u Objects, values and types ************************* *Objects* are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.) Every object has an identity, a type and a value. An object’s *identity* never changes once it has been created; you may think of it as the object’s address in memory. The ‘"is"’ operator compares the identity of two objects; the "id()" function returns an integer representing its identity. **CPython implementation detail:** For CPython, "id(x)" is the memory address where "x" is stored. An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The "type()" function returns an object’s type (which is an object itself). Like its identity, an object’s *type* is also unchangeable. [1] The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable. Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable. **CPython implementation detail:** CPython currently uses a reference- counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the "gc" module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (so you should always close files explicitly). Note that the use of the implementation’s tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a ‘"try"…"except"’ statement may keep objects alive. Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a "close()" method. Programs are strongly recommended to explicitly close such objects. The ‘"try"…"finally"’ statement and the ‘"with"’ statement provide convenient ways to do this. Some objects contain references to other objects; these are called *containers*. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after "a = 1; b = 1", "a" and "b" may or may not refer to the same object with the value one, depending on the implementation, but after "c = []; d = []", "c" and "d" are guaranteed to refer to two different, unique, newly created empty lists. (Note that "c = d = []" assigns the same object to both "c" and "d".) uOperator precedence ******************* The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left). Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section. +-------------------------------------------------+---------------------------------------+ | Operator | Description | +=================================================+=======================================+ | "lambda" | Lambda expression | +-------------------------------------------------+---------------------------------------+ | "if" – "else" | Conditional expression | +-------------------------------------------------+---------------------------------------+ | "or" | Boolean OR | +-------------------------------------------------+---------------------------------------+ | "and" | Boolean AND | +-------------------------------------------------+---------------------------------------+ | "not" "x" | Boolean NOT | +-------------------------------------------------+---------------------------------------+ | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership | | ">=", "!=", "==" | tests and identity tests | +-------------------------------------------------+---------------------------------------+ | "|" | Bitwise OR | +-------------------------------------------------+---------------------------------------+ | "^" | Bitwise XOR | +-------------------------------------------------+---------------------------------------+ | "&" | Bitwise AND | +-------------------------------------------------+---------------------------------------+ | "<<", ">>" | Shifts | +-------------------------------------------------+---------------------------------------+ | "+", "-" | Addition and subtraction | +-------------------------------------------------+---------------------------------------+ | "*", "@", "/", "//", "%" | Multiplication, matrix | | | multiplication, division, floor | | | division, remainder [5] | +-------------------------------------------------+---------------------------------------+ | "+x", "-x", "~x" | Positive, negative, bitwise NOT | +-------------------------------------------------+---------------------------------------+ | "**" | Exponentiation [6] | +-------------------------------------------------+---------------------------------------+ | "await" "x" | Await expression | +-------------------------------------------------+---------------------------------------+ | "x[index]", "x[index:index]", | Subscription, slicing, call, | | "x(arguments...)", "x.attribute" | attribute reference | +-------------------------------------------------+---------------------------------------+ | "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list | | value...}", "{expressions...}" | display, dictionary display, set | | | display | +-------------------------------------------------+---------------------------------------+ -[ Footnotes ]- [1] While "abs(x%y) < abs(y)" is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double- precision number, in order that "-1e-100 % 1e100" have the same sign as "1e100", the computed result is "-1e-100 + 1e100", which is numerically exactly equal to "1e100". The function "math.fmod()" returns a result whose sign matches the sign of the first argument instead, and so returns "-1e-100" in this case. Which approach is more appropriate depends on the application. [2] If x is very close to an exact integer multiple of y, it’s possible for "x//y" to be one larger than "(x-x%y)//y" due to rounding. In such cases, Python returns the latter result, in order to preserve that "divmod(x,y)[0] * y + x % y" be very close to "x". [3] The Unicode standard distinguishes between *code points* (e.g. U+0041) and *abstract characters* (e.g. “LATIN CAPITAL LETTER A”). While most abstract characters in Unicode are only represented using one code point, there is a number of abstract characters that can in addition be represented using a sequence of more than one code point. For example, the abstract character “LATIN CAPITAL LETTER C WITH CEDILLA” can be represented as a single *precomposed character* at code position U+00C7, or as a sequence of a *base character* at code position U+0043 (LATIN CAPITAL LETTER C), followed by a *combining character* at code position U+0327 (COMBINING CEDILLA). The comparison operators on strings compare at the level of Unicode code points. This may be counter-intuitive to humans. For example, ""\u00C7" == "\u0043\u0327"" is "False", even though both strings represent the same abstract character “LATIN CAPITAL LETTER C WITH CEDILLA”. To compare strings at the level of abstract characters (that is, in a way intuitive to humans), use "unicodedata.normalize()". [4] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the "is" operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [5] The "%" operator is also used for string formatting; the same precedence applies. [6] The power operator "**" binds less tightly than an arithmetic or bitwise unary operator on its right, that is, "2**-1" is "0.5". uwThe "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) aThe power operator ****************** The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is: power ::= (await_expr | primary) ["**" u_expr] Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): "-1**2" results in "-1". The power operator has the same semantics as the built-in "pow()" function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type, and the result is of that type. For int operands, the result has the same type as the operands unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, "10**2" returns "100", but "10**-2" returns "0.01". Raising "0.0" to a negative power results in a "ZeroDivisionError". Raising a negative number to a fractional power results in a "complex" number. (In earlier versions it raised a "ValueError".) ul The "raise" statement ********************* raise_stmt ::= "raise" [expression ["from" expression]] If no expressions are present, "raise" re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a "RuntimeError" exception is raised indicating that this is an error. Otherwise, "raise" evaluates the first expression as the exception object. It must be either a subclass or an instance of "BaseException". If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments. The *type* of the exception is the exception instance’s class, the *value* is the instance itself. A traceback object is normally created automatically when an exception is raised and attached to it as the "__traceback__" attribute, which is writable. You can create an exception and set your own traceback in one step using the "with_traceback()" exception method (which returns the same exception instance, with its traceback set to its argument), like so: raise Exception("foo occurred").with_traceback(tracebackobj) The "from" clause is used for exception chaining: if given, the second *expression* must be another exception class or instance, which will then be attached to the raised exception as the "__cause__" attribute (which is writable). If the raised exception is not handled, both exceptions will be printed: >>> try: ... print(1 / 0) ... except Exception as exc: ... raise RuntimeError("Something bad happened") from exc ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero The above exception was the direct cause of the following exception: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened A similar mechanism works implicitly if an exception is raised inside an exception handler or a "finally" clause: the previous exception is then attached as the new exception’s "__context__" attribute: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Exception chaining can be explicitly suppressed by specifying "None" in the "from" clause: >>> try: ... print(1 / 0) ... except: ... raise RuntimeError("Something bad happened") from None ... Traceback (most recent call last): File "", line 4, in RuntimeError: Something bad happened Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement. Changed in version 3.3: "None" is now permitted as "Y" in "raise X from Y". New in version 3.3: The "__suppress_context__" attribute to suppress automatic display of the exception context. aThe "return" statement ********************** return_stmt ::= "return" [expression_list] "return" may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else "None" is substituted. "return" leaves the current function call with the expression list (or "None") as return value. When "return" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the function. In a generator function, the "return" statement indicates that the generator is done and will cause "StopIteration" to be raised. The returned value (if any) is used as an argument to construct "StopIteration" and becomes the "StopIteration.value" attribute. In an asynchronous generator function, an empty "return" statement indicates that the asynchronous generator is done and will cause "StopAsyncIteration" to be raised. A non-empty "return" statement is a syntax error in an asynchronous generator function. uEmulating container types ************************* The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. aShifting operations ******************* The shifting operations have lower priority than the arithmetic operations: shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr These operators accept integers as arguments. They shift the first argument to the left or right by the number of bits given by the second argument. A right shift by *n* bits is defined as floor division by "pow(2,n)". A left shift by *n* bits is defined as multiplication with "pow(2,n)". Note: In the current implementation, the right-hand operand is required to be at most "sys.maxsize". If the right-hand operand is larger than "sys.maxsize" an "OverflowError" exception is raised. aSlicings ******** A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or "del" statements. The syntax for a slicing: slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ] lower_bound ::= expression upper_bound ::= expression stride ::= expression There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice). The semantics for a slicing are as follows. The primary is indexed (using the same "__getitem__()" method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose "start", "stop" and "step" attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting "None" for missing expressions. u~Special Attributes ****************** The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the "dir()" built-in function. object.__dict__ A dictionary or other mapping object used to store an object’s (writable) attributes. instance.__class__ The class to which a class instance belongs. class.__bases__ The tuple of base classes of a class object. definition.__name__ The name of the class, function, method, descriptor, or generator instance. definition.__qualname__ The *qualified name* of the class, function, method, descriptor, or generator instance. New in version 3.3. class.__mro__ This attribute is a tuple of classes that are considered when looking for base classes during method resolution. class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in "__mro__". class.__subclasses__() Each class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example: >>> int.__subclasses__() [] -[ Footnotes ]- [1] Additional information on these special methods may be found in the Python Reference Manual (Basic customization). [2] As a consequence, the list "[1, 2]" is considered equal to "[1.0, 2.0]", and similarly for tuples. [3] They must have since the parser can’t tell the type of the operands. [4] Cased characters are those with general category property being one of “Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase). [5] To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted. u.Special method names ******************** A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to *operator overloading*, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named "__getitem__()", and "x" is an instance of this class, then "x[i]" is roughly equivalent to "type(x).__getitem__(x, i)". Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically "AttributeError" or "TypeError"). Setting a special method to "None" indicates that the corresponding operation is not available. For example, if a class sets "__iter__()" to "None", the class is not iterable, so calling "iter()" on its instances will raise a "TypeError" (without falling back to "__getitem__()"). [2] When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the "NodeList" interface in the W3C’s Document Object Model.) Basic customization =================== object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass’s "__new__()" method using "super().__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance’s "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance’s "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class’s "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "super().__init__([args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customize it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a finalizer or (improperly) a destructor. If a base class has a "__del__()" method, the derived class’s "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. It is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. This is called object *resurrection*. It is implementation-dependent whether "__del__()" is called a second time when a resurrected object is about to be destroyed; the current *CPython* implementation only calls it once. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn’t directly call "x.__del__()" — the former decrements the reference count for "x" by one, and the latter is only called when "x"’s reference count reaches zero. **CPython implementation detail:** It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the *cyclic garbage collector*. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback. See also: Documentation for the "gc" module. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. In particular: * "__del__()" can be invoked when arbitrary code is being executed, including from any arbitrary thread. If "__del__()" needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute "__del__()". * "__del__()" can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to "None". Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. object.__repr__(self) Called by the "repr()" built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by "str(object)" and the built-in functions "format()" and "print()" to compute the “informal” or nicely printable string representation of an object. The return value must be a string object. This method differs from "object.__repr__()" in that there is no expectation that "__str__()" return a valid Python expression: a more convenient or concise representation can be used. The default implementation defined by the built-in type "object" calls "object.__repr__()". object.__bytes__(self) Called by bytes to compute a byte-string representation of an object. This should return a "bytes" object. object.__format__(self, format_spec) Called by the "format()" built-in function, and by extension, evaluation of formatted string literals and the "str.format()" method, to produce a “formatted” string representation of an object. The "format_spec" argument is a string that contains a description of the formatting options desired. The interpretation of the "format_spec" argument is up to the type implementing "__format__()", however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax. See Format Specification Mini-Language for a description of the standard formatting syntax. The return value must be a string object. Changed in version 3.4: The __format__ method of "object" itself raises a "TypeError" if passed any non-empty string. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: "xy" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. By default, "__ne__()" delegates to "__eq__()" and inverts the result unless it is "NotImplemented". There are no other implied relationships among the comparison operators, for example, the truth of "(x.__hash__". If a class that does not override "__eq__()" wishes to suppress hash support, it should include "__hash__ = None" in the class definition. A class which defines its own "__hash__()" that explicitly raises a "TypeError" would be incorrectly identified as hashable by an "isinstance(obj, collections.Hashable)" call. Note: By default, the "__hash__()" values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.This is intended to provide protection against a denial- of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).See also "PYTHONHASHSEED". Changed in version 3.3: Hash randomization is enabled by default. object.__bool__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__bool__()", all its instances are considered true. Customizing attribute access ============================ The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when the default attribute access fails with an "AttributeError" (either "__getattribute__()" raises an "AttributeError" because *name* is not an instance attribute or an attribute in the class tree for "self"; or "__get__()" of a *name* property raises "AttributeError"). This method should either return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control over attribute access. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. object.__dir__(self) Called when "dir()" is called on the object. A sequence must be returned. "dir()" converts the returned sequence to a list and sorts it. Customizing module attribute access ----------------------------------- For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the "__class__" attribute of a module object to a subclass of "types.ModuleType". For example: import sys from types import ModuleType class VerboseModule(ModuleType): def __repr__(self): return f'Verbose {self.__name__}' def __setattr__(self, attr, value): print(f'Setting {attr}...') setattr(self, attr, value) sys.modules[__name__].__class__ = VerboseModule Note: Setting module "__class__" only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected. Changed in version 3.5: "__class__" module attribute is now writable. Implementing Descriptors ------------------------ The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’ "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. object.__set_name__(self, owner, name) Called at the time the owning class *owner* is created. The descriptor has been assigned to *name*. New in version 3.6. The attribute "__objclass__" is interpreted by the "inspect" module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C). Invoking Descriptors -------------------- In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to an object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ --------- *__slots__* allow us to explicitly declare data members (like properties) and deny the creation of *__dict__* and *__weakref__* (unless explicitly declared in *__slots__* or available in a parent.) The space saved over using *__dict__* can be significant. object.__slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. Notes on using *__slots__* ~~~~~~~~~~~~~~~~~~~~~~~~~~ * When inheriting from a class without *__slots__*, the *__dict__* and *__weakref__* attribute of the instances will always be accessible. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is not limited to the class where it is defined. *__slots__* declared in parents are available in child classes. However, child subclasses will get a *__dict__* and *__weakref__* unless they also define *__slots__* (which should only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from “variable-length” built-in types such as "int", "bytes" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. * Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise "TypeError". Customizing class creation ========================== Whenever a class inherits from another class, *__init_subclass__* is called on that class. This way, it is possible to write classes which change the behavior of subclasses. This is closely related to class decorators, but where class decorators only affect the specific class they’re applied to, "__init_subclass__" solely applies to future subclasses of the class defining the method. classmethod object.__init_subclass__(cls) This method is called whenever the containing class is subclassed. *cls* is then the new subclass. If defined as a normal instance method, this method is implicitly converted to a class method. Keyword arguments which are given to a new class are passed to the parent’s class "__init_subclass__". For compatibility with other classes using "__init_subclass__", one should take out the needed keyword arguments and pass the others over to the base class, as in: class Philosopher: def __init_subclass__(cls, default_name, **kwargs): super().__init_subclass__(**kwargs) cls.default_name = default_name class AustralianPhilosopher(Philosopher, default_name="Bruce"): pass The default implementation "object.__init_subclass__" does nothing, but raises an error if it is called with any arguments. Note: The metaclass hint "metaclass" is consumed by the rest of the type machinery, and is never passed to "__init_subclass__" implementations. The actual metaclass (rather than the explicit hint) can be accessed as "type(cls)". New in version 3.6. Metaclasses ----------- By default, classes are constructed using "type()". The class body is executed in a new namespace and the class name is bound locally to the result of "type(name, bases, namespace)". The class creation process can be customized by passing the "metaclass" keyword argument in the class definition line, or by inheriting from an existing class that included such an argument. In the following example, both "MyClass" and "MySubclass" are instances of "Meta": class Meta(type): pass class MyClass(metaclass=Meta): pass class MySubclass(MyClass): pass Any other keyword arguments that are specified in the class definition are passed through to all metaclass operations described below. When a class definition is executed, the following steps occur: * the appropriate metaclass is determined * the class namespace is prepared * the class body is executed * the class object is created Determining the appropriate metaclass ------------------------------------- The appropriate metaclass for a class definition is determined as follows: * if no bases and no explicit metaclass are given, then "type()" is used * if an explicit metaclass is given and it is *not* an instance of "type()", then it is used directly as the metaclass * if an instance of "type()" is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used The most derived metaclass is selected from the explicitly specified metaclass (if any) and the metaclasses (i.e. "type(cls)") of all specified base classes. The most derived metaclass is one which is a subtype of *all* of these candidate metaclasses. If none of the candidate metaclasses meets that criterion, then the class definition will fail with "TypeError". Preparing the class namespace ----------------------------- Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a "__prepare__" attribute, it is called as "namespace = metaclass.__prepare__(name, bases, **kwds)" (where the additional keyword arguments, if any, come from the class definition). If the metaclass has no "__prepare__" attribute, then the class namespace is initialised as an empty ordered mapping. See also: **PEP 3115** - Metaclasses in Python 3000 Introduced the "__prepare__" namespace hook Executing the class body ------------------------ The class body is executed (approximately) as "exec(body, globals(), namespace)". The key difference from a normal call to "exec()" is that lexical scoping allows the class body (including any methods) to reference names from the current and outer scopes when the class definition occurs inside a function. However, even when the class definition occurs inside the function, methods defined inside the class still cannot see names defined at the class scope. Class variables must be accessed through the first parameter of instance or class methods, or through the implicit lexically scoped "__class__" reference described in the next section. Creating the class object ------------------------- Once the class namespace has been populated by executing the class body, the class object is created by calling "metaclass(name, bases, namespace, **kwds)" (the additional keywords passed here are the same as those passed to "__prepare__"). This class object is the one that will be referenced by the zero- argument form of "super()". "__class__" is an implicit closure reference created by the compiler if any methods in a class body refer to either "__class__" or "super". This allows the zero argument form of "super()" to correctly identify the class being defined based on lexical scoping, while the class or instance that was used to make the current call is identified based on the first argument passed to the method. **CPython implementation detail:** In CPython 3.6 and later, the "__class__" cell is passed to the metaclass as a "__classcell__" entry in the class namespace. If present, this must be propagated up to the "type.__new__" call in order for the class to be initialised correctly. Failing to do so will result in a "DeprecationWarning" in Python 3.6, and a "RuntimeError" in Python 3.8. When using the default metaclass "type", or any metaclass that ultimately calls "type.__new__", the following additional customisation steps are invoked after creating the class object: * first, "type.__new__" collects all of the descriptors in the class namespace that define a "__set_name__()" method; * second, all of these "__set_name__" methods are called with the class being defined and the assigned name of that particular descriptor; and * finally, the "__init_subclass__()" hook is called on the immediate parent of the new class in its method resolution order. After the class object is created, it is passed to the class decorators included in the class definition (if any) and the resulting object is bound in the local namespace as the defined class. When a new class is created by "type.__new__", the object provided as the namespace parameter is copied to a new ordered mapping and the original object is discarded. The new copy is wrapped in a read-only proxy, which becomes the "__dict__" attribute of the class object. See also: **PEP 3135** - New super Describes the implicit "__class__" closure reference Uses for metaclasses -------------------- The potential uses for metaclasses are boundless. Some ideas that have been explored include enum, logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. Customizing instance and subclass checks ======================================== The following methods are used to override the default behavior of the "isinstance()" and "issubclass()" built-in functions. In particular, the metaclass "abc.ABCMeta" implements these methods in order to allow the addition of Abstract Base Classes (ABCs) as “virtual base classes” to any class or type (including built-in types), including other ABCs. class.__instancecheck__(self, instance) Return true if *instance* should be considered a (direct or indirect) instance of *class*. If defined, called to implement "isinstance(instance, class)". class.__subclasscheck__(self, subclass) Return true if *subclass* should be considered a (direct or indirect) subclass of *class*. If defined, called to implement "issubclass(subclass, class)". Note that these methods are looked up on the type (metaclass) of a class. They cannot be defined as class methods in the actual class. This is consistent with the lookup of special methods that are called on instances, only in this case the instance is itself a class. See also: **PEP 3119** - Introducing Abstract Base Classes Includes the specification for customizing "isinstance()" and "issubclass()" behavior through "__instancecheck__()" and "__subclasscheck__()", with motivation for this functionality in the context of adding Abstract Base Classes (see the "abc" module) to the language. Emulating callable objects ========================== object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". Emulating container types ========================= The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods "keys()", "values()", "items()", "get()", "clear()", "setdefault()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python’s standard dictionary objects. The "collections" module provides a "MutableMapping" abstract base class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "keys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn’t define a "__bool__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__bool__()" method. object.__length_hint__(self) Called to implement "operator.length_hint()". Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer ">=" 0. This method is purely an optimization and is never required for correctness. New in version 3.4. Note: Slicing is done exclusively with the following three methods. A call like a[1:2] = b is translated to a[slice(1, 2, None)] = b and so forth. Missing slice items are always filled in with "None". object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container. Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. Emulating numeric types ======================= The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__matmul__(self, other) object.__truediv__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()". Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rmatmul__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "@", "/", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__imatmul__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if *x* is an instance of a class with an "__iadd__()" method, "x += y" is equivalent to "x = x.__iadd__(y)" . Otherwise, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘item’] raise an exception when the addition works?), but this behavior is in fact part of the data model. object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()" and "float()". Should return a value of the appropriate type. object.__index__(self) Called to implement "operator.index()", and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in "bin()", "hex()" and "oct()" functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer. Note: In order to have a coherent integer type class, when "__index__()" is defined "__int__()" should also be defined, and both should return the same value. object.__round__(self[, ndigits]) object.__trunc__(self) object.__floor__(self) object.__ceil__(self) Called to implement the built-in function "round()" and "math" functions "trunc()", "floor()" and "ceil()". Unless *ndigits* is passed to "__round__()" all these methods should return the value of the object truncated to an "Integral" (typically an "int"). If "__int__()" is not defined then the built-in function "int()" falls back to "__trunc__()". With Statement Context Managers =============================== A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method’s return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller’s responsibility. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. Special method lookup ===================== For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary. That behaviour is the reason why the following code raises an exception: >>> class C: ... pass ... >>> c = C() >>> c.__len__ = lambda: 5 >>> len(c) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such as "__hash__()" and "__repr__()" that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself: >>> 1 .__hash__() == hash(1) True >>> int.__hash__() == hash(int) Traceback (most recent call last): File "", line 1, in TypeError: descriptor '__hash__' of 'int' object needs an argument Incorrectly attempting to invoke an unbound method of a class in this way is sometimes referred to as ‘metaclass confusion’, and is avoided by bypassing the instance when looking up special methods: >>> type(1).__hash__(1) == hash(1) True >>> type(int).__hash__(int) == hash(int) True In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the "__getattribute__()" method even of the object’s metaclass: >>> class Meta(type): ... def __getattribute__(*args): ... print("Metaclass getattribute invoked") ... return type.__getattribute__(*args) ... >>> class C(object, metaclass=Meta): ... def __len__(self): ... return 10 ... def __getattribute__(*args): ... print("Class getattribute invoked") ... return object.__getattribute__(*args) ... >>> c = C() >>> c.__len__() # Explicit lookup via instance Class getattribute invoked 10 >>> type(c).__len__(c) # Explicit lookup via type Metaclass getattribute invoked 10 >>> len(c) # Implicit lookup 10 Bypassing the "__getattribute__()" machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class object itself in order to be consistently invoked by the interpreter). u=WString Methods ************** Strings implement all of the common sequence operations, along with the additional methods described below. Strings also support two styles of string formatting, one providing a large degree of flexibility and customization (see "str.format()", Format String Syntax and Custom String Formatting) and the other based on C "printf" style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle (printf-style String Formatting). The Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities (including regular expression support in the "re" module). str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. str.casefold() Return a casefolded copy of the string. Casefolded strings may be used for caseless matching. Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter "'ß'" is equivalent to ""ss"". Since it is already lowercase, "lower()" would do nothing to "'ß'"; "casefold()" converts it to ""ss"". The casefolding algorithm is described in section 3.13 of the Unicode Standard. New in version 3.3. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.encode(encoding="utf-8", errors="strict") Return an encoded version of the string as a bytes object. Default encoding is "'utf-8'". *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Error Handlers. For a list of possible encodings, see section Standard Encodings. Changed in version 3.1: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. str.expandtabs(tabsize=8) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. Note: When formatting a number ("int", "float", "complex", "decimal.Decimal" and subclasses) with the "n" type (ex: "'{:n}'.format(1234)"), the function temporarily sets the "LC_CTYPE" locale to the "LC_NUMERIC" locale to decode "decimal_point" and "thousands_sep" fields of "localeconv()" if they are non-ASCII or longer than 1 byte, and the "LC_NUMERIC" locale is different than the "LC_CTYPE" locale. This temporary change affects other threads. Changed in version 3.6.5: When formatting a number with the "n" type, the function sets temporarily the "LC_CTYPE" locale to the "LC_NUMERIC" locale in some cases. str.format_map(mapping) Similar to "str.format(**mapping)", except that "mapping" is used directly and not copied to a "dict". This is useful if for example "mapping" is a dict subclass: >>> class Default(dict): ... def __missing__(self, key): ... return key ... >>> '{name} was born in {country}'.format_map(Default(name='Guido')) 'Guido was born in country' New in version 3.2. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character "c" is alphanumeric if one of the following returns "True": "c.isalpha()", "c.isdecimal()", "c.isdigit()", or "c.isnumeric()". str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard. str.isdecimal() Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal. str.isidentifier() Return true if the string is a valid identifier according to the language definition, section Identifiers and keywords. Use "keyword.iskeyword()" to test for reserved identifiers such as "def" and "class". str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. str.isnumeric() Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric. str.isprintable() Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when "repr()" is invoked on a string. It has no bearing on the handling of strings written to "sys.stdout" or "sys.stderr".) str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as “Other” or “Separator” and those with bidirectional property being one of “WS”, “B”, or “S”. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. The lowercasing algorithm used is described in section 3.13 of the Unicode Standard. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' static str.maketrans(x[, y[, z]]) This static method returns a translation table usable for "str.translate()". If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or "None". Character keys will then be converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to "None" in the result. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is an ASCII space). The original string is returned if *width* is less than or equal to "len(s)". str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. str.rsplit(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' str.split(sep=None, maxsplit=-1) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". For example: >>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', ''] If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example: >>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3'] str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless *keepends* is given and true. This method splits on the following line boundaries. In particular, the boundaries are a superset of *universal newlines*. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 3.2: "\v" and "\f" added to list of line boundaries. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' The outermost leading and trailing *chars* argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in *chars*. A similar action takes place on the trailing end. For example: >>> comment_string = '#....... Section 3.2.1 Issue #32 .......' >>> comment_string.strip('.#! ') 'Section 3.2.1 Issue #32' str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that "s.swapcase().swapcase() == s". str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. For example: >>> 'Hello world'.title() 'Hello World' The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." str.translate(table) Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via "__getitem__()", typically a *mapping* or *sequence*. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return "None", to delete the character from the return string; or raise a "LookupError" exception, to map the character to itself. You can use "str.maketrans()" to create a translation map from character-to-character mappings in different formats. See also the "codecs" module for a more flexible approach to custom character mappings. str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "s.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase). The uppercasing algorithm used is described in section 3.13 of the Unicode Standard. str.zfill(width) Return a copy of the string left filled with ASCII "'0'" digits to make a string of length *width*. A leading sign prefix ("'+'"/"'-'") is handled by inserting the padding *after* the sign character rather than before. The original string is returned if *width* is less than or equal to "len(s)". For example: >>> "42".zfill(5) '00042' >>> "-42".zfill(5) '-0042' uw String and Bytes literals ************************* String literals are described by the following lexical definitions: stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F" | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | stringescapeseq longstringitem ::= longstringchar | stringescapeseq shortstringchar ::= longstringchar ::= stringescapeseq ::= "\" bytesliteral ::= bytesprefix(shortbytes | longbytes) bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= longbyteschar ::= bytesescapeseq ::= "\" One syntactic restriction not indicated by these productions is that whitespace is not allowed between the "stringprefix" or "bytesprefix" and the rest of the literal. The source character set is defined by the encoding declaration; it is UTF-8 if no encoding declaration is given in the source file; see section Encoding declarations. In plain English: Both types of literals can be enclosed in matching single quotes ("'") or double quotes ("""). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as *triple-quoted strings*). The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. Bytes literals are always prefixed with "'b'" or "'B'"; they produce an instance of the "bytes" type instead of the "str" type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes. Both string and bytes literals may optionally be prefixed with a letter "'r'" or "'R'"; such strings are called *raw strings* and treat backslashes as literal characters. As a result, in string literals, "'\U'" and "'\u'" escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the "'ur'" syntax is not supported. New in version 3.3: The "'rb'" prefix of raw bytes literals has been added as a synonym of "'br'". New in version 3.3: Support for the unicode legacy literal ("u'value'") was reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases. See **PEP 414** for more information. A string literal with "'f'" or "'F'" in its prefix is a *formatted string literal*; see Formatted string literals. The "'f'" may be combined with "'r'", but not with "'b'" or "'u'", therefore raw formatted strings are possible, but formatted bytes literals are not. In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either "'" or """.) Unless an "'r'" or "'R'" prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\newline" | Backslash and newline ignored | | +-------------------+-----------------------------------+---------+ | "\\" | Backslash ("\") | | +-------------------+-----------------------------------+---------+ | "\'" | Single quote ("'") | | +-------------------+-----------------------------------+---------+ | "\"" | Double quote (""") | | +-------------------+-----------------------------------+---------+ | "\a" | ASCII Bell (BEL) | | +-------------------+-----------------------------------+---------+ | "\b" | ASCII Backspace (BS) | | +-------------------+-----------------------------------+---------+ | "\f" | ASCII Formfeed (FF) | | +-------------------+-----------------------------------+---------+ | "\n" | ASCII Linefeed (LF) | | +-------------------+-----------------------------------+---------+ | "\r" | ASCII Carriage Return (CR) | | +-------------------+-----------------------------------+---------+ | "\t" | ASCII Horizontal Tab (TAB) | | +-------------------+-----------------------------------+---------+ | "\v" | ASCII Vertical Tab (VT) | | +-------------------+-----------------------------------+---------+ | "\ooo" | Character with octal value *ooo* | (1,3) | +-------------------+-----------------------------------+---------+ | "\xhh" | Character with hex value *hh* | (2,3) | +-------------------+-----------------------------------+---------+ Escape sequences only recognized in string literals are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\N{name}" | Character named *name* in the | (4) | | | Unicode database | | +-------------------+-----------------------------------+---------+ | "\uxxxx" | Character with 16-bit hex value | (5) | | | *xxxx* | | +-------------------+-----------------------------------+---------+ | "\Uxxxxxxxx" | Character with 32-bit hex value | (6) | | | *xxxxxxxx* | | +-------------------+-----------------------------------+---------+ Notes: 1. As in Standard C, up to three octal digits are accepted. 2. Unlike in Standard C, exactly two hex digits are required. 3. In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value. 4. Changed in version 3.3: Support for name aliases [1] has been added. 5. Exactly four hex digits are required. 6. Any Unicode character can be encoded this way. Exactly eight hex digits are required. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., *the backslash is left in the result*. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences only recognized in string literals fall into the category of unrecognized escapes for bytes literals. Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning. In some future version of Python they will be a SyntaxError. Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, "r"\""" is a valid string literal consisting of two characters: a backslash and a double quote; "r"\"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw literal cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, *not* as a line continuation. uMSubscriptions ************* A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object that supports subscription (lists or dictionaries for example). User-defined objects can support subscription by defining a "__getitem__()" method. For built-in objects, there are two types of objects that support subscription: If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression list must evaluate to an integer or a slice (as discussed in the following section). The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a "__getitem__()" method that interprets negative indices by adding the length of the sequence to the index (so that "x[-1]" selects the last item of "x"). The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). Since the support for negative indices and slicing occurs in the object’s "__getitem__()" method, subclasses overriding this method will need to explicitly add that support. A string’s items are characters. A character is not a separate data type but a string of exactly one character. axTruth Value Testing ******************* Any object can be tested for truth value, for use in an "if" or "while" condition or as operand of the Boolean operations below. By default, an object is considered true unless its class defines either a "__bool__()" method that returns "False" or a "__len__()" method that returns zero, when called with the object. [1] Here are most of the built-in objects considered false: * constants defined to be false: "None" and "False". * zero of any numeric type: "0", "0.0", "0j", "Decimal(0)", "Fraction(0, 1)" * empty sequences and collections: "''", "()", "[]", "{}", "set()", "range(0)" Operations and built-in functions that have a Boolean result always return "0" or "False" for false and "1" or "True" for true, unless otherwise stated. (Important exception: the Boolean operations "or" and "and" always return one of their operands.) u=The "try" statement ******************* The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified after the "as" keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) When an exception has been assigned using "as target", it is cleared at the end of the except clause. This is as if except E as N: foo was translated to except E as N: try: foo finally: del N This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs. Before an except clause’s suite is executed, details about the exception are stored in the "sys" module and can be accessed via "sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. "sys.exc_info()" values are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception, the saved exception is set as the context of the new exception. If the "finally" clause executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"…"finally" statement, the "finally" clause is also executed ‘on the way out.’ A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. uThe standard type hierarchy *************************** Below is a list of the types that are built into Python. Extension modules (written in C, Java, or other languages, depending on the implementation) can define additional types. Future versions of Python may add types to the type hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.), although such additions will often be provided via the standard library instead. Some of the type descriptions below contain a paragraph listing ‘special attributes.’ These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future. None This type has a single value. There is a single object with this value. This object is accessed through the built-in name "None". It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false. NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name "NotImplemented". Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. See Implementing the arithmetic operations for more details. Ellipsis This type has a single value. There is a single object with this value. This object is accessed through the literal "..." or the built-in name "Ellipsis". Its truth value is true. "numbers.Number" These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers. Python distinguishes between integers, floating point numbers, and complex numbers: "numbers.Integral" These represent elements from the mathematical set of integers (positive and negative). There are two types of integers: Integers ("int") These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2’s complement which gives the illusion of an infinite string of sign bits extending to the left. Booleans ("bool") These represent the truth values False and True. The two objects representing the values "False" and "True" are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ""False"" or ""True"" are returned, respectively. The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers. "numbers.Real" ("float") These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single- precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers. "numbers.Complex" ("complex") These represent complex numbers as a pair of machine-level double precision floating point numbers. The same caveats apply as for floating point numbers. The real and imaginary parts of a complex number "z" can be retrieved through the read-only attributes "z.real" and "z.imag". Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function "len()" returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* is selected by "a[i]". Sequences also support slicing: "a[i:j]" selects all items with index *k* such that *i* "<=" *k* "<" *j*. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support “extended slicing” with a third “step” parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*. Sequences are distinguished according to their mutability: Immutable sequences An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.) The following types are immutable sequences: Strings A string is a sequence of values that represent Unicode code points. All the code points in the range "U+0000 - U+10FFFF" can be represented in a string. Python doesn’t have a "char" type; instead, every code point in the string is represented as a string object with length "1". The built-in function "ord()" converts a code point from its string form to an integer in the range "0 - 10FFFF"; "chr()" converts an integer in the range "0 - 10FFFF" to the corresponding length "1" string object. "str.encode()" can be used to convert a "str" to "bytes" using the given text encoding, and "bytes.decode()" can be used to achieve the opposite. Tuples The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses. Bytes A bytes object is an immutable array. The items are 8-bit bytes, represented by integers in the range 0 <= x < 256. Bytes literals (like "b'abc'") and the built-in "bytes()" constructor can be used to create bytes objects. Also, bytes objects can be decoded to strings via the "decode()" method. Mutable sequences Mutable sequences can be changed after they are created. The subscription and slicing notations can be used as the target of assignment and "del" (delete) statements. There are currently two intrinsic mutable sequence types: Lists The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.) Byte Arrays A bytearray object is a mutable array. They are created by the built-in "bytearray()" constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable "bytes" objects. The extension module "array" provides an additional example of a mutable sequence type, as does the "collections" module. Set types These represent unordered, finite sets of unique, immutable objects. As such, they cannot be indexed by any subscript. However, they can be iterated over, and the built-in function "len()" returns the number of items in a set. Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. For set elements, the same immutability rules apply as for dictionary keys. Note that numeric types obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0"), only one of them can be contained in a set. There are currently two intrinsic set types: Sets These represent a mutable set. They are created by the built-in "set()" constructor and can be modified afterwards by several methods, such as "add()". Frozen sets These represent an immutable set. They are created by the built-in "frozenset()" constructor. As a frozenset is immutable and *hashable*, it can be used again as an element of another set, or as a dictionary key. Mappings These represent finite sets of objects indexed by arbitrary index sets. The subscript notation "a[k]" selects the item indexed by "k" from the mapping "a"; this can be used in expressions and as the target of assignments or "del" statements. The built-in function "len()" returns the number of items in a mapping. There is currently a single intrinsic mapping type: Dictionaries These represent finite sets of objects indexed by nearly arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key’s hash value to remain constant. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. Dictionaries are mutable; they can be created by the "{...}" notation (see section Dictionary displays). The extension modules "dbm.ndbm" and "dbm.gnu" provide additional examples of mapping types, as does the "collections" module. Callable types These are the types to which the function call operation (see section Calls) can be applied: User-defined functions A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function’s formal parameter list. Special attributes: +---------------------------+---------------------------------+-------------+ | Attribute | Meaning | | +===========================+=================================+=============+ | "__doc__" | The function’s documentation | Writable | | | string, or "None" if | | | | unavailable; not inherited by | | | | subclasses | | +---------------------------+---------------------------------+-------------+ | "__name__" | The function’s name | Writable | +---------------------------+---------------------------------+-------------+ | "__qualname__" | The function’s *qualified name* | Writable | | | New in version 3.3. | | +---------------------------+---------------------------------+-------------+ | "__module__" | The name of the module the | Writable | | | function was defined in, or | | | | "None" if unavailable. | | +---------------------------+---------------------------------+-------------+ | "__defaults__" | A tuple containing default | Writable | | | argument values for those | | | | arguments that have defaults, | | | | or "None" if no arguments have | | | | a default value | | +---------------------------+---------------------------------+-------------+ | "__code__" | The code object representing | Writable | | | the compiled function body. | | +---------------------------+---------------------------------+-------------+ | "__globals__" | A reference to the dictionary | Read-only | | | that holds the function’s | | | | global variables — the global | | | | namespace of the module in | | | | which the function was defined. | | +---------------------------+---------------------------------+-------------+ | "__dict__" | The namespace supporting | Writable | | | arbitrary function attributes. | | +---------------------------+---------------------------------+-------------+ | "__closure__" | "None" or a tuple of cells that | Read-only | | | contain bindings for the | | | | function’s free variables. | | +---------------------------+---------------------------------+-------------+ | "__annotations__" | A dict containing annotations | Writable | | | of parameters. The keys of the | | | | dict are the parameter names, | | | | and "'return'" for the return | | | | annotation, if provided. | | +---------------------------+---------------------------------+-------------+ | "__kwdefaults__" | A dict containing defaults for | Writable | | | keyword-only parameters. | | +---------------------------+---------------------------------+-------------+ Most of the attributes labelled “Writable” check the type of the assigned value. Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. *Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.* Additional information about a function’s definition can be retrieved from its code object; see the description of internal types below. Instance methods An instance method object combines a class, a class instance and any callable object (normally a user-defined function). Special read-only attributes: "__self__" is the class instance object, "__func__" is the function object; "__doc__" is the method’s documentation (same as "__func__.__doc__"); "__name__" is the method name (same as "__func__.__name__"); "__module__" is the name of the module the method was defined in, or "None" if unavailable. Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object. User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object. When an instance method object is created by retrieving a user- defined function object from a class via one of its instances, its "__self__" attribute is the instance, and the method object is said to be bound. The new method’s "__func__" attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the "__func__" attribute of the new instance is not the original method object but its "__func__" attribute. When an instance method object is created by retrieving a class method object from a class or instance, its "__self__" attribute is the class itself, and its "__func__" attribute is the function object underlying the class method. When an instance method object is called, the underlying function ("__func__") is called, inserting the class instance ("__self__") in front of the argument list. For instance, when "C" is a class which contains a definition for a function "f()", and "x" is an instance of "C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)". When an instance method object is derived from a class method object, the “class instance” stored in "__self__" will actually be the class itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to calling "f(C,1)" where "f" is the underlying function. Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non- callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this *only* happens when the function is an attribute of the class. Generator functions A function or method which uses the "yield" statement (see section The yield statement) is called a *generator function*. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s "iterator.__next__()" method will cause the function to execute until it provides a value using the "yield" statement. When the function executes a "return" statement or falls off the end, a "StopIteration" exception is raised and the iterator will have reached the end of the set of values to be returned. Coroutine functions A function or method which is defined using "async def" is called a *coroutine function*. Such a function, when called, returns a *coroutine* object. It may contain "await" expressions, as well as "async with" and "async for" statements. See also the Coroutine Objects section. Asynchronous generator functions A function or method which is defined using "async def" and which uses the "yield" statement is called a *asynchronous generator function*. Such a function, when called, returns an asynchronous iterator object which can be used in an "async for" statement to execute the body of the function. Calling the asynchronous iterator’s "aiterator.__anext__()" method will return an *awaitable* which when awaited will execute until it provides a value using the "yield" expression. When the function executes an empty "return" statement or falls off the end, a "StopAsyncIteration" exception is raised and the asynchronous iterator will have reached the end of the set of values to be yielded. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are "len()" and "math.sin()" ("math" is a standard built-in module). The number and type of the arguments are determined by the C function. Special read- only attributes: "__doc__" is the function’s documentation string, or "None" if unavailable; "__name__" is the function’s name; "__self__" is set to "None" (but see the next item); "__module__" is the name of the module the function was defined in or "None" if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is "alist.append()", assuming *alist* is a list object. In this case, the special read-only attribute "__self__" is set to the object denoted by *alist*. Classes Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override "__new__()". The arguments of the call are passed to "__new__()" and, in the typical case, to "__init__()" to initialize the new instance. Class Instances Instances of arbitrary classes can be made callable by defining a "__call__()" method in their class. Modules Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the "import" statement (see "import"), or by calling functions such as "importlib.import_module()" and built-in "__import__()". A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the "__globals__" attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done). Attribute assignment updates the module’s namespace dictionary, e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1". Predefined (writable) attributes: "__name__" is the module’s name; "__doc__" is the module’s documentation string, or "None" if unavailable; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during module body execution; "__file__" is the pathname of the file from which the module was loaded, if it was loaded from a file. The "__file__" attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file. Special read-only attribute: "__dict__" is the module’s namespace as a dictionary object. **CPython implementation detail:** Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly. Custom classes Custom class types are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., "C.x" is translated to "C.__dict__["x"]" (although there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes. This search of the base classes uses the C3 method resolution order which behaves correctly even in the presence of ‘diamond’ inheritance structures where there are multiple inheritance paths leading back to a common ancestor. Additional details on the C3 MRO used by Python can be found in the documentation accompanying the 2.3 release at https://www.python.org/download/releases/2.3/mro/. When a class attribute reference (for class "C", say) would yield a class method object, it is transformed into an instance method object whose "__self__" attribute is "C". When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section Implementing Descriptors for another way in which attributes retrieved from a class may differ from those actually contained in its "__dict__". Class attribute assignments update the class’s dictionary, never the dictionary of a base class. A class object can be called (see above) to yield a class instance (see below). Special attributes: "__name__" is the class name; "__module__" is the module name in which the class was defined; "__dict__" is the dictionary containing the class’s namespace; "__bases__" is a tuple containing the base classes, in the order of their occurrence in the base class list; "__doc__" is the class’s documentation string, or "None" if undefined; "__annotations__" (optional) is a dictionary containing *variable annotations* collected during class body execution. Class instances A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object, it is transformed into an instance method object whose "__self__" attribute is the instance. Static method and class method objects are also transformed; see above under “Classes”. See section Implementing Descriptors for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class’s "__dict__". If no class attribute is found, and the object’s class has a "__getattr__()" method, that is called to satisfy the lookup. Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. If the class has a "__setattr__()" or "__delattr__()" method, this is called instead of updating the instance dictionary directly. Class instances can pretend to be numbers, sequences, or mappings if they have methods with certain special names. See section Special method names. Special attributes: "__dict__" is the attribute dictionary; "__class__" is the instance’s class. I/O objects (also known as file objects) A *file object* represents an open file. Various shortcuts are available to create file objects: the "open()" built-in function, and also "os.popen()", "os.fdopen()", and the "makefile()" method of socket objects (and perhaps by other functions or methods provided by extension modules). The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized to file objects corresponding to the interpreter’s standard input, output and error streams; they are all open in text mode and therefore follow the interface defined by the "io.TextIOBase" abstract class. Internal types A few types used internally by the interpreter are exposed to the user. Their definitions may change with future versions of the interpreter, but they are mentioned here for completeness. Code objects Code objects represent *byte-compiled* executable Python code, or *bytecode*. The difference between a code object and a function object is that the function object contains an explicit reference to the function’s globals (the module in which it was defined), while a code object contains no context; also the default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time). Unlike function objects, code objects are immutable and contain no references (directly or indirectly) to mutable objects. Special read-only attributes: "co_name" gives the function name; "co_argcount" is the number of positional arguments (including arguments with default values); "co_nlocals" is the number of local variables used by the function (including arguments); "co_varnames" is a tuple containing the names of the local variables (starting with the argument names); "co_cellvars" is a tuple containing the names of local variables that are referenced by nested functions; "co_freevars" is a tuple containing the names of free variables; "co_code" is a string representing the sequence of bytecode instructions; "co_consts" is a tuple containing the literals used by the bytecode; "co_names" is a tuple containing the names used by the bytecode; "co_filename" is the filename from which the code was compiled; "co_firstlineno" is the first line number of the function; "co_lnotab" is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); "co_stacksize" is the required stack size (including local variables); "co_flags" is an integer encoding a number of flags for the interpreter. The following flag bits are defined for "co_flags": bit "0x04" is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit "0x08" is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; bit "0x20" is set if the function is a generator. Future feature declarations ("from __future__ import division") also use bits in "co_flags" to indicate whether a code object was compiled with a particular feature enabled: bit "0x2000" is set if the function was compiled with future division enabled; bits "0x10" and "0x1000" were used in earlier versions of Python. Other bits in "co_flags" are reserved for internal use. If a code object represents a function, the first item in "co_consts" is the documentation string of the function, or "None" if undefined. Frame objects Frame objects represent execution frames. They may occur in traceback objects (see below). Special read-only attributes: "f_back" is to the previous stack frame (towards the caller), or "None" if this is the bottom stack frame; "f_code" is the code object being executed in this frame; "f_locals" is the dictionary used to look up local variables; "f_globals" is used for global variables; "f_builtins" is used for built-in (intrinsic) names; "f_lasti" gives the precise instruction (this is an index into the bytecode string of the code object). Special writable attributes: "f_trace", if not "None", is a function called at the start of each source code line (this is used by the debugger); "f_lineno" is the current line number of the frame — writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno. Frame objects support one method: frame.clear() This method clears all references to local variables held by the frame. Also, if the frame belonged to a generator, the generator is finalized. This helps break reference cycles involving frame objects (for example when catching an exception and storing its traceback for later use). "RuntimeError" is raised if the frame is currently executing. New in version 3.4. Traceback objects Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as the third item of the tuple returned by "sys.exc_info()". When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as "sys.last_traceback". Special read-only attributes: "tb_next" is the next level in the stack trace (towards the frame where the exception occurred), or "None" if there is no next level; "tb_frame" points to the execution frame of the current level; "tb_lineno" gives the line number where the exception occurred; "tb_lasti" indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a "try" statement with no matching except clause or with a finally clause. Slice objects Slice objects are used to represent slices for "__getitem__()" methods. They are also created by the built-in "slice()" function. Special read-only attributes: "start" is the lower bound; "stop" is the upper bound; "step" is the step value; each is "None" if omitted. These attributes can have any type. Slice objects support one method: slice.indices(self, length) This method takes a single integer argument *length* and computes information about the slice that the slice object would describe if applied to a sequence of *length* items. It returns a tuple of three integers; respectively these are the *start* and *stop* indices and the *step* or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices. Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in "staticmethod()" constructor. Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under “User-defined methods”. Class method objects are created by the built-in "classmethod()" constructor. aFunctions ********* Function objects are created by function definitions. The only operation on a function object is to call it: "func(argument-list)". There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types. See Function definitions for more information. u8$Mapping Types — "dict" ********************** A *mapping* object maps *hashable* values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the *dictionary*. (For other containers see the built- in "list", "set", and "tuple" classes, and the "collections" module.) A dictionary’s keys are *almost* arbitrary values. Values that are not *hashable*, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) Dictionaries can be created by placing a comma-separated list of "key: value" pairs within braces, for example: "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}", or by the "dict" constructor. class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an *iterable* object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary. If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument. To illustrate, the following examples all return a dictionary equal to "{"one": 1, "two": 2, "three": 3}": >>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. These are the operations that dictionaries support (and therefore, custom mapping types should support too): len(d) Return the number of items in the dictionary *d*. d[key] Return the item of *d* with key *key*. Raises a "KeyError" if *key* is not in the map. If a subclass of dict defines a method "__missing__()" and *key* is not present, the "d[key]" operation calls that method with the key *key* as argument. The "d[key]" operation then returns or raises whatever is returned or raised by the "__missing__(key)" call. No other operations or methods invoke "__missing__()". If "__missing__()" is not defined, "KeyError" is raised. "__missing__()" must be a method; it cannot be an instance variable: >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 The example above shows part of the implementation of "collections.Counter". A different "__missing__" method is used by "collections.defaultdict". d[key] = value Set "d[key]" to *value*. del d[key] Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not in the map. key in d Return "True" if *d* has a key *key*, else "False". key not in d Equivalent to "not key in d". iter(d) Return an iterator over the keys of the dictionary. This is a shortcut for "iter(d.keys())". clear() Remove all items from the dictionary. copy() Return a shallow copy of the dictionary. classmethod fromkeys(seq[, value]) Create a new dictionary with keys from *seq* and values set to *value*. "fromkeys()" is a class method that returns a new dictionary. *value* defaults to "None". get(key[, default]) Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to "None", so that this method never raises a "KeyError". items() Return a new view of the dictionary’s items ("(key, value)" pairs). See the documentation of view objects. keys() Return a new view of the dictionary’s keys. See the documentation of view objects. pop(key[, default]) If *key* is in the dictionary, remove it and return its value, else return *default*. If *default* is not given and *key* is not in the dictionary, a "KeyError" is raised. popitem() Remove and return an arbitrary "(key, value)" pair from the dictionary. "popitem()" is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling "popitem()" raises a "KeyError". setdefault(key[, default]) If *key* is in the dictionary, return its value. If not, insert *key* with a value of *default* and return *default*. *default* defaults to "None". update([other]) Update the dictionary with the key/value pairs from *other*, overwriting existing keys. Return "None". "update()" accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: "d.update(red=1, blue=2)". values() Return a new view of the dictionary’s values. See the documentation of view objects. Dictionaries compare equal if and only if they have the same "(key, value)" pairs. Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise "TypeError". See also: "types.MappingProxyType" can be used to create a read-only view of a "dict". Dictionary view objects ======================= The objects returned by "dict.keys()", "dict.values()" and "dict.items()" are *view objects*. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data, and support membership tests: len(dictview) Return the number of entries in the dictionary. iter(dictview) Return an iterator over the keys, values or items (represented as tuples of "(key, value)") in the dictionary. Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.items()]". Iterating views while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. x in dictview Return "True" if *x* is in the underlying dictionary’s keys, values or items (in the latter case, *x* should be a "(key, value)" tuple). Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that "(key, value)" pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) For set-like views, all of the operations defined for the abstract base class "collections.abc.Set" are available (for example, "==", "<", or "^"). An example of dictionary view usage: >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.keys() >>> values = dishes.values() >>> # iteration >>> n = 0 >>> for val in values: ... n += val >>> print(n) 504 >>> # keys and values are iterated over in the same order >>> list(keys) ['eggs', 'bacon', 'sausage', 'spam'] >>> list(values) [2, 1, 1, 500] >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> del dishes['sausage'] >>> list(keys) ['spam', 'bacon'] >>> # set operations >>> keys & {'eggs', 'bacon', 'salad'} {'bacon'} >>> keys ^ {'sausage', 'juice'} {'juice', 'sausage', 'bacon', 'spam'} aMethods ******* Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as "append()" on lists) and class instance methods. Built-in methods are described with the types that support them. If you access a method (a function defined in a class namespace) through an instance, you get a special object: a *bound method* (also called *instance method*) object. When called, it will add the "self" argument to the argument list. Bound methods have two special read- only attributes: "m.__self__" is the object on which the method operates, and "m.__func__" is the function implementing the method. Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)". Like function objects, bound method objects support getting arbitrary attributes. However, since method attributes are actually stored on the underlying function object ("meth.__func__"), setting method attributes on bound methods is disallowed. Attempting to set an attribute on a method results in an "AttributeError" being raised. In order to set a method attribute, you need to explicitly set it on the underlying function object: >>> class C: ... def method(self): ... pass ... >>> c = C() >>> c.method.whoami = 'my name is method' # can't set on the method Traceback (most recent call last): File "", line 1, in AttributeError: 'method' object has no attribute 'whoami' >>> c.method.__func__.whoami = 'my name is method' >>> c.method.whoami 'my name is method' See The standard type hierarchy for more information. u$Modules ******* The only special operation on a module is attribute access: "m.name", where *m* is a module and *name* accesses a name defined in *m*’s symbol table. Module attributes can be assigned to. (Note that the "import" statement is not, strictly speaking, an operation on a module object; "import foo" does not require a module object named *foo* to exist, rather it requires an (external) *definition* for a module named *foo* somewhere.) A special attribute of every module is "__dict__". This is the dictionary containing the module’s symbol table. Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the "__dict__" attribute is not possible (you can write "m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can’t write "m.__dict__ = {}"). Modifying "__dict__" directly is not recommended. Modules built into the interpreter are written like this: "". If loaded from a file, they are written as "". uYSequence Types — "list", "tuple", "range" ***************************************** There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections. Common Sequence Operations ========================== The operations in the following table are supported by most sequence types, both mutable and immutable. The "collections.abc.Sequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. This table lists the sequence operations sorted in ascending priority. In the table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are integers and *x* is an arbitrary object that meets any type and value restrictions imposed by *s*. The "in" and "not in" operations have the same priorities as the comparison operations. The "+" (concatenation) and "*" (repetition) operations have the same priority as the corresponding numeric operations. [3] +----------------------------+----------------------------------+------------+ | Operation | Result | Notes | +============================+==================================+============+ | "x in s" | "True" if an item of *s* is | (1) | | | equal to *x*, else "False" | | +----------------------------+----------------------------------+------------+ | "x not in s" | "False" if an item of *s* is | (1) | | | equal to *x*, else "True" | | +----------------------------+----------------------------------+------------+ | "s + t" | the concatenation of *s* and *t* | (6)(7) | +----------------------------+----------------------------------+------------+ | "s * n" or "n * s" | equivalent to adding *s* to | (2)(7) | | | itself *n* times | | +----------------------------+----------------------------------+------------+ | "s[i]" | *i*th item of *s*, origin 0 | (3) | +----------------------------+----------------------------------+------------+ | "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) | +----------------------------+----------------------------------+------------+ | "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) | | | with step *k* | | +----------------------------+----------------------------------+------------+ | "len(s)" | length of *s* | | +----------------------------+----------------------------------+------------+ | "min(s)" | smallest item of *s* | | +----------------------------+----------------------------------+------------+ | "max(s)" | largest item of *s* | | +----------------------------+----------------------------------+------------+ | "s.index(x[, i[, j]])" | index of the first occurrence of | (8) | | | *x* in *s* (at or after index | | | | *i* and before index *j*) | | +----------------------------+----------------------------------+------------+ | "s.count(x)" | total number of occurrences of | | | | *x* in *s* | | +----------------------------+----------------------------------+------------+ Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.) Notes: 1. While the "in" and "not in" operations are used only for simple containment testing in the general case, some specialised sequences (such as "str", "bytes" and "bytearray") also use them for subsequence testing: >>> "gg" in "eggs" True 2. Values of *n* less than "0" are treated as "0" (which yields an empty sequence of the same type as *s*). Note that items in the sequence *s* are not copied; they are referenced multiple times. This often haunts new Python programmers; consider: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] What has happened is that "[[]]" is a one-element list containing an empty list, so all three elements of "[[]] * 3" are references to this single empty list. Modifying any of the elements of "lists" modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]] Further explanation is available in the FAQ entry How do I create a multidimensional list?. 3. If *i* or *j* is negative, the index is relative to the end of sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But note that "-0" is still "0". 4. The slice of *s* from *i* to *j* is defined as the sequence of items with index *k* such that "i <= k < j". If *i* or *j* is greater than "len(s)", use "len(s)". If *i* is omitted or "None", use "0". If *j* is omitted or "None", use "len(s)". If *i* is greater than or equal to *j*, the slice is empty. 5. The slice of *s* from *i* to *j* with step *k* is defined as the sequence of items with index "x = i + n*k" such that "0 <= n < (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k", "i+3*k" and so on, stopping when *j* is reached (but never including *j*). When *k* is positive, *i* and *j* are reduced to "len(s)" if they are greater. When *k* is negative, *i* and *j* are reduced to "len(s) - 1" if they are greater. If *i* or *j* are omitted or "None", they become “end” values (which end depends on the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it is treated like "1". 6. Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below: * if concatenating "str" objects, you can build a list and use "str.join()" at the end or else write to an "io.StringIO" instance and retrieve its value when complete * if concatenating "bytes" objects, you can similarly use "bytes.join()" or "io.BytesIO", or you can do in-place concatenation with a "bytearray" object. "bytearray" objects are mutable and have an efficient overallocation mechanism * if concatenating "tuple" objects, extend a "list" instead * for other types, investigate the relevant class documentation 7. Some sequence types (such as "range") only support item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition. 8. "index" raises "ValueError" when *x* is not found in *s*. Not all implementations support passing the additional arguments *i* and *j*. These arguments allow efficient searching of subsections of the sequence. Passing the extra arguments is roughly equivalent to using "s[i:j].index(x)", only without copying any data and with the returned index being relative to the start of the sequence rather than the start of the slice. Immutable Sequence Types ======================== The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the "hash()" built-in. This support allows immutable sequences, such as "tuple" instances, to be used as "dict" keys and stored in "set" and "frozenset" instances. Attempting to hash an immutable sequence that contains unhashable values will result in "TypeError". Mutable Sequence Types ====================== The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. Lists ===== Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application). class list([iterable]) Lists may be constructed in several ways: * Using a pair of square brackets to denote the empty list: "[]" * Using square brackets, separating items with commas: "[a]", "[a, b, c]" * Using a list comprehension: "[x for x in iterable]" * Using the type constructor: "list()" or "list(iterable)" The constructor builds a list whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a list, a copy is made and returned, similar to "iterable[:]". For example, "list('abc')" returns "['a', 'b', 'c']" and "list( (1, 2, 3) )" returns "[1, 2, 3]". If no argument is given, the constructor creates a new empty list, "[]". Many other operations also produce lists, including the "sorted()" built-in. Lists implement all of the common and mutable sequence operations. Lists also provide the following additional method: sort(*, key=None, reverse=False) This method sorts the list in place, using only "<" comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state). "sort()" accepts two arguments that can only be passed by keyword (keyword-only arguments): *key* specifies a function of one argument that is used to extract a comparison key from each list element (for example, "key=str.lower"). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of "None" means that list items are sorted directly without calculating a separate key value. The "functools.cmp_to_key()" utility is available to convert a 2.x style *cmp* function to a *key* function. *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use "sorted()" to explicitly request a new sorted list instance). The "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. Tuples ====== Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the "enumerate()" built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a "set" or "dict" instance). class tuple([iterable]) Tuples may be constructed in a number of ways: * Using a pair of parentheses to denote the empty tuple: "()" * Using a trailing comma for a singleton tuple: "a," or "(a,)" * Separating items with commas: "a, b, c" or "(a, b, c)" * Using the "tuple()" built-in: "tuple()" or "tuple(iterable)" The constructor builds a tuple whose items are the same and in the same order as *iterable*’s items. *iterable* may be either a sequence, a container that supports iteration, or an iterator object. If *iterable* is already a tuple, it is returned unchanged. For example, "tuple('abc')" returns "('a', 'b', 'c')" and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument is given, the constructor creates a new empty tuple, "()". Note that it is actually the comma which makes a tuple, not the parentheses. The parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity. For example, "f(a, b, c)" is a function call with three arguments, while "f((a, b, c))" is a function call with a 3-tuple as the sole argument. Tuples implement all of the common sequence operations. For heterogeneous collections of data where access by name is clearer than access by index, "collections.namedtuple()" may be a more appropriate choice than a simple tuple object. Ranges ====== The "range" type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in "for" loops. class range(stop) class range(start, stop[, step]) The arguments to the range constructor must be integers (either built-in "int" or any object that implements the "__index__" special method). If the *step* argument is omitted, it defaults to "1". If the *start* argument is omitted, it defaults to "0". If *step* is zero, "ValueError" is raised. For a positive *step*, the contents of a range "r" are determined by the formula "r[i] = start + step*i" where "i >= 0" and "r[i] < stop". For a negative *step*, the contents of the range are still determined by the formula "r[i] = start + step*i", but the constraints are "i >= 0" and "r[i] > stop". A range object will be empty if "r[0]" does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices. Ranges containing absolute values larger than "sys.maxsize" are permitted but some features (such as "len()") may raise "OverflowError". Range examples: >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(1, 11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] >>> list(range(0, 10, 3)) [0, 3, 6, 9] >>> list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> list(range(0)) [] >>> list(range(1, 0)) [] Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern). start The value of the *start* parameter (or "0" if the parameter was not supplied) stop The value of the *stop* parameter step The value of the *step* parameter (or "1" if the parameter was not supplied) The advantage of the "range" type over a regular "list" or "tuple" is that a "range" object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the "start", "stop" and "step" values, calculating individual items and subranges as needed). Range objects implement the "collections.abc.Sequence" ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices (see Sequence Types — list, tuple, range): >>> r = range(0, 20, 2) >>> r range(0, 20, 2) >>> 11 in r False >>> 10 in r True >>> r.index(10) 5 >>> r[5] 10 >>> r[:5] range(0, 10, 2) >>> r[-1] 18 Testing range objects for equality with "==" and "!=" compares them as sequences. That is, two range objects are considered equal if they represent the same sequence of values. (Note that two range objects that compare equal might have different "start", "stop" and "step" attributes, for example "range(0) == range(2, 1, 3)" or "range(0, 3, 2) == range(0, 4, 2)".) Changed in version 3.2: Implement the Sequence ABC. Support slicing and negative indices. Test "int" objects for membership in constant time instead of iterating through all items. Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define (instead of comparing based on object identity). New in version 3.3: The "start", "stop" and "step" attributes. See also: * The linspace recipe shows how to implement a lazy version of range suitable for floating point applications. usMutable Sequence Types ********************** The operations in the following table are defined on mutable sequence types. The "collections.abc.MutableSequence" ABC is provided to make it easier to correctly implement these operations on custom sequence types. In the table *s* is an instance of a mutable sequence type, *t* is any iterable object and *x* is an arbitrary object that meets any type and value restrictions imposed by *s* (for example, "bytearray" only accepts integers that meet the value restriction "0 <= x <= 255"). +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | appends *x* to the end of the | | | | sequence (same as | | | | "s[len(s):len(s)] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.clear()" | removes all items from *s* (same | (5) | | | as "del s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.copy()" | creates a shallow copy of *s* | (5) | | | (same as "s[:]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | extends *s* with the contents of | | | | *t* (for the most part the same | | | | as "s[len(s):len(s)] = t") | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (6) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | inserts *x* into *s* at the | | | | index given by *i* (same as | | | | "s[i:i] = [x]") | | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | retrieves the item at *i* and | (2) | | | also removes it from *s* | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | remove the first item from *s* | (3) | | | where "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (4) | | | place | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 3. "remove" raises "ValueError" when *x* is not found in *s*. 4. The "reverse()" method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence. 5. "clear()" and "copy()" are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as "dict" and "set") New in version 3.3: "clear()" and "copy()" methods. 6. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Common Sequence Operations. a~Unary arithmetic and bitwise operations *************************************** All unary arithmetic and bitwise operations have the same priority: u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr The unary "-" (minus) operator yields the negation of its numeric argument. The unary "+" (plus) operator yields its numeric argument unchanged. The unary "~" (invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of "x" is defined as "-(x+1)". It only applies to integral numbers. In all three cases, if the argument does not have the proper type, a "TypeError" exception is raised. uThe "while" statement ********************* The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause’s suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. u& The "with" statement ******************** The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"…"except"…"finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one “item” proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager’s "__exit__()" is loaded for later use. 3. The context manager’s "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager’s "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. See also: **PEP 343** - The “with” statement The specification, background, and examples for the Python "with" statement. a,The "yield" statement ********************* yield_stmt ::= yield_expression A "yield" statement is semantically equivalent to a yield expression. The yield statement can be used to omit the parentheses that would otherwise be required in the equivalent yield expression statement. For example, the yield statements yield yield from are equivalent to the yield expression statements (yield ) (yield from ) Yield expressions and statements are only used when defining a *generator* function, and are only used in the body of the generator function. Using yield in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. For full details of "yield" semantics, refer to the Yield expressions section. )MassertZ assignmentzatom-identifiersz atom-literalszattribute-accesszattribute-referencesZ augassignZbinaryZbitwisezbltin-code-objectszbltin-ellipsis-objectzbltin-null-objectzbltin-type-objectsZbooleansbreakzcallable-typesZcallsclassZ comparisonsZcompoundzcontext-managerscontinueZ conversionsZ customizationZdebuggerdeldictzdynamic-featureselse exceptionsZ execmodelZ exprlistsZfloatingforZ formatstringsZfunctionglobalz id-classesZ identifiersifZ imaginaryimportinZintegerslambdaZlistsZnamingnonlocalZnumbersz numeric-typesZobjectszoperator-summarypassZpowerraisereturnzsequence-typesZshiftingZslicingsZ specialattrsZ specialnameszstring-methodsZstringsZ subscriptionstruthtrytypesZtypesfunctionsZ typesmappingZ typesmethodsZ typesmodulesZtypesseqztypesseq-mutableZunarywhilewithyieldN)Ztopicsrr)/usr/lib64/python3.6/pydoc_data/topics.pys0't("@X   1 =`u >8,LC%GW*+0$.LA0"i#h3U?f9qg9%> JPKr|0]g9~~#__pycache__/__init__.cpython-36.pycnu[3 uj@sdS)Nrrr+/usr/lib64/python3.6/pydoc_data/__init__.pysPKr|0] __init__.pynu[PKr|0] DN N topics.pynu[# -*- coding: utf-8 -*- # Autogenerated by Sphinx on Sat Aug 26 11:16:28 2017 topics = {'assert': '\n' 'The "assert" statement\n' '**********************\n' '\n' 'Assert statements are a convenient way to insert debugging ' 'assertions\n' 'into a program:\n' '\n' ' assert_stmt ::= "assert" expression ["," expression]\n' '\n' 'The simple form, "assert expression", is equivalent to\n' '\n' ' if __debug__:\n' ' if not expression: raise AssertionError\n' '\n' 'The extended form, "assert expression1, expression2", is ' 'equivalent to\n' '\n' ' if __debug__:\n' ' if not expression1: raise AssertionError(expression2)\n' '\n' 'These equivalences assume that "__debug__" and "AssertionError" ' 'refer\n' 'to the built-in variables with those names. In the current\n' 'implementation, the built-in variable "__debug__" is "True" under\n' 'normal circumstances, "False" when optimization is requested ' '(command\n' 'line option -O). The current code generator emits no code for an\n' 'assert statement when optimization is requested at compile time. ' 'Note\n' 'that it is unnecessary to include the source code for the ' 'expression\n' 'that failed in the error message; it will be displayed as part of ' 'the\n' 'stack trace.\n' '\n' 'Assignments to "__debug__" are illegal. The value for the ' 'built-in\n' 'variable is determined when the interpreter starts.\n', 'assignment': '\n' 'Assignment statements\n' '*********************\n' '\n' 'Assignment statements are used to (re)bind names to values and ' 'to\n' 'modify attributes or items of mutable objects:\n' '\n' ' assignment_stmt ::= (target_list "=")+ (expression_list | ' 'yield_expression)\n' ' target_list ::= target ("," target)* [","]\n' ' target ::= identifier\n' ' | "(" target_list ")"\n' ' | "[" [target_list] "]"\n' ' | attributeref\n' ' | subscription\n' ' | slicing\n' '\n' '(See section Primaries for the syntax definitions for the last ' 'three\n' 'symbols.)\n' '\n' 'An assignment statement evaluates the expression list ' '(remember that\n' 'this can be a single expression or a comma-separated list, the ' 'latter\n' 'yielding a tuple) and assigns the single resulting object to ' 'each of\n' 'the target lists, from left to right.\n' '\n' 'Assignment is defined recursively depending on the form of the ' 'target\n' '(list). When a target is part of a mutable object (an ' 'attribute\n' 'reference, subscription or slicing), the mutable object must\n' 'ultimately perform the assignment and decide about its ' 'validity, and\n' 'may raise an exception if the assignment is unacceptable. The ' 'rules\n' 'observed by various types and the exceptions raised are given ' 'with the\n' 'definition of the object types (see section The standard type\n' 'hierarchy).\n' '\n' 'Assignment of an object to a target list is recursively ' 'defined as\n' 'follows.\n' '\n' '* If the target list is a single target: The object is ' 'assigned to\n' ' that target.\n' '\n' '* If the target list is a comma-separated list of targets: ' 'The\n' ' object must be an iterable with the same number of items as ' 'there\n' ' are targets in the target list, and the items are assigned, ' 'from\n' ' left to right, to the corresponding targets.\n' '\n' 'Assignment of an object to a single target is recursively ' 'defined as\n' 'follows.\n' '\n' '* If the target is an identifier (name):\n' '\n' ' * If the name does not occur in a "global" statement in the\n' ' current code block: the name is bound to the object in the ' 'current\n' ' local namespace.\n' '\n' ' * Otherwise: the name is bound to the object in the current ' 'global\n' ' namespace.\n' '\n' ' The name is rebound if it was already bound. This may cause ' 'the\n' ' reference count for the object previously bound to the name ' 'to reach\n' ' zero, causing the object to be deallocated and its ' 'destructor (if it\n' ' has one) to be called.\n' '\n' '* If the target is a target list enclosed in parentheses or ' 'in\n' ' square brackets: The object must be an iterable with the ' 'same number\n' ' of items as there are targets in the target list, and its ' 'items are\n' ' assigned, from left to right, to the corresponding targets.\n' '\n' '* If the target is an attribute reference: The primary ' 'expression in\n' ' the reference is evaluated. It should yield an object with\n' ' assignable attributes; if this is not the case, "TypeError" ' 'is\n' ' raised. That object is then asked to assign the assigned ' 'object to\n' ' the given attribute; if it cannot perform the assignment, it ' 'raises\n' ' an exception (usually but not necessarily ' '"AttributeError").\n' '\n' ' Note: If the object is a class instance and the attribute ' 'reference\n' ' occurs on both sides of the assignment operator, the RHS ' 'expression,\n' ' "a.x" can access either an instance attribute or (if no ' 'instance\n' ' attribute exists) a class attribute. The LHS target "a.x" ' 'is always\n' ' set as an instance attribute, creating it if necessary. ' 'Thus, the\n' ' two occurrences of "a.x" do not necessarily refer to the ' 'same\n' ' attribute: if the RHS expression refers to a class ' 'attribute, the\n' ' LHS creates a new instance attribute as the target of the\n' ' assignment:\n' '\n' ' class Cls:\n' ' x = 3 # class variable\n' ' inst = Cls()\n' ' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x ' 'as 3\n' '\n' ' This description does not necessarily apply to descriptor\n' ' attributes, such as properties created with "property()".\n' '\n' '* If the target is a subscription: The primary expression in ' 'the\n' ' reference is evaluated. It should yield either a mutable ' 'sequence\n' ' object (such as a list) or a mapping object (such as a ' 'dictionary).\n' ' Next, the subscript expression is evaluated.\n' '\n' ' If the primary is a mutable sequence object (such as a ' 'list), the\n' ' subscript must yield a plain integer. If it is negative, ' 'the\n' " sequence's length is added to it. The resulting value must " 'be a\n' " nonnegative integer less than the sequence's length, and " 'the\n' ' sequence is asked to assign the assigned object to its item ' 'with\n' ' that index. If the index is out of range, "IndexError" is ' 'raised\n' ' (assignment to a subscripted sequence cannot add new items ' 'to a\n' ' list).\n' '\n' ' If the primary is a mapping object (such as a dictionary), ' 'the\n' " subscript must have a type compatible with the mapping's key " 'type,\n' ' and the mapping is then asked to create a key/datum pair ' 'which maps\n' ' the subscript to the assigned object. This can either ' 'replace an\n' ' existing key/value pair with the same key value, or insert a ' 'new\n' ' key/value pair (if no key with the same value existed).\n' '\n' '* If the target is a slicing: The primary expression in the\n' ' reference is evaluated. It should yield a mutable sequence ' 'object\n' ' (such as a list). The assigned object should be a sequence ' 'object\n' ' of the same type. Next, the lower and upper bound ' 'expressions are\n' ' evaluated, insofar they are present; defaults are zero and ' 'the\n' " sequence's length. The bounds should evaluate to (small) " 'integers.\n' " If either bound is negative, the sequence's length is added " 'to it.\n' ' The resulting bounds are clipped to lie between zero and ' 'the\n' " sequence's length, inclusive. Finally, the sequence object " 'is asked\n' ' to replace the slice with the items of the assigned ' 'sequence. The\n' ' length of the slice may be different from the length of the ' 'assigned\n' ' sequence, thus changing the length of the target sequence, ' 'if the\n' ' object allows it.\n' '\n' '**CPython implementation detail:** In the current ' 'implementation, the\n' 'syntax for targets is taken to be the same as for expressions, ' 'and\n' 'invalid syntax is rejected during the code generation phase, ' 'causing\n' 'less detailed error messages.\n' '\n' 'WARNING: Although the definition of assignment implies that ' 'overlaps\n' "between the left-hand side and the right-hand side are 'safe' " '(for\n' 'example "a, b = b, a" swaps two variables), overlaps *within* ' 'the\n' 'collection of assigned-to variables are not safe! For ' 'instance, the\n' 'following program prints "[0, 2]":\n' '\n' ' x = [0, 1]\n' ' i = 0\n' ' i, x[i] = 1, 2\n' ' print x\n' '\n' '\n' 'Augmented assignment statements\n' '===============================\n' '\n' 'Augmented assignment is the combination, in a single ' 'statement, of a\n' 'binary operation and an assignment statement:\n' '\n' ' augmented_assignment_stmt ::= augtarget augop ' '(expression_list | yield_expression)\n' ' augtarget ::= identifier | attributeref | ' 'subscription | slicing\n' ' augop ::= "+=" | "-=" | "*=" | "/=" | ' '"//=" | "%=" | "**="\n' ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' '\n' '(See section Primaries for the syntax definitions for the last ' 'three\n' 'symbols.)\n' '\n' 'An augmented assignment evaluates the target (which, unlike ' 'normal\n' 'assignment statements, cannot be an unpacking) and the ' 'expression\n' 'list, performs the binary operation specific to the type of ' 'assignment\n' 'on the two operands, and assigns the result to the original ' 'target.\n' 'The target is only evaluated once.\n' '\n' 'An augmented assignment expression like "x += 1" can be ' 'rewritten as\n' '"x = x + 1" to achieve a similar, but not exactly equal ' 'effect. In the\n' 'augmented version, "x" is only evaluated once. Also, when ' 'possible,\n' 'the actual operation is performed *in-place*, meaning that ' 'rather than\n' 'creating a new object and assigning that to the target, the ' 'old object\n' 'is modified instead.\n' '\n' 'With the exception of assigning to tuples and multiple targets ' 'in a\n' 'single statement, the assignment done by augmented assignment\n' 'statements is handled the same way as normal assignments. ' 'Similarly,\n' 'with the exception of the possible *in-place* behavior, the ' 'binary\n' 'operation performed by augmented assignment is the same as the ' 'normal\n' 'binary operations.\n' '\n' 'For targets which are attribute references, the same caveat ' 'about\n' 'class and instance attributes applies as for regular ' 'assignments.\n', 'atom-identifiers': '\n' 'Identifiers (Names)\n' '*******************\n' '\n' 'An identifier occurring as an atom is a name. See ' 'section Identifiers\n' 'and keywords for lexical definition and section Naming ' 'and binding for\n' 'documentation of naming and binding.\n' '\n' 'When the name is bound to an object, evaluation of the ' 'atom yields\n' 'that object. When a name is not bound, an attempt to ' 'evaluate it\n' 'raises a "NameError" exception.\n' '\n' '**Private name mangling:** When an identifier that ' 'textually occurs in\n' 'a class definition begins with two or more underscore ' 'characters and\n' 'does not end in two or more underscores, it is ' 'considered a *private\n' 'name* of that class. Private names are transformed to a ' 'longer form\n' 'before code is generated for them. The transformation ' 'inserts the\n' 'class name, with leading underscores removed and a ' 'single underscore\n' 'inserted, in front of the name. For example, the ' 'identifier "__spam"\n' 'occurring in a class named "Ham" will be transformed to ' '"_Ham__spam".\n' 'This transformation is independent of the syntactical ' 'context in which\n' 'the identifier is used. If the transformed name is ' 'extremely long\n' '(longer than 255 characters), implementation defined ' 'truncation may\n' 'happen. If the class name consists only of underscores, ' 'no\n' 'transformation is done.\n', 'atom-literals': '\n' 'Literals\n' '********\n' '\n' 'Python supports string literals and various numeric ' 'literals:\n' '\n' ' literal ::= stringliteral | integer | longinteger\n' ' | floatnumber | imagnumber\n' '\n' 'Evaluation of a literal yields an object of the given type ' '(string,\n' 'integer, long integer, floating point number, complex ' 'number) with the\n' 'given value. The value may be approximated in the case of ' 'floating\n' 'point and imaginary (complex) literals. See section ' 'Literals for\n' 'details.\n' '\n' 'All literals correspond to immutable data types, and hence ' 'the\n' "object's identity is less important than its value. " 'Multiple\n' 'evaluations of literals with the same value (either the ' 'same\n' 'occurrence in the program text or a different occurrence) ' 'may obtain\n' 'the same object or a different object with the same ' 'value.\n', 'attribute-access': '\n' 'Customizing attribute access\n' '****************************\n' '\n' 'The following methods can be defined to customize the ' 'meaning of\n' 'attribute access (use of, assignment to, or deletion of ' '"x.name") for\n' 'class instances.\n' '\n' 'object.__getattr__(self, name)\n' '\n' ' Called when an attribute lookup has not found the ' 'attribute in the\n' ' usual places (i.e. it is not an instance attribute ' 'nor is it found\n' ' in the class tree for "self"). "name" is the ' 'attribute name. This\n' ' method should return the (computed) attribute value ' 'or raise an\n' ' "AttributeError" exception.\n' '\n' ' Note that if the attribute is found through the ' 'normal mechanism,\n' ' "__getattr__()" is not called. (This is an ' 'intentional asymmetry\n' ' between "__getattr__()" and "__setattr__()".) This is ' 'done both for\n' ' efficiency reasons and because otherwise ' '"__getattr__()" would have\n' ' no way to access other attributes of the instance. ' 'Note that at\n' ' least for instance variables, you can fake total ' 'control by not\n' ' inserting any values in the instance attribute ' 'dictionary (but\n' ' instead inserting them in another object). See the\n' ' "__getattribute__()" method below for a way to ' 'actually get total\n' ' control in new-style classes.\n' '\n' 'object.__setattr__(self, name, value)\n' '\n' ' Called when an attribute assignment is attempted. ' 'This is called\n' ' instead of the normal mechanism (i.e. store the value ' 'in the\n' ' instance dictionary). *name* is the attribute name, ' '*value* is the\n' ' value to be assigned to it.\n' '\n' ' If "__setattr__()" wants to assign to an instance ' 'attribute, it\n' ' should not simply execute "self.name = value" --- ' 'this would cause\n' ' a recursive call to itself. Instead, it should ' 'insert the value in\n' ' the dictionary of instance attributes, e.g., ' '"self.__dict__[name] =\n' ' value". For new-style classes, rather than accessing ' 'the instance\n' ' dictionary, it should call the base class method with ' 'the same\n' ' name, for example, "object.__setattr__(self, name, ' 'value)".\n' '\n' 'object.__delattr__(self, name)\n' '\n' ' Like "__setattr__()" but for attribute deletion ' 'instead of\n' ' assignment. This should only be implemented if "del ' 'obj.name" is\n' ' meaningful for the object.\n' '\n' '\n' 'More attribute access for new-style classes\n' '===========================================\n' '\n' 'The following methods only apply to new-style classes.\n' '\n' 'object.__getattribute__(self, name)\n' '\n' ' Called unconditionally to implement attribute ' 'accesses for\n' ' instances of the class. If the class also defines ' '"__getattr__()",\n' ' the latter will not be called unless ' '"__getattribute__()" either\n' ' calls it explicitly or raises an "AttributeError". ' 'This method\n' ' should return the (computed) attribute value or raise ' 'an\n' ' "AttributeError" exception. In order to avoid ' 'infinite recursion in\n' ' this method, its implementation should always call ' 'the base class\n' ' method with the same name to access any attributes it ' 'needs, for\n' ' example, "object.__getattribute__(self, name)".\n' '\n' ' Note: This method may still be bypassed when looking ' 'up special\n' ' methods as the result of implicit invocation via ' 'language syntax\n' ' or built-in functions. See Special method lookup ' 'for new-style\n' ' classes.\n' '\n' '\n' 'Implementing Descriptors\n' '========================\n' '\n' 'The following methods only apply when an instance of the ' 'class\n' 'containing the method (a so-called *descriptor* class) ' 'appears in an\n' '*owner* class (the descriptor must be in either the ' "owner's class\n" 'dictionary or in the class dictionary for one of its ' 'parents). In the\n' 'examples below, "the attribute" refers to the attribute ' 'whose name is\n' "the key of the property in the owner class' " '"__dict__".\n' '\n' 'object.__get__(self, instance, owner)\n' '\n' ' Called to get the attribute of the owner class (class ' 'attribute\n' ' access) or of an instance of that class (instance ' 'attribute\n' ' access). *owner* is always the owner class, while ' '*instance* is the\n' ' instance that the attribute was accessed through, or ' '"None" when\n' ' the attribute is accessed through the *owner*. This ' 'method should\n' ' return the (computed) attribute value or raise an ' '"AttributeError"\n' ' exception.\n' '\n' 'object.__set__(self, instance, value)\n' '\n' ' Called to set the attribute on an instance *instance* ' 'of the owner\n' ' class to a new value, *value*.\n' '\n' 'object.__delete__(self, instance)\n' '\n' ' Called to delete the attribute on an instance ' '*instance* of the\n' ' owner class.\n' '\n' '\n' 'Invoking Descriptors\n' '====================\n' '\n' 'In general, a descriptor is an object attribute with ' '"binding\n' 'behavior", one whose attribute access has been ' 'overridden by methods\n' 'in the descriptor protocol: "__get__()", "__set__()", ' 'and\n' '"__delete__()". If any of those methods are defined for ' 'an object, it\n' 'is said to be a descriptor.\n' '\n' 'The default behavior for attribute access is to get, ' 'set, or delete\n' "the attribute from an object's dictionary. For instance, " '"a.x" has a\n' 'lookup chain starting with "a.__dict__[\'x\']", then\n' '"type(a).__dict__[\'x\']", and continuing through the ' 'base classes of\n' '"type(a)" excluding metaclasses.\n' '\n' 'However, if the looked-up value is an object defining ' 'one of the\n' 'descriptor methods, then Python may override the default ' 'behavior and\n' 'invoke the descriptor method instead. Where this occurs ' 'in the\n' 'precedence chain depends on which descriptor methods ' 'were defined and\n' 'how they were called. Note that descriptors are only ' 'invoked for new\n' 'style objects or classes (ones that subclass "object()" ' 'or "type()").\n' '\n' 'The starting point for descriptor invocation is a ' 'binding, "a.x". How\n' 'the arguments are assembled depends on "a":\n' '\n' 'Direct Call\n' ' The simplest and least common call is when user code ' 'directly\n' ' invokes a descriptor method: "x.__get__(a)".\n' '\n' 'Instance Binding\n' ' If binding to a new-style object instance, "a.x" is ' 'transformed\n' ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' 'type(a))".\n' '\n' 'Class Binding\n' ' If binding to a new-style class, "A.x" is transformed ' 'into the\n' ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' '\n' 'Super Binding\n' ' If "a" is an instance of "super", then the binding ' '"super(B,\n' ' obj).m()" searches "obj.__class__.__mro__" for the ' 'base class "A"\n' ' immediately preceding "B" and then invokes the ' 'descriptor with the\n' ' call: "A.__dict__[\'m\'].__get__(obj, ' 'obj.__class__)".\n' '\n' 'For instance bindings, the precedence of descriptor ' 'invocation depends\n' 'on the which descriptor methods are defined. A ' 'descriptor can define\n' 'any combination of "__get__()", "__set__()" and ' '"__delete__()". If it\n' 'does not define "__get__()", then accessing the ' 'attribute will return\n' 'the descriptor object itself unless there is a value in ' "the object's\n" 'instance dictionary. If the descriptor defines ' '"__set__()" and/or\n' '"__delete__()", it is a data descriptor; if it defines ' 'neither, it is\n' 'a non-data descriptor. Normally, data descriptors ' 'define both\n' '"__get__()" and "__set__()", while non-data descriptors ' 'have just the\n' '"__get__()" method. Data descriptors with "__set__()" ' 'and "__get__()"\n' 'defined always override a redefinition in an instance ' 'dictionary. In\n' 'contrast, non-data descriptors can be overridden by ' 'instances.\n' '\n' 'Python methods (including "staticmethod()" and ' '"classmethod()") are\n' 'implemented as non-data descriptors. Accordingly, ' 'instances can\n' 'redefine and override methods. This allows individual ' 'instances to\n' 'acquire behaviors that differ from other instances of ' 'the same class.\n' '\n' 'The "property()" function is implemented as a data ' 'descriptor.\n' 'Accordingly, instances cannot override the behavior of a ' 'property.\n' '\n' '\n' '__slots__\n' '=========\n' '\n' 'By default, instances of both old and new-style classes ' 'have a\n' 'dictionary for attribute storage. This wastes space for ' 'objects\n' 'having very few instance variables. The space ' 'consumption can become\n' 'acute when creating large numbers of instances.\n' '\n' 'The default can be overridden by defining *__slots__* in ' 'a new-style\n' 'class definition. The *__slots__* declaration takes a ' 'sequence of\n' 'instance variables and reserves just enough space in ' 'each instance to\n' 'hold a value for each variable. Space is saved because ' '*__dict__* is\n' 'not created for each instance.\n' '\n' '__slots__\n' '\n' ' This class variable can be assigned a string, ' 'iterable, or sequence\n' ' of strings with variable names used by instances. If ' 'defined in a\n' ' new-style class, *__slots__* reserves space for the ' 'declared\n' ' variables and prevents the automatic creation of ' '*__dict__* and\n' ' *__weakref__* for each instance.\n' '\n' ' New in version 2.2.\n' '\n' 'Notes on using *__slots__*\n' '\n' '* When inheriting from a class without *__slots__*, the ' '*__dict__*\n' ' attribute of that class will always be accessible, so ' 'a *__slots__*\n' ' definition in the subclass is meaningless.\n' '\n' '* Without a *__dict__* variable, instances cannot be ' 'assigned new\n' ' variables not listed in the *__slots__* definition. ' 'Attempts to\n' ' assign to an unlisted variable name raises ' '"AttributeError". If\n' ' dynamic assignment of new variables is desired, then ' 'add\n' ' "\'__dict__\'" to the sequence of strings in the ' '*__slots__*\n' ' declaration.\n' '\n' ' Changed in version 2.3: Previously, adding ' '"\'__dict__\'" to the\n' ' *__slots__* declaration would not enable the ' 'assignment of new\n' ' attributes not specifically listed in the sequence of ' 'instance\n' ' variable names.\n' '\n' '* Without a *__weakref__* variable for each instance, ' 'classes\n' ' defining *__slots__* do not support weak references to ' 'its\n' ' instances. If weak reference support is needed, then ' 'add\n' ' "\'__weakref__\'" to the sequence of strings in the ' '*__slots__*\n' ' declaration.\n' '\n' ' Changed in version 2.3: Previously, adding ' '"\'__weakref__\'" to the\n' ' *__slots__* declaration would not enable support for ' 'weak\n' ' references.\n' '\n' '* *__slots__* are implemented at the class level by ' 'creating\n' ' descriptors (Implementing Descriptors) for each ' 'variable name. As a\n' ' result, class attributes cannot be used to set default ' 'values for\n' ' instance variables defined by *__slots__*; otherwise, ' 'the class\n' ' attribute would overwrite the descriptor assignment.\n' '\n' '* The action of a *__slots__* declaration is limited to ' 'the class\n' ' where it is defined. As a result, subclasses will ' 'have a *__dict__*\n' ' unless they also define *__slots__* (which must only ' 'contain names\n' ' of any *additional* slots).\n' '\n' '* If a class defines a slot also defined in a base ' 'class, the\n' ' instance variable defined by the base class slot is ' 'inaccessible\n' ' (except by retrieving its descriptor directly from the ' 'base class).\n' ' This renders the meaning of the program undefined. In ' 'the future, a\n' ' check may be added to prevent this.\n' '\n' '* Nonempty *__slots__* does not work for classes derived ' 'from\n' ' "variable-length" built-in types such as "long", "str" ' 'and "tuple".\n' '\n' '* Any non-string iterable may be assigned to ' '*__slots__*. Mappings\n' ' may also be used; however, in the future, special ' 'meaning may be\n' ' assigned to the values corresponding to each key.\n' '\n' '* *__class__* assignment works only if both classes have ' 'the same\n' ' *__slots__*.\n' '\n' ' Changed in version 2.6: Previously, *__class__* ' 'assignment raised an\n' ' error if either new or old class had *__slots__*.\n', 'attribute-references': '\n' 'Attribute references\n' '********************\n' '\n' 'An attribute reference is a primary followed by a ' 'period and a name:\n' '\n' ' attributeref ::= primary "." identifier\n' '\n' 'The primary must evaluate to an object of a type ' 'that supports\n' 'attribute references, e.g., a module, list, or an ' 'instance. This\n' 'object is then asked to produce the attribute whose ' 'name is the\n' 'identifier. If this attribute is not available, the ' 'exception\n' '"AttributeError" is raised. Otherwise, the type and ' 'value of the\n' 'object produced is determined by the object. ' 'Multiple evaluations of\n' 'the same attribute reference may yield different ' 'objects.\n', 'augassign': '\n' 'Augmented assignment statements\n' '*******************************\n' '\n' 'Augmented assignment is the combination, in a single statement, ' 'of a\n' 'binary operation and an assignment statement:\n' '\n' ' augmented_assignment_stmt ::= augtarget augop ' '(expression_list | yield_expression)\n' ' augtarget ::= identifier | attributeref | ' 'subscription | slicing\n' ' augop ::= "+=" | "-=" | "*=" | "/=" | ' '"//=" | "%=" | "**="\n' ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' '\n' '(See section Primaries for the syntax definitions for the last ' 'three\n' 'symbols.)\n' '\n' 'An augmented assignment evaluates the target (which, unlike ' 'normal\n' 'assignment statements, cannot be an unpacking) and the ' 'expression\n' 'list, performs the binary operation specific to the type of ' 'assignment\n' 'on the two operands, and assigns the result to the original ' 'target.\n' 'The target is only evaluated once.\n' '\n' 'An augmented assignment expression like "x += 1" can be ' 'rewritten as\n' '"x = x + 1" to achieve a similar, but not exactly equal effect. ' 'In the\n' 'augmented version, "x" is only evaluated once. Also, when ' 'possible,\n' 'the actual operation is performed *in-place*, meaning that ' 'rather than\n' 'creating a new object and assigning that to the target, the old ' 'object\n' 'is modified instead.\n' '\n' 'With the exception of assigning to tuples and multiple targets ' 'in a\n' 'single statement, the assignment done by augmented assignment\n' 'statements is handled the same way as normal assignments. ' 'Similarly,\n' 'with the exception of the possible *in-place* behavior, the ' 'binary\n' 'operation performed by augmented assignment is the same as the ' 'normal\n' 'binary operations.\n' '\n' 'For targets which are attribute references, the same caveat ' 'about\n' 'class and instance attributes applies as for regular ' 'assignments.\n', 'binary': '\n' 'Binary arithmetic operations\n' '****************************\n' '\n' 'The binary arithmetic operations have the conventional priority\n' 'levels. Note that some of these operations also apply to certain ' 'non-\n' 'numeric types. Apart from the power operator, there are only two\n' 'levels, one for multiplicative operators and one for additive\n' 'operators:\n' '\n' ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | ' 'm_expr "/" u_expr\n' ' | m_expr "%" u_expr\n' ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' '\n' 'The "*" (multiplication) operator yields the product of its ' 'arguments.\n' 'The arguments must either both be numbers, or one argument must be ' 'an\n' 'integer (plain or long) and the other must be a sequence. In the\n' 'former case, the numbers are converted to a common type and then\n' 'multiplied together. In the latter case, sequence repetition is\n' 'performed; a negative repetition factor yields an empty sequence.\n' '\n' 'The "/" (division) and "//" (floor division) operators yield the\n' 'quotient of their arguments. The numeric arguments are first\n' 'converted to a common type. Plain or long integer division yields ' 'an\n' 'integer of the same type; the result is that of mathematical ' 'division\n' "with the 'floor' function applied to the result. Division by zero\n" 'raises the "ZeroDivisionError" exception.\n' '\n' 'The "%" (modulo) operator yields the remainder from the division ' 'of\n' 'the first argument by the second. The numeric arguments are ' 'first\n' 'converted to a common type. A zero right argument raises the\n' '"ZeroDivisionError" exception. The arguments may be floating ' 'point\n' 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' '"4*0.7 +\n' '0.34".) The modulo operator always yields a result with the same ' 'sign\n' 'as its second operand (or zero); the absolute value of the result ' 'is\n' 'strictly smaller than the absolute value of the second operand ' '[2].\n' '\n' 'The integer division and modulo operators are connected by the\n' 'following identity: "x == (x/y)*y + (x%y)". Integer division and\n' 'modulo are also connected with the built-in function "divmod()":\n' '"divmod(x, y) == (x/y, x%y)". These identities don\'t hold for\n' 'floating point numbers; there similar identities hold ' 'approximately\n' 'where "x/y" is replaced by "floor(x/y)" or "floor(x/y) - 1" [3].\n' '\n' 'In addition to performing the modulo operation on numbers, the ' '"%"\n' 'operator is also overloaded by string and unicode objects to ' 'perform\n' 'string formatting (also known as interpolation). The syntax for ' 'string\n' 'formatting is described in the Python Library Reference, section\n' 'String Formatting Operations.\n' '\n' 'Deprecated since version 2.3: The floor division operator, the ' 'modulo\n' 'operator, and the "divmod()" function are no longer defined for\n' 'complex numbers. Instead, convert to a floating point number ' 'using\n' 'the "abs()" function if appropriate.\n' '\n' 'The "+" (addition) operator yields the sum of its arguments. The\n' 'arguments must either both be numbers or both sequences of the ' 'same\n' 'type. In the former case, the numbers are converted to a common ' 'type\n' 'and then added together. In the latter case, the sequences are\n' 'concatenated.\n' '\n' 'The "-" (subtraction) operator yields the difference of its ' 'arguments.\n' 'The numeric arguments are first converted to a common type.\n', 'bitwise': '\n' 'Binary bitwise operations\n' '*************************\n' '\n' 'Each of the three bitwise operations has a different priority ' 'level:\n' '\n' ' and_expr ::= shift_expr | and_expr "&" shift_expr\n' ' xor_expr ::= and_expr | xor_expr "^" and_expr\n' ' or_expr ::= xor_expr | or_expr "|" xor_expr\n' '\n' 'The "&" operator yields the bitwise AND of its arguments, which ' 'must\n' 'be plain or long integers. The arguments are converted to a ' 'common\n' 'type.\n' '\n' 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' 'arguments, which must be plain or long integers. The arguments ' 'are\n' 'converted to a common type.\n' '\n' 'The "|" operator yields the bitwise (inclusive) OR of its ' 'arguments,\n' 'which must be plain or long integers. The arguments are ' 'converted to\n' 'a common type.\n', 'bltin-code-objects': '\n' 'Code Objects\n' '************\n' '\n' 'Code objects are used by the implementation to ' 'represent "pseudo-\n' 'compiled" executable Python code such as a function ' 'body. They differ\n' "from function objects because they don't contain a " 'reference to their\n' 'global execution environment. Code objects are ' 'returned by the built-\n' 'in "compile()" function and can be extracted from ' 'function objects\n' 'through their "func_code" attribute. See also the ' '"code" module.\n' '\n' 'A code object can be executed or evaluated by passing ' 'it (instead of a\n' 'source string) to the "exec" statement or the built-in ' '"eval()"\n' 'function.\n' '\n' 'See The standard type hierarchy for more ' 'information.\n', 'bltin-ellipsis-object': '\n' 'The Ellipsis Object\n' '*******************\n' '\n' 'This object is used by extended slice notation (see ' 'Slicings). It\n' 'supports no special operations. There is exactly ' 'one ellipsis object,\n' 'named "Ellipsis" (a built-in name).\n' '\n' 'It is written as "Ellipsis". When in a subscript, ' 'it can also be\n' 'written as "...", for example "seq[...]".\n', 'bltin-file-objects': '\n' 'File Objects\n' '************\n' '\n' 'File objects are implemented using C\'s "stdio" ' 'package and can be\n' 'created with the built-in "open()" function. File ' 'objects are also\n' 'returned by some other built-in functions and methods, ' 'such as\n' '"os.popen()" and "os.fdopen()" and the "makefile()" ' 'method of socket\n' 'objects. Temporary files can be created using the ' '"tempfile" module,\n' 'and high-level file operations such as copying, ' 'moving, and deleting\n' 'files and directories can be achieved with the ' '"shutil" module.\n' '\n' 'When a file operation fails for an I/O-related reason, ' 'the exception\n' '"IOError" is raised. This includes situations where ' 'the operation is\n' 'not defined for some reason, like "seek()" on a tty ' 'device or writing\n' 'a file opened for reading.\n' '\n' 'Files have the following methods:\n' '\n' 'file.close()\n' '\n' ' Close the file. A closed file cannot be read or ' 'written any more.\n' ' Any operation which requires that the file be open ' 'will raise a\n' ' "ValueError" after the file has been closed. ' 'Calling "close()"\n' ' more than once is allowed.\n' '\n' ' As of Python 2.5, you can avoid having to call this ' 'method\n' ' explicitly if you use the "with" statement. For ' 'example, the\n' ' following code will automatically close *f* when ' 'the "with" block\n' ' is exited:\n' '\n' ' from __future__ import with_statement # This ' "isn't required in Python 2.6\n" '\n' ' with open("hello.txt") as f:\n' ' for line in f:\n' ' print line,\n' '\n' ' In older versions of Python, you would have needed ' 'to do this to\n' ' get the same effect:\n' '\n' ' f = open("hello.txt")\n' ' try:\n' ' for line in f:\n' ' print line,\n' ' finally:\n' ' f.close()\n' '\n' ' Note: Not all "file-like" types in Python support ' 'use as a\n' ' context manager for the "with" statement. If ' 'your code is\n' ' intended to work with any file-like object, you ' 'can use the\n' ' function "contextlib.closing()" instead of using ' 'the object\n' ' directly.\n' '\n' 'file.flush()\n' '\n' ' Flush the internal buffer, like "stdio"\'s ' '"fflush()". This may be\n' ' a no-op on some file-like objects.\n' '\n' ' Note: "flush()" does not necessarily write the ' "file's data to\n" ' disk. Use "flush()" followed by "os.fsync()" to ' 'ensure this\n' ' behavior.\n' '\n' 'file.fileno()\n' '\n' ' Return the integer "file descriptor" that is used ' 'by the underlying\n' ' implementation to request I/O operations from the ' 'operating system.\n' ' This can be useful for other, lower level ' 'interfaces that use file\n' ' descriptors, such as the "fcntl" module or ' '"os.read()" and friends.\n' '\n' ' Note: File-like objects which do not have a real ' 'file descriptor\n' ' should *not* provide this method!\n' '\n' 'file.isatty()\n' '\n' ' Return "True" if the file is connected to a ' 'tty(-like) device, else\n' ' "False".\n' '\n' ' Note: If a file-like object is not associated with ' 'a real file,\n' ' this method should *not* be implemented.\n' '\n' 'file.next()\n' '\n' ' A file object is its own iterator, for example ' '"iter(f)" returns\n' ' *f* (unless *f* is closed). When a file is used as ' 'an iterator,\n' ' typically in a "for" loop (for example, "for line ' 'in f: print\n' ' line.strip()"), the "next()" method is called ' 'repeatedly. This\n' ' method returns the next input line, or raises ' '"StopIteration" when\n' ' EOF is hit when the file is open for reading ' '(behavior is undefined\n' ' when the file is open for writing). In order to ' 'make a "for" loop\n' ' the most efficient way of looping over the lines of ' 'a file (a very\n' ' common operation), the "next()" method uses a ' 'hidden read-ahead\n' ' buffer. As a consequence of using a read-ahead ' 'buffer, combining\n' ' "next()" with other file methods (like ' '"readline()") does not work\n' ' right. However, using "seek()" to reposition the ' 'file to an\n' ' absolute position will flush the read-ahead ' 'buffer.\n' '\n' ' New in version 2.3.\n' '\n' 'file.read([size])\n' '\n' ' Read at most *size* bytes from the file (less if ' 'the read hits EOF\n' ' before obtaining *size* bytes). If the *size* ' 'argument is negative\n' ' or omitted, read all data until EOF is reached. ' 'The bytes are\n' ' returned as a string object. An empty string is ' 'returned when EOF\n' ' is encountered immediately. (For certain files, ' 'like ttys, it\n' ' makes sense to continue reading after an EOF is ' 'hit.) Note that\n' ' this method may call the underlying C function ' '"fread()" more than\n' ' once in an effort to acquire as close to *size* ' 'bytes as possible.\n' ' Also note that when in non-blocking mode, less data ' 'than was\n' ' requested may be returned, even if no *size* ' 'parameter was given.\n' '\n' ' Note: This function is simply a wrapper for the ' 'underlying\n' ' "fread()" C function, and will behave the same in ' 'corner cases,\n' ' such as whether the EOF value is cached.\n' '\n' 'file.readline([size])\n' '\n' ' Read one entire line from the file. A trailing ' 'newline character\n' ' is kept in the string (but may be absent when a ' 'file ends with an\n' ' incomplete line). [6] If the *size* argument is ' 'present and non-\n' ' negative, it is a maximum byte count (including the ' 'trailing\n' ' newline) and an incomplete line may be returned. ' 'When *size* is not\n' ' 0, an empty string is returned *only* when EOF is ' 'encountered\n' ' immediately.\n' '\n' ' Note: Unlike "stdio"\'s "fgets()", the returned ' 'string contains\n' ' null characters ("\'\\0\'") if they occurred in ' 'the input.\n' '\n' 'file.readlines([sizehint])\n' '\n' ' Read until EOF using "readline()" and return a list ' 'containing the\n' ' lines thus read. If the optional *sizehint* ' 'argument is present,\n' ' instead of reading up to EOF, whole lines totalling ' 'approximately\n' ' *sizehint* bytes (possibly after rounding up to an ' 'internal buffer\n' ' size) are read. Objects implementing a file-like ' 'interface may\n' ' choose to ignore *sizehint* if it cannot be ' 'implemented, or cannot\n' ' be implemented efficiently.\n' '\n' 'file.xreadlines()\n' '\n' ' This method returns the same thing as "iter(f)".\n' '\n' ' New in version 2.1.\n' '\n' ' Deprecated since version 2.3: Use "for line in ' 'file" instead.\n' '\n' 'file.seek(offset[, whence])\n' '\n' ' Set the file\'s current position, like "stdio"\'s ' '"fseek()". The\n' ' *whence* argument is optional and defaults to ' '"os.SEEK_SET" or "0"\n' ' (absolute file positioning); other values are ' '"os.SEEK_CUR" or "1"\n' ' (seek relative to the current position) and ' '"os.SEEK_END" or "2"\n' " (seek relative to the file's end). There is no " 'return value.\n' '\n' ' For example, "f.seek(2, os.SEEK_CUR)" advances the ' 'position by two\n' ' and "f.seek(-3, os.SEEK_END)" sets the position to ' 'the third to\n' ' last.\n' '\n' ' Note that if the file is opened for appending (mode ' '"\'a\'" or\n' ' "\'a+\'"), any "seek()" operations will be undone ' 'at the next write.\n' ' If the file is only opened for writing in append ' 'mode (mode "\'a\'"),\n' ' this method is essentially a no-op, but it remains ' 'useful for files\n' ' opened in append mode with reading enabled (mode ' '"\'a+\'"). If the\n' ' file is opened in text mode (without "\'b\'"), only ' 'offsets returned\n' ' by "tell()" are legal. Use of other offsets causes ' 'undefined\n' ' behavior.\n' '\n' ' Note that not all file objects are seekable.\n' '\n' ' Changed in version 2.6: Passing float values as ' 'offset has been\n' ' deprecated.\n' '\n' 'file.tell()\n' '\n' " Return the file's current position, like " '"stdio"\'s "ftell()".\n' '\n' ' Note: On Windows, "tell()" can return illegal ' 'values (after an\n' ' "fgets()") when reading files with Unix-style ' 'line-endings. Use\n' ' binary mode ("\'rb\'") to circumvent this ' 'problem.\n' '\n' 'file.truncate([size])\n' '\n' " Truncate the file's size. If the optional *size* " 'argument is\n' ' present, the file is truncated to (at most) that ' 'size. The size\n' ' defaults to the current position. The current file ' 'position is not\n' ' changed. Note that if a specified size exceeds the ' "file's current\n" ' size, the result is platform-dependent: ' 'possibilities include that\n' ' the file may remain unchanged, increase to the ' 'specified size as if\n' ' zero-filled, or increase to the specified size with ' 'undefined new\n' ' content. Availability: Windows, many Unix ' 'variants.\n' '\n' 'file.write(str)\n' '\n' ' Write a string to the file. There is no return ' 'value. Due to\n' ' buffering, the string may not actually show up in ' 'the file until\n' ' the "flush()" or "close()" method is called.\n' '\n' 'file.writelines(sequence)\n' '\n' ' Write a sequence of strings to the file. The ' 'sequence can be any\n' ' iterable object producing strings, typically a list ' 'of strings.\n' ' There is no return value. (The name is intended to ' 'match\n' ' "readlines()"; "writelines()" does not add line ' 'separators.)\n' '\n' 'Files support the iterator protocol. Each iteration ' 'returns the same\n' 'result as "readline()", and iteration ends when the ' '"readline()"\n' 'method returns an empty string.\n' '\n' 'File objects also offer a number of other interesting ' 'attributes.\n' 'These are not required for file-like objects, but ' 'should be\n' 'implemented if they make sense for the particular ' 'object.\n' '\n' 'file.closed\n' '\n' ' bool indicating the current state of the file ' 'object. This is a\n' ' read-only attribute; the "close()" method changes ' 'the value. It may\n' ' not be available on all file-like objects.\n' '\n' 'file.encoding\n' '\n' ' The encoding that this file uses. When Unicode ' 'strings are written\n' ' to a file, they will be converted to byte strings ' 'using this\n' ' encoding. In addition, when the file is connected ' 'to a terminal,\n' ' the attribute gives the encoding that the terminal ' 'is likely to use\n' ' (that information might be incorrect if the user ' 'has misconfigured\n' ' the terminal). The attribute is read-only and may ' 'not be present\n' ' on all file-like objects. It may also be "None", in ' 'which case the\n' ' file uses the system default encoding for ' 'converting Unicode\n' ' strings.\n' '\n' ' New in version 2.3.\n' '\n' 'file.errors\n' '\n' ' The Unicode error handler used along with the ' 'encoding.\n' '\n' ' New in version 2.6.\n' '\n' 'file.mode\n' '\n' ' The I/O mode for the file. If the file was created ' 'using the\n' ' "open()" built-in function, this will be the value ' 'of the *mode*\n' ' parameter. This is a read-only attribute and may ' 'not be present on\n' ' all file-like objects.\n' '\n' 'file.name\n' '\n' ' If the file object was created using "open()", the ' 'name of the\n' ' file. Otherwise, some string that indicates the ' 'source of the file\n' ' object, of the form "<...>". This is a read-only ' 'attribute and may\n' ' not be present on all file-like objects.\n' '\n' 'file.newlines\n' '\n' ' If Python was built with *universal newlines* ' 'enabled (the default)\n' ' this read-only attribute exists, and for files ' 'opened in universal\n' ' newline read mode it keeps track of the types of ' 'newlines\n' ' encountered while reading the file. The values it ' 'can take are\n' ' "\'\\r\'", "\'\\n\'", "\'\\r\\n\'", "None" ' '(unknown, no newlines read yet) or\n' ' a tuple containing all the newline types seen, to ' 'indicate that\n' ' multiple newline conventions were encountered. For ' 'files not opened\n' ' in universal newlines read mode the value of this ' 'attribute will be\n' ' "None".\n' '\n' 'file.softspace\n' '\n' ' Boolean that indicates whether a space character ' 'needs to be\n' ' printed before another value when using the "print" ' 'statement.\n' ' Classes that are trying to simulate a file object ' 'should also have\n' ' a writable "softspace" attribute, which should be ' 'initialized to\n' ' zero. This will be automatic for most classes ' 'implemented in\n' ' Python (care may be needed for objects that ' 'override attribute\n' ' access); types implemented in C will have to ' 'provide a writable\n' ' "softspace" attribute.\n' '\n' ' Note: This attribute is not used to control the ' '"print"\n' ' statement, but to allow the implementation of ' '"print" to keep\n' ' track of its internal state.\n', 'bltin-null-object': '\n' 'The Null Object\n' '***************\n' '\n' "This object is returned by functions that don't " 'explicitly return a\n' 'value. It supports no special operations. There is ' 'exactly one null\n' 'object, named "None" (a built-in name).\n' '\n' 'It is written as "None".\n', 'bltin-type-objects': '\n' 'Type Objects\n' '************\n' '\n' 'Type objects represent the various object types. An ' "object's type is\n" 'accessed by the built-in function "type()". There are ' 'no special\n' 'operations on types. The standard module "types" ' 'defines names for\n' 'all standard built-in types.\n' '\n' 'Types are written like this: "".\n', 'booleans': '\n' 'Boolean operations\n' '******************\n' '\n' ' or_test ::= and_test | or_test "or" and_test\n' ' and_test ::= not_test | and_test "and" not_test\n' ' not_test ::= comparison | "not" not_test\n' '\n' 'In the context of Boolean operations, and also when expressions ' 'are\n' 'used by control flow statements, the following values are ' 'interpreted\n' 'as false: "False", "None", numeric zero of all types, and empty\n' 'strings and containers (including strings, tuples, lists,\n' 'dictionaries, sets and frozensets). All other values are ' 'interpreted\n' 'as true. (See the "__nonzero__()" special method for a way to ' 'change\n' 'this.)\n' '\n' 'The operator "not" yields "True" if its argument is false, ' '"False"\n' 'otherwise.\n' '\n' 'The expression "x and y" first evaluates *x*; if *x* is false, ' 'its\n' 'value is returned; otherwise, *y* is evaluated and the resulting ' 'value\n' 'is returned.\n' '\n' 'The expression "x or y" first evaluates *x*; if *x* is true, its ' 'value\n' 'is returned; otherwise, *y* is evaluated and the resulting value ' 'is\n' 'returned.\n' '\n' '(Note that neither "and" nor "or" restrict the value and type ' 'they\n' 'return to "False" and "True", but rather return the last ' 'evaluated\n' 'argument. This is sometimes useful, e.g., if "s" is a string ' 'that\n' 'should be replaced by a default value if it is empty, the ' 'expression\n' '"s or \'foo\'" yields the desired value. Because "not" has to ' 'invent a\n' 'value anyway, it does not bother to return a value of the same ' 'type as\n' 'its argument, so e.g., "not \'foo\'" yields "False", not ' '"\'\'".)\n', 'break': '\n' 'The "break" statement\n' '*********************\n' '\n' ' break_stmt ::= "break"\n' '\n' '"break" may only occur syntactically nested in a "for" or "while"\n' 'loop, but not nested in a function or class definition within that\n' 'loop.\n' '\n' 'It terminates the nearest enclosing loop, skipping the optional ' '"else"\n' 'clause if the loop has one.\n' '\n' 'If a "for" loop is terminated by "break", the loop control target\n' 'keeps its current value.\n' '\n' 'When "break" passes control out of a "try" statement with a ' '"finally"\n' 'clause, that "finally" clause is executed before really leaving ' 'the\n' 'loop.\n', 'callable-types': '\n' 'Emulating callable objects\n' '**************************\n' '\n' 'object.__call__(self[, args...])\n' '\n' ' Called when the instance is "called" as a function; if ' 'this method\n' ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' ' "x.__call__(arg1, arg2, ...)".\n', 'calls': '\n' 'Calls\n' '*****\n' '\n' 'A call calls a callable object (e.g., a *function*) with a ' 'possibly\n' 'empty series of *arguments*:\n' '\n' ' call ::= primary "(" [argument_list [","]\n' ' | expression genexpr_for] ")"\n' ' argument_list ::= positional_arguments ["," ' 'keyword_arguments]\n' ' ["," "*" expression] ["," ' 'keyword_arguments]\n' ' ["," "**" expression]\n' ' | keyword_arguments ["," "*" expression]\n' ' ["," "**" expression]\n' ' | "*" expression ["," keyword_arguments] ["," ' '"**" expression]\n' ' | "**" expression\n' ' positional_arguments ::= expression ("," expression)*\n' ' keyword_arguments ::= keyword_item ("," keyword_item)*\n' ' keyword_item ::= identifier "=" expression\n' '\n' 'A trailing comma may be present after the positional and keyword\n' 'arguments but does not affect the semantics.\n' '\n' 'The primary must evaluate to a callable object (user-defined\n' 'functions, built-in functions, methods of built-in objects, class\n' 'objects, methods of class instances, and certain class instances\n' 'themselves are callable; extensions may define additional callable\n' 'object types). All argument expressions are evaluated before the ' 'call\n' 'is attempted. Please refer to section Function definitions for ' 'the\n' 'syntax of formal *parameter* lists.\n' '\n' 'If keyword arguments are present, they are first converted to\n' 'positional arguments, as follows. First, a list of unfilled slots ' 'is\n' 'created for the formal parameters. If there are N positional\n' 'arguments, they are placed in the first N slots. Next, for each\n' 'keyword argument, the identifier is used to determine the\n' 'corresponding slot (if the identifier is the same as the first ' 'formal\n' 'parameter name, the first slot is used, and so on). If the slot ' 'is\n' 'already filled, a "TypeError" exception is raised. Otherwise, the\n' 'value of the argument is placed in the slot, filling it (even if ' 'the\n' 'expression is "None", it fills the slot). When all arguments have\n' 'been processed, the slots that are still unfilled are filled with ' 'the\n' 'corresponding default value from the function definition. ' '(Default\n' 'values are calculated, once, when the function is defined; thus, a\n' 'mutable object such as a list or dictionary used as default value ' 'will\n' "be shared by all calls that don't specify an argument value for " 'the\n' 'corresponding slot; this should usually be avoided.) If there are ' 'any\n' 'unfilled slots for which no default value is specified, a ' '"TypeError"\n' 'exception is raised. Otherwise, the list of filled slots is used ' 'as\n' 'the argument list for the call.\n' '\n' '**CPython implementation detail:** An implementation may provide\n' 'built-in functions whose positional parameters do not have names, ' 'even\n' "if they are 'named' for the purpose of documentation, and which\n" 'therefore cannot be supplied by keyword. In CPython, this is the ' 'case\n' 'for functions implemented in C that use "PyArg_ParseTuple()" to ' 'parse\n' 'their arguments.\n' '\n' 'If there are more positional arguments than there are formal ' 'parameter\n' 'slots, a "TypeError" exception is raised, unless a formal ' 'parameter\n' 'using the syntax "*identifier" is present; in this case, that ' 'formal\n' 'parameter receives a tuple containing the excess positional ' 'arguments\n' '(or an empty tuple if there were no excess positional arguments).\n' '\n' 'If any keyword argument does not correspond to a formal parameter\n' 'name, a "TypeError" exception is raised, unless a formal parameter\n' 'using the syntax "**identifier" is present; in this case, that ' 'formal\n' 'parameter receives a dictionary containing the excess keyword\n' 'arguments (using the keywords as keys and the argument values as\n' 'corresponding values), or a (new) empty dictionary if there were ' 'no\n' 'excess keyword arguments.\n' '\n' 'If the syntax "*expression" appears in the function call, ' '"expression"\n' 'must evaluate to an iterable. Elements from this iterable are ' 'treated\n' 'as if they were additional positional arguments; if there are\n' 'positional arguments *x1*, ..., *xN*, and "expression" evaluates to ' 'a\n' 'sequence *y1*, ..., *yM*, this is equivalent to a call with M+N\n' 'positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*.\n' '\n' 'A consequence of this is that although the "*expression" syntax ' 'may\n' 'appear *after* some keyword arguments, it is processed *before* ' 'the\n' 'keyword arguments (and the "**expression" argument, if any -- see\n' 'below). So:\n' '\n' ' >>> def f(a, b):\n' ' ... print a, b\n' ' ...\n' ' >>> f(b=1, *(2,))\n' ' 2 1\n' ' >>> f(a=1, *(2,))\n' ' Traceback (most recent call last):\n' ' File "", line 1, in \n' " TypeError: f() got multiple values for keyword argument 'a'\n" ' >>> f(1, *(2,))\n' ' 1 2\n' '\n' 'It is unusual for both keyword arguments and the "*expression" ' 'syntax\n' 'to be used in the same call, so in practice this confusion does ' 'not\n' 'arise.\n' '\n' 'If the syntax "**expression" appears in the function call,\n' '"expression" must evaluate to a mapping, the contents of which are\n' 'treated as additional keyword arguments. In the case of a keyword\n' 'appearing in both "expression" and as an explicit keyword argument, ' 'a\n' '"TypeError" exception is raised.\n' '\n' 'Formal parameters using the syntax "*identifier" or "**identifier"\n' 'cannot be used as positional argument slots or as keyword argument\n' 'names. Formal parameters using the syntax "(sublist)" cannot be ' 'used\n' 'as keyword argument names; the outermost sublist corresponds to a\n' 'single unnamed argument slot, and the argument value is assigned ' 'to\n' 'the sublist using the usual tuple assignment rules after all other\n' 'parameter processing is done.\n' '\n' 'A call always returns some value, possibly "None", unless it raises ' 'an\n' 'exception. How this value is computed depends on the type of the\n' 'callable object.\n' '\n' 'If it is---\n' '\n' 'a user-defined function:\n' ' The code block for the function is executed, passing it the\n' ' argument list. The first thing the code block will do is bind ' 'the\n' ' formal parameters to the arguments; this is described in ' 'section\n' ' Function definitions. When the code block executes a "return"\n' ' statement, this specifies the return value of the function ' 'call.\n' '\n' 'a built-in function or method:\n' ' The result is up to the interpreter; see Built-in Functions for ' 'the\n' ' descriptions of built-in functions and methods.\n' '\n' 'a class object:\n' ' A new instance of that class is returned.\n' '\n' 'a class instance method:\n' ' The corresponding user-defined function is called, with an ' 'argument\n' ' list that is one longer than the argument list of the call: the\n' ' instance becomes the first argument.\n' '\n' 'a class instance:\n' ' The class must define a "__call__()" method; the effect is then ' 'the\n' ' same as if that method was called.\n', 'class': '\n' 'Class definitions\n' '*****************\n' '\n' 'A class definition defines a class object (see section The ' 'standard\n' 'type hierarchy):\n' '\n' ' classdef ::= "class" classname [inheritance] ":" suite\n' ' inheritance ::= "(" [expression_list] ")"\n' ' classname ::= identifier\n' '\n' 'A class definition is an executable statement. It first evaluates ' 'the\n' 'inheritance list, if present. Each item in the inheritance list\n' 'should evaluate to a class object or class type which allows\n' "subclassing. The class's suite is then executed in a new " 'execution\n' 'frame (see section Naming and binding), using a newly created ' 'local\n' 'namespace and the original global namespace. (Usually, the suite\n' "contains only function definitions.) When the class's suite " 'finishes\n' 'execution, its execution frame is discarded but its local namespace ' 'is\n' 'saved. [4] A class object is then created using the inheritance ' 'list\n' 'for the base classes and the saved local namespace for the ' 'attribute\n' 'dictionary. The class name is bound to this class object in the\n' 'original local namespace.\n' '\n' "**Programmer's note:** Variables defined in the class definition " 'are\n' 'class variables; they are shared by all instances. To create ' 'instance\n' 'variables, they can be set in a method with "self.name = value". ' 'Both\n' 'class and instance variables are accessible through the notation\n' '""self.name"", and an instance variable hides a class variable ' 'with\n' 'the same name when accessed in this way. Class variables can be ' 'used\n' 'as defaults for instance variables, but using mutable values there ' 'can\n' 'lead to unexpected results. For *new-style class*es, descriptors ' 'can\n' 'be used to create instance variables with different implementation\n' 'details.\n' '\n' 'Class definitions, like function definitions, may be wrapped by one ' 'or\n' 'more *decorator* expressions. The evaluation rules for the ' 'decorator\n' 'expressions are the same as for functions. The result must be a ' 'class\n' 'object, which is then bound to the class name.\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] The exception is propagated to the invocation stack unless\n' ' there is a "finally" clause which happens to raise another\n' ' exception. That new exception causes the old one to be lost.\n' '\n' '[2] Currently, control "flows off the end" except in the case of\n' ' an exception or the execution of a "return", "continue", or\n' ' "break" statement.\n' '\n' '[3] A string literal appearing as the first statement in the\n' ' function body is transformed into the function\'s "__doc__"\n' " attribute and therefore the function's *docstring*.\n" '\n' '[4] A string literal appearing as the first statement in the class\n' ' body is transformed into the namespace\'s "__doc__" item and\n' " therefore the class's *docstring*.\n", 'comparisons': '\n' 'Comparisons\n' '***********\n' '\n' 'Unlike C, all comparison operations in Python have the same ' 'priority,\n' 'which is lower than that of any arithmetic, shifting or ' 'bitwise\n' 'operation. Also unlike C, expressions like "a < b < c" have ' 'the\n' 'interpretation that is conventional in mathematics:\n' '\n' ' comparison ::= or_expr ( comp_operator or_expr )*\n' ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | ' '"!="\n' ' | "is" ["not"] | ["not"] "in"\n' '\n' 'Comparisons yield boolean values: "True" or "False".\n' '\n' 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' 'is\n' 'equivalent to "x < y and y <= z", except that "y" is ' 'evaluated only\n' 'once (but in both cases "z" is not evaluated at all when "x < ' 'y" is\n' 'found to be false).\n' '\n' 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and ' '*op1*,\n' '*op2*, ..., *opN* are comparison operators, then "a op1 b op2 ' 'c ... y\n' 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' 'z", except\n' 'that each expression is evaluated at most once.\n' '\n' 'Note that "a op1 b op2 c" doesn\'t imply any kind of ' 'comparison between\n' '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' '(though\n' 'perhaps not pretty).\n' '\n' 'The forms "<>" and "!=" are equivalent; for consistency with ' 'C, "!="\n' 'is preferred; where "!=" is mentioned below "<>" is also ' 'accepted.\n' 'The "<>" spelling is considered obsolescent.\n' '\n' '\n' 'Value comparisons\n' '=================\n' '\n' 'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' 'the values\n' 'of two objects. The objects do not need to have the same ' 'type.\n' '\n' 'Chapter Objects, values and types states that objects have a ' 'value (in\n' 'addition to type and identity). The value of an object is a ' 'rather\n' 'abstract notion in Python: For example, there is no canonical ' 'access\n' "method for an object's value. Also, there is no requirement " 'that the\n' 'value of an object should be constructed in a particular way, ' 'e.g.\n' 'comprised of all its data attributes. Comparison operators ' 'implement a\n' 'particular notion of what the value of an object is. One can ' 'think of\n' 'them as defining the value of an object indirectly, by means ' 'of their\n' 'comparison implementation.\n' '\n' 'Types can customize their comparison behavior by implementing ' 'a\n' '"__cmp__()" method or *rich comparison methods* like ' '"__lt__()",\n' 'described in Basic customization.\n' '\n' 'The default behavior for equality comparison ("==" and "!=") ' 'is based\n' 'on the identity of the objects. Hence, equality comparison ' 'of\n' 'instances with the same identity results in equality, and ' 'equality\n' 'comparison of instances with different identities results in\n' 'inequality. A motivation for this default behavior is the ' 'desire that\n' 'all objects should be reflexive (i.e. "x is y" implies "x == ' 'y").\n' '\n' 'The default order comparison ("<", ">", "<=", and ">=") gives ' 'a\n' 'consistent but arbitrary order.\n' '\n' '(This unusual definition of comparison was used to simplify ' 'the\n' 'definition of operations like sorting and the "in" and "not ' 'in"\n' 'operators. In the future, the comparison rules for objects ' 'of\n' 'different types are likely to change.)\n' '\n' 'The behavior of the default equality comparison, that ' 'instances with\n' 'different identities are always unequal, may be in contrast ' 'to what\n' 'types will need that have a sensible definition of object ' 'value and\n' 'value-based equality. Such types will need to customize ' 'their\n' 'comparison behavior, and in fact, a number of built-in types ' 'have done\n' 'that.\n' '\n' 'The following list describes the comparison behavior of the ' 'most\n' 'important built-in types.\n' '\n' '* Numbers of built-in numeric types (Numeric Types --- int, ' 'float,\n' ' long, complex) and of the standard library types\n' ' "fractions.Fraction" and "decimal.Decimal" can be compared ' 'within\n' ' and across their types, with the restriction that complex ' 'numbers do\n' ' not support order comparison. Within the limits of the ' 'types\n' ' involved, they compare mathematically (algorithmically) ' 'correct\n' ' without loss of precision.\n' '\n' '* Strings (instances of "str" or "unicode") compare\n' ' lexicographically using the numeric equivalents (the result ' 'of the\n' ' built-in function "ord()") of their characters. [4] When ' 'comparing\n' ' an 8-bit string and a Unicode string, the 8-bit string is ' 'converted\n' ' to Unicode. If the conversion fails, the strings are ' 'considered\n' ' unequal.\n' '\n' '* Instances of "tuple" or "list" can be compared only within ' 'each of\n' ' their types. Equality comparison across these types ' 'results in\n' ' unequality, and ordering comparison across these types ' 'gives an\n' ' arbitrary order.\n' '\n' ' These sequences compare lexicographically using comparison ' 'of\n' ' corresponding elements, whereby reflexivity of the elements ' 'is\n' ' enforced.\n' '\n' ' In enforcing reflexivity of elements, the comparison of ' 'collections\n' ' assumes that for a collection element "x", "x == x" is ' 'always true.\n' ' Based on that assumption, element identity is compared ' 'first, and\n' ' element comparison is performed only for distinct ' 'elements. This\n' ' approach yields the same result as a strict element ' 'comparison\n' ' would, if the compared elements are reflexive. For ' 'non-reflexive\n' ' elements, the result is different than for strict element\n' ' comparison.\n' '\n' ' Lexicographical comparison between built-in collections ' 'works as\n' ' follows:\n' '\n' ' * For two collections to compare equal, they must be of the ' 'same\n' ' type, have the same length, and each pair of ' 'corresponding\n' ' elements must compare equal (for example, "[1,2] == ' '(1,2)" is\n' ' false because the type is not the same).\n' '\n' ' * Collections are ordered the same as their first unequal ' 'elements\n' ' (for example, "cmp([1,2,x], [1,2,y])" returns the same ' 'as\n' ' "cmp(x,y)"). If a corresponding element does not exist, ' 'the\n' ' shorter collection is ordered first (for example, "[1,2] ' '<\n' ' [1,2,3]" is true).\n' '\n' '* Mappings (instances of "dict") compare equal if and only if ' 'they\n' ' have equal *(key, value)* pairs. Equality comparison of the ' 'keys and\n' ' values enforces reflexivity.\n' '\n' ' Outcomes other than equality are resolved consistently, but ' 'are not\n' ' otherwise defined. [5]\n' '\n' '* Most other objects of built-in types compare unequal unless ' 'they\n' ' are the same object; the choice whether one object is ' 'considered\n' ' smaller or larger than another one is made arbitrarily but\n' ' consistently within one execution of a program.\n' '\n' 'User-defined classes that customize their comparison behavior ' 'should\n' 'follow some consistency rules, if possible:\n' '\n' '* Equality comparison should be reflexive. In other words, ' 'identical\n' ' objects should compare equal:\n' '\n' ' "x is y" implies "x == y"\n' '\n' '* Comparison should be symmetric. In other words, the ' 'following\n' ' expressions should have the same result:\n' '\n' ' "x == y" and "y == x"\n' '\n' ' "x != y" and "y != x"\n' '\n' ' "x < y" and "y > x"\n' '\n' ' "x <= y" and "y >= x"\n' '\n' '* Comparison should be transitive. The following ' '(non-exhaustive)\n' ' examples illustrate that:\n' '\n' ' "x > y and y > z" implies "x > z"\n' '\n' ' "x < y and y <= z" implies "x < z"\n' '\n' '* Inverse comparison should result in the boolean negation. ' 'In other\n' ' words, the following expressions should have the same ' 'result:\n' '\n' ' "x == y" and "not x != y"\n' '\n' ' "x < y" and "not x >= y" (for total ordering)\n' '\n' ' "x > y" and "not x <= y" (for total ordering)\n' '\n' ' The last two expressions apply to totally ordered ' 'collections (e.g.\n' ' to sequences, but not to sets or mappings). See also the\n' ' "total_ordering()" decorator.\n' '\n' '* The "hash()" result should be consistent with equality. ' 'Objects\n' ' that are equal should either have the same hash value, or ' 'be marked\n' ' as unhashable.\n' '\n' 'Python does not enforce these consistency rules.\n' '\n' '\n' 'Membership test operations\n' '==========================\n' '\n' 'The operators "in" and "not in" test for membership. "x in ' 's"\n' 'evaluates to "True" if *x* is a member of *s*, and "False" ' 'otherwise.\n' '"x not in s" returns the negation of "x in s". All built-in ' 'sequences\n' 'and set types support this as well as dictionary, for which ' '"in" tests\n' 'whether the dictionary has a given key. For container types ' 'such as\n' 'list, tuple, set, frozenset, dict, or collections.deque, the\n' 'expression "x in y" is equivalent to "any(x is e or x == e ' 'for e in\n' 'y)".\n' '\n' 'For the string and bytes types, "x in y" is "True" if and ' 'only if *x*\n' 'is a substring of *y*. An equivalent test is "y.find(x) != ' '-1".\n' 'Empty strings are always considered to be a substring of any ' 'other\n' 'string, so """ in "abc"" will return "True".\n' '\n' 'For user-defined classes which define the "__contains__()" ' 'method, "x\n' 'in y" returns "True" if "y.__contains__(x)" returns a true ' 'value, and\n' '"False" otherwise.\n' '\n' 'For user-defined classes which do not define "__contains__()" ' 'but do\n' 'define "__iter__()", "x in y" is "True" if some value "z" ' 'with "x ==\n' 'z" is produced while iterating over "y". If an exception is ' 'raised\n' 'during the iteration, it is as if "in" raised that ' 'exception.\n' '\n' 'Lastly, the old-style iteration protocol is tried: if a class ' 'defines\n' '"__getitem__()", "x in y" is "True" if and only if there is a ' 'non-\n' 'negative integer index *i* such that "x == y[i]", and all ' 'lower\n' 'integer indices do not raise "IndexError" exception. (If any ' 'other\n' 'exception is raised, it is as if "in" raised that ' 'exception).\n' '\n' 'The operator "not in" is defined to have the inverse true ' 'value of\n' '"in".\n' '\n' '\n' 'Identity comparisons\n' '====================\n' '\n' 'The operators "is" and "is not" test for object identity: "x ' 'is y" is\n' 'true if and only if *x* and *y* are the same object. "x is ' 'not y"\n' 'yields the inverse truth value. [6]\n', 'compound': '\n' 'Compound statements\n' '*******************\n' '\n' 'Compound statements contain (groups of) other statements; they ' 'affect\n' 'or control the execution of those other statements in some way. ' 'In\n' 'general, compound statements span multiple lines, although in ' 'simple\n' 'incarnations a whole compound statement may be contained in one ' 'line.\n' '\n' 'The "if", "while" and "for" statements implement traditional ' 'control\n' 'flow constructs. "try" specifies exception handlers and/or ' 'cleanup\n' 'code for a group of statements. Function and class definitions ' 'are\n' 'also syntactically compound statements.\n' '\n' "Compound statements consist of one or more 'clauses.' A clause\n" "consists of a header and a 'suite.' The clause headers of a\n" 'particular compound statement are all at the same indentation ' 'level.\n' 'Each clause header begins with a uniquely identifying keyword ' 'and ends\n' 'with a colon. A suite is a group of statements controlled by a\n' 'clause. A suite can be one or more semicolon-separated simple\n' 'statements on the same line as the header, following the ' "header's\n" 'colon, or it can be one or more indented statements on ' 'subsequent\n' 'lines. Only the latter form of suite can contain nested ' 'compound\n' "statements; the following is illegal, mostly because it wouldn't " 'be\n' 'clear to which "if" clause a following "else" clause would ' 'belong:\n' '\n' ' if test1: if test2: print x\n' '\n' 'Also note that the semicolon binds tighter than the colon in ' 'this\n' 'context, so that in the following example, either all or none of ' 'the\n' '"print" statements are executed:\n' '\n' ' if x < y < z: print x; print y; print z\n' '\n' 'Summarizing:\n' '\n' ' compound_stmt ::= if_stmt\n' ' | while_stmt\n' ' | for_stmt\n' ' | try_stmt\n' ' | with_stmt\n' ' | funcdef\n' ' | classdef\n' ' | decorated\n' ' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' 'statement+ DEDENT\n' ' statement ::= stmt_list NEWLINE | compound_stmt\n' ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' '\n' 'Note that statements always end in a "NEWLINE" possibly followed ' 'by a\n' '"DEDENT". Also note that optional continuation clauses always ' 'begin\n' 'with a keyword that cannot start a statement, thus there are no\n' 'ambiguities (the \'dangling "else"\' problem is solved in Python ' 'by\n' 'requiring nested "if" statements to be indented).\n' '\n' 'The formatting of the grammar rules in the following sections ' 'places\n' 'each clause on a separate line for clarity.\n' '\n' '\n' 'The "if" statement\n' '==================\n' '\n' 'The "if" statement is used for conditional execution:\n' '\n' ' if_stmt ::= "if" expression ":" suite\n' ' ( "elif" expression ":" suite )*\n' ' ["else" ":" suite]\n' '\n' 'It selects exactly one of the suites by evaluating the ' 'expressions one\n' 'by one until one is found to be true (see section Boolean ' 'operations\n' 'for the definition of true and false); then that suite is ' 'executed\n' '(and no other part of the "if" statement is executed or ' 'evaluated).\n' 'If all expressions are false, the suite of the "else" clause, ' 'if\n' 'present, is executed.\n' '\n' '\n' 'The "while" statement\n' '=====================\n' '\n' 'The "while" statement is used for repeated execution as long as ' 'an\n' 'expression is true:\n' '\n' ' while_stmt ::= "while" expression ":" suite\n' ' ["else" ":" suite]\n' '\n' 'This repeatedly tests the expression and, if it is true, ' 'executes the\n' 'first suite; if the expression is false (which may be the first ' 'time\n' 'it is tested) the suite of the "else" clause, if present, is ' 'executed\n' 'and the loop terminates.\n' '\n' 'A "break" statement executed in the first suite terminates the ' 'loop\n' 'without executing the "else" clause\'s suite. A "continue" ' 'statement\n' 'executed in the first suite skips the rest of the suite and goes ' 'back\n' 'to testing the expression.\n' '\n' '\n' 'The "for" statement\n' '===================\n' '\n' 'The "for" statement is used to iterate over the elements of a ' 'sequence\n' '(such as a string, tuple or list) or other iterable object:\n' '\n' ' for_stmt ::= "for" target_list "in" expression_list ":" ' 'suite\n' ' ["else" ":" suite]\n' '\n' 'The expression list is evaluated once; it should yield an ' 'iterable\n' 'object. An iterator is created for the result of the\n' '"expression_list". The suite is then executed once for each ' 'item\n' 'provided by the iterator, in the order of ascending indices. ' 'Each\n' 'item in turn is assigned to the target list using the standard ' 'rules\n' 'for assignments, and then the suite is executed. When the items ' 'are\n' 'exhausted (which is immediately when the sequence is empty), the ' 'suite\n' 'in the "else" clause, if present, is executed, and the loop\n' 'terminates.\n' '\n' 'A "break" statement executed in the first suite terminates the ' 'loop\n' 'without executing the "else" clause\'s suite. A "continue" ' 'statement\n' 'executed in the first suite skips the rest of the suite and ' 'continues\n' 'with the next item, or with the "else" clause if there was no ' 'next\n' 'item.\n' '\n' 'The suite may assign to the variable(s) in the target list; this ' 'does\n' 'not affect the next item assigned to it.\n' '\n' 'The target list is not deleted when the loop is finished, but if ' 'the\n' 'sequence is empty, it will not have been assigned to at all by ' 'the\n' 'loop. Hint: the built-in function "range()" returns a sequence ' 'of\n' 'integers suitable to emulate the effect of Pascal\'s "for i := a ' 'to b\n' 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' '\n' 'Note: There is a subtlety when the sequence is being modified by ' 'the\n' ' loop (this can only occur for mutable sequences, i.e. lists). ' 'An\n' ' internal counter is used to keep track of which item is used ' 'next,\n' ' and this is incremented on each iteration. When this counter ' 'has\n' ' reached the length of the sequence the loop terminates. This ' 'means\n' ' that if the suite deletes the current (or a previous) item ' 'from the\n' ' sequence, the next item will be skipped (since it gets the ' 'index of\n' ' the current item which has already been treated). Likewise, ' 'if the\n' ' suite inserts an item in the sequence before the current item, ' 'the\n' ' current item will be treated again the next time through the ' 'loop.\n' ' This can lead to nasty bugs that can be avoided by making a\n' ' temporary copy using a slice of the whole sequence, e.g.,\n' '\n' ' for x in a[:]:\n' ' if x < 0: a.remove(x)\n' '\n' '\n' 'The "try" statement\n' '===================\n' '\n' 'The "try" statement specifies exception handlers and/or cleanup ' 'code\n' 'for a group of statements:\n' '\n' ' try_stmt ::= try1_stmt | try2_stmt\n' ' try1_stmt ::= "try" ":" suite\n' ' ("except" [expression [("as" | ",") ' 'identifier]] ":" suite)+\n' ' ["else" ":" suite]\n' ' ["finally" ":" suite]\n' ' try2_stmt ::= "try" ":" suite\n' ' "finally" ":" suite\n' '\n' 'Changed in version 2.5: In previous versions of Python,\n' '"try"..."except"..."finally" did not work. "try"..."except" had ' 'to be\n' 'nested in "try"..."finally".\n' '\n' 'The "except" clause(s) specify one or more exception handlers. ' 'When no\n' 'exception occurs in the "try" clause, no exception handler is\n' 'executed. When an exception occurs in the "try" suite, a search ' 'for an\n' 'exception handler is started. This search inspects the except ' 'clauses\n' 'in turn until one is found that matches the exception. An ' 'expression-\n' 'less except clause, if present, must be last; it matches any\n' 'exception. For an except clause with an expression, that ' 'expression\n' 'is evaluated, and the clause matches the exception if the ' 'resulting\n' 'object is "compatible" with the exception. An object is ' 'compatible\n' 'with an exception if it is the class or a base class of the ' 'exception\n' 'object, or a tuple containing an item compatible with the ' 'exception.\n' '\n' 'If no except clause matches the exception, the search for an ' 'exception\n' 'handler continues in the surrounding code and on the invocation ' 'stack.\n' '[1]\n' '\n' 'If the evaluation of an expression in the header of an except ' 'clause\n' 'raises an exception, the original search for a handler is ' 'canceled and\n' 'a search starts for the new exception in the surrounding code ' 'and on\n' 'the call stack (it is treated as if the entire "try" statement ' 'raised\n' 'the exception).\n' '\n' 'When a matching except clause is found, the exception is ' 'assigned to\n' 'the target specified in that except clause, if present, and the ' 'except\n' "clause's suite is executed. All except clauses must have an\n" 'executable block. When the end of this block is reached, ' 'execution\n' 'continues normally after the entire try statement. (This means ' 'that\n' 'if two nested handlers exist for the same exception, and the ' 'exception\n' 'occurs in the try clause of the inner handler, the outer handler ' 'will\n' 'not handle the exception.)\n' '\n' "Before an except clause's suite is executed, details about the\n" 'exception are assigned to three variables in the "sys" module:\n' '"sys.exc_type" receives the object identifying the exception;\n' '"sys.exc_value" receives the exception\'s parameter;\n' '"sys.exc_traceback" receives a traceback object (see section ' 'The\n' 'standard type hierarchy) identifying the point in the program ' 'where\n' 'the exception occurred. These details are also available through ' 'the\n' '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' 'exc_value, exc_traceback)". Use of the corresponding variables ' 'is\n' 'deprecated in favor of this function, since their use is unsafe ' 'in a\n' 'threaded program. As of Python 1.5, the variables are restored ' 'to\n' 'their previous values (before the call) when returning from a ' 'function\n' 'that handled an exception.\n' '\n' 'The optional "else" clause is executed if and when control flows ' 'off\n' 'the end of the "try" clause. [2] Exceptions in the "else" clause ' 'are\n' 'not handled by the preceding "except" clauses.\n' '\n' 'If "finally" is present, it specifies a \'cleanup\' handler. ' 'The "try"\n' 'clause is executed, including any "except" and "else" clauses. ' 'If an\n' 'exception occurs in any of the clauses and is not handled, the\n' 'exception is temporarily saved. The "finally" clause is ' 'executed. If\n' 'there is a saved exception, it is re-raised at the end of the\n' '"finally" clause. If the "finally" clause raises another ' 'exception or\n' 'executes a "return" or "break" statement, the saved exception ' 'is\n' 'discarded:\n' '\n' ' >>> def f():\n' ' ... try:\n' ' ... 1/0\n' ' ... finally:\n' ' ... return 42\n' ' ...\n' ' >>> f()\n' ' 42\n' '\n' 'The exception information is not available to the program ' 'during\n' 'execution of the "finally" clause.\n' '\n' 'When a "return", "break" or "continue" statement is executed in ' 'the\n' '"try" suite of a "try"..."finally" statement, the "finally" ' 'clause is\n' 'also executed \'on the way out.\' A "continue" statement is ' 'illegal in\n' 'the "finally" clause. (The reason is a problem with the current\n' 'implementation --- this restriction may be lifted in the ' 'future).\n' '\n' 'The return value of a function is determined by the last ' '"return"\n' 'statement executed. Since the "finally" clause always executes, ' 'a\n' '"return" statement executed in the "finally" clause will always ' 'be the\n' 'last one executed:\n' '\n' ' >>> def foo():\n' ' ... try:\n' " ... return 'try'\n" ' ... finally:\n' " ... return 'finally'\n" ' ...\n' ' >>> foo()\n' " 'finally'\n" '\n' 'Additional information on exceptions can be found in section\n' 'Exceptions, and information on using the "raise" statement to ' 'generate\n' 'exceptions may be found in section The raise statement.\n' '\n' '\n' 'The "with" statement\n' '====================\n' '\n' 'New in version 2.5.\n' '\n' 'The "with" statement is used to wrap the execution of a block ' 'with\n' 'methods defined by a context manager (see section With ' 'Statement\n' 'Context Managers). This allows common ' '"try"..."except"..."finally"\n' 'usage patterns to be encapsulated for convenient reuse.\n' '\n' ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' ' with_item ::= expression ["as" target]\n' '\n' 'The execution of the "with" statement with one "item" proceeds ' 'as\n' 'follows:\n' '\n' '1. The context expression (the expression given in the ' '"with_item")\n' ' is evaluated to obtain a context manager.\n' '\n' '2. The context manager\'s "__exit__()" is loaded for later use.\n' '\n' '3. The context manager\'s "__enter__()" method is invoked.\n' '\n' '4. If a target was included in the "with" statement, the return\n' ' value from "__enter__()" is assigned to it.\n' '\n' ' Note: The "with" statement guarantees that if the ' '"__enter__()"\n' ' method returns without an error, then "__exit__()" will ' 'always be\n' ' called. Thus, if an error occurs during the assignment to ' 'the\n' ' target list, it will be treated the same as an error ' 'occurring\n' ' within the suite would be. See step 6 below.\n' '\n' '5. The suite is executed.\n' '\n' '6. The context manager\'s "__exit__()" method is invoked. If an\n' ' exception caused the suite to be exited, its type, value, ' 'and\n' ' traceback are passed as arguments to "__exit__()". Otherwise, ' 'three\n' ' "None" arguments are supplied.\n' '\n' ' If the suite was exited due to an exception, and the return ' 'value\n' ' from the "__exit__()" method was false, the exception is ' 'reraised.\n' ' If the return value was true, the exception is suppressed, ' 'and\n' ' execution continues with the statement following the "with"\n' ' statement.\n' '\n' ' If the suite was exited for any reason other than an ' 'exception, the\n' ' return value from "__exit__()" is ignored, and execution ' 'proceeds\n' ' at the normal location for the kind of exit that was taken.\n' '\n' 'With more than one item, the context managers are processed as ' 'if\n' 'multiple "with" statements were nested:\n' '\n' ' with A() as a, B() as b:\n' ' suite\n' '\n' 'is equivalent to\n' '\n' ' with A() as a:\n' ' with B() as b:\n' ' suite\n' '\n' 'Note: In Python 2.5, the "with" statement is only allowed when ' 'the\n' ' "with_statement" feature has been enabled. It is always ' 'enabled in\n' ' Python 2.6.\n' '\n' 'Changed in version 2.7: Support for multiple context ' 'expressions.\n' '\n' 'See also:\n' '\n' ' **PEP 343** - The "with" statement\n' ' The specification, background, and examples for the Python ' '"with"\n' ' statement.\n' '\n' '\n' 'Function definitions\n' '====================\n' '\n' 'A function definition defines a user-defined function object ' '(see\n' 'section The standard type hierarchy):\n' '\n' ' decorated ::= decorators (classdef | funcdef)\n' ' decorators ::= decorator+\n' ' decorator ::= "@" dotted_name ["(" [argument_list [","]] ' '")"] NEWLINE\n' ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' '":" suite\n' ' dotted_name ::= identifier ("." identifier)*\n' ' parameter_list ::= (defparameter ",")*\n' ' ( "*" identifier ["," "**" identifier]\n' ' | "**" identifier\n' ' | defparameter [","] )\n' ' defparameter ::= parameter ["=" expression]\n' ' sublist ::= parameter ("," parameter)* [","]\n' ' parameter ::= identifier | "(" sublist ")"\n' ' funcname ::= identifier\n' '\n' 'A function definition is an executable statement. Its execution ' 'binds\n' 'the function name in the current local namespace to a function ' 'object\n' '(a wrapper around the executable code for the function). This\n' 'function object contains a reference to the current global ' 'namespace\n' 'as the global namespace to be used when the function is called.\n' '\n' 'The function definition does not execute the function body; this ' 'gets\n' 'executed only when the function is called. [3]\n' '\n' 'A function definition may be wrapped by one or more *decorator*\n' 'expressions. Decorator expressions are evaluated when the ' 'function is\n' 'defined, in the scope that contains the function definition. ' 'The\n' 'result must be a callable, which is invoked with the function ' 'object\n' 'as the only argument. The returned value is bound to the ' 'function name\n' 'instead of the function object. Multiple decorators are applied ' 'in\n' 'nested fashion. For example, the following code:\n' '\n' ' @f1(arg)\n' ' @f2\n' ' def func(): pass\n' '\n' 'is equivalent to:\n' '\n' ' def func(): pass\n' ' func = f1(arg)(f2(func))\n' '\n' 'When one or more top-level *parameters* have the form ' '*parameter* "="\n' '*expression*, the function is said to have "default parameter ' 'values."\n' 'For a parameter with a default value, the corresponding ' '*argument* may\n' "be omitted from a call, in which case the parameter's default " 'value is\n' 'substituted. If a parameter has a default value, all following\n' 'parameters must also have a default value --- this is a ' 'syntactic\n' 'restriction that is not expressed by the grammar.\n' '\n' '**Default parameter values are evaluated when the function ' 'definition\n' 'is executed.** This means that the expression is evaluated ' 'once, when\n' 'the function is defined, and that the same "pre-computed" value ' 'is\n' 'used for each call. This is especially important to understand ' 'when a\n' 'default parameter is a mutable object, such as a list or a ' 'dictionary:\n' 'if the function modifies the object (e.g. by appending an item ' 'to a\n' 'list), the default value is in effect modified. This is ' 'generally not\n' 'what was intended. A way around this is to use "None" as the\n' 'default, and explicitly test for it in the body of the function, ' 'e.g.:\n' '\n' ' def whats_on_the_telly(penguin=None):\n' ' if penguin is None:\n' ' penguin = []\n' ' penguin.append("property of the zoo")\n' ' return penguin\n' '\n' 'Function call semantics are described in more detail in section ' 'Calls.\n' 'A function call always assigns values to all parameters ' 'mentioned in\n' 'the parameter list, either from position arguments, from ' 'keyword\n' 'arguments, or from default values. If the form ""*identifier"" ' 'is\n' 'present, it is initialized to a tuple receiving any excess ' 'positional\n' 'parameters, defaulting to the empty tuple. If the form\n' '""**identifier"" is present, it is initialized to a new ' 'dictionary\n' 'receiving any excess keyword arguments, defaulting to a new ' 'empty\n' 'dictionary.\n' '\n' 'It is also possible to create anonymous functions (functions not ' 'bound\n' 'to a name), for immediate use in expressions. This uses lambda\n' 'expressions, described in section Lambdas. Note that the ' 'lambda\n' 'expression is merely a shorthand for a simplified function ' 'definition;\n' 'a function defined in a ""def"" statement can be passed around ' 'or\n' 'assigned to another name just like a function defined by a ' 'lambda\n' 'expression. The ""def"" form is actually more powerful since ' 'it\n' 'allows the execution of multiple statements.\n' '\n' "**Programmer's note:** Functions are first-class objects. A " '""def""\n' 'form executed inside a function definition defines a local ' 'function\n' 'that can be returned or passed around. Free variables used in ' 'the\n' 'nested function can access the local variables of the function\n' 'containing the def. See section Naming and binding for ' 'details.\n' '\n' '\n' 'Class definitions\n' '=================\n' '\n' 'A class definition defines a class object (see section The ' 'standard\n' 'type hierarchy):\n' '\n' ' classdef ::= "class" classname [inheritance] ":" suite\n' ' inheritance ::= "(" [expression_list] ")"\n' ' classname ::= identifier\n' '\n' 'A class definition is an executable statement. It first ' 'evaluates the\n' 'inheritance list, if present. Each item in the inheritance ' 'list\n' 'should evaluate to a class object or class type which allows\n' "subclassing. The class's suite is then executed in a new " 'execution\n' 'frame (see section Naming and binding), using a newly created ' 'local\n' 'namespace and the original global namespace. (Usually, the ' 'suite\n' "contains only function definitions.) When the class's suite " 'finishes\n' 'execution, its execution frame is discarded but its local ' 'namespace is\n' 'saved. [4] A class object is then created using the inheritance ' 'list\n' 'for the base classes and the saved local namespace for the ' 'attribute\n' 'dictionary. The class name is bound to this class object in ' 'the\n' 'original local namespace.\n' '\n' "**Programmer's note:** Variables defined in the class definition " 'are\n' 'class variables; they are shared by all instances. To create ' 'instance\n' 'variables, they can be set in a method with "self.name = ' 'value". Both\n' 'class and instance variables are accessible through the ' 'notation\n' '""self.name"", and an instance variable hides a class variable ' 'with\n' 'the same name when accessed in this way. Class variables can be ' 'used\n' 'as defaults for instance variables, but using mutable values ' 'there can\n' 'lead to unexpected results. For *new-style class*es, ' 'descriptors can\n' 'be used to create instance variables with different ' 'implementation\n' 'details.\n' '\n' 'Class definitions, like function definitions, may be wrapped by ' 'one or\n' 'more *decorator* expressions. The evaluation rules for the ' 'decorator\n' 'expressions are the same as for functions. The result must be a ' 'class\n' 'object, which is then bound to the class name.\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] The exception is propagated to the invocation stack unless\n' ' there is a "finally" clause which happens to raise another\n' ' exception. That new exception causes the old one to be ' 'lost.\n' '\n' '[2] Currently, control "flows off the end" except in the case ' 'of\n' ' an exception or the execution of a "return", "continue", or\n' ' "break" statement.\n' '\n' '[3] A string literal appearing as the first statement in the\n' ' function body is transformed into the function\'s "__doc__"\n' " attribute and therefore the function's *docstring*.\n" '\n' '[4] A string literal appearing as the first statement in the ' 'class\n' ' body is transformed into the namespace\'s "__doc__" item ' 'and\n' " therefore the class's *docstring*.\n", 'context-managers': '\n' 'With Statement Context Managers\n' '*******************************\n' '\n' 'New in version 2.5.\n' '\n' 'A *context manager* is an object that defines the ' 'runtime context to\n' 'be established when executing a "with" statement. The ' 'context manager\n' 'handles the entry into, and the exit from, the desired ' 'runtime context\n' 'for the execution of the block of code. Context ' 'managers are normally\n' 'invoked using the "with" statement (described in section ' 'The with\n' 'statement), but can also be used by directly invoking ' 'their methods.\n' '\n' 'Typical uses of context managers include saving and ' 'restoring various\n' 'kinds of global state, locking and unlocking resources, ' 'closing opened\n' 'files, etc.\n' '\n' 'For more information on context managers, see Context ' 'Manager Types.\n' '\n' 'object.__enter__(self)\n' '\n' ' Enter the runtime context related to this object. The ' '"with"\n' " statement will bind this method's return value to the " 'target(s)\n' ' specified in the "as" clause of the statement, if ' 'any.\n' '\n' 'object.__exit__(self, exc_type, exc_value, traceback)\n' '\n' ' Exit the runtime context related to this object. The ' 'parameters\n' ' describe the exception that caused the context to be ' 'exited. If the\n' ' context was exited without an exception, all three ' 'arguments will\n' ' be "None".\n' '\n' ' If an exception is supplied, and the method wishes to ' 'suppress the\n' ' exception (i.e., prevent it from being propagated), ' 'it should\n' ' return a true value. Otherwise, the exception will be ' 'processed\n' ' normally upon exit from this method.\n' '\n' ' Note that "__exit__()" methods should not reraise the ' 'passed-in\n' " exception; this is the caller's responsibility.\n" '\n' 'See also:\n' '\n' ' **PEP 343** - The "with" statement\n' ' The specification, background, and examples for the ' 'Python "with"\n' ' statement.\n', 'continue': '\n' 'The "continue" statement\n' '************************\n' '\n' ' continue_stmt ::= "continue"\n' '\n' '"continue" may only occur syntactically nested in a "for" or ' '"while"\n' 'loop, but not nested in a function or class definition or ' '"finally"\n' 'clause within that loop. It continues with the next cycle of ' 'the\n' 'nearest enclosing loop.\n' '\n' 'When "continue" passes control out of a "try" statement with a\n' '"finally" clause, that "finally" clause is executed before ' 'really\n' 'starting the next loop cycle.\n', 'conversions': '\n' 'Arithmetic conversions\n' '**********************\n' '\n' 'When a description of an arithmetic operator below uses the ' 'phrase\n' '"the numeric arguments are converted to a common type," the ' 'arguments\n' 'are coerced using the coercion rules listed at Coercion ' 'rules. If\n' 'both arguments are standard numeric types, the following ' 'coercions are\n' 'applied:\n' '\n' '* If either argument is a complex number, the other is ' 'converted to\n' ' complex;\n' '\n' '* otherwise, if either argument is a floating point number, ' 'the\n' ' other is converted to floating point;\n' '\n' '* otherwise, if either argument is a long integer, the other ' 'is\n' ' converted to long integer;\n' '\n' '* otherwise, both must be plain integers and no conversion ' 'is\n' ' necessary.\n' '\n' 'Some additional rules apply for certain operators (e.g., a ' 'string left\n' "argument to the '%' operator). Extensions can define their " 'own\n' 'coercions.\n', 'customization': '\n' 'Basic customization\n' '*******************\n' '\n' 'object.__new__(cls[, ...])\n' '\n' ' Called to create a new instance of class *cls*. ' '"__new__()" is a\n' ' static method (special-cased so you need not declare it ' 'as such)\n' ' that takes the class of which an instance was requested ' 'as its\n' ' first argument. The remaining arguments are those ' 'passed to the\n' ' object constructor expression (the call to the class). ' 'The return\n' ' value of "__new__()" should be the new object instance ' '(usually an\n' ' instance of *cls*).\n' '\n' ' Typical implementations create a new instance of the ' 'class by\n' ' invoking the superclass\'s "__new__()" method using\n' ' "super(currentclass, cls).__new__(cls[, ...])" with ' 'appropriate\n' ' arguments and then modifying the newly-created instance ' 'as\n' ' necessary before returning it.\n' '\n' ' If "__new__()" returns an instance of *cls*, then the ' 'new\n' ' instance\'s "__init__()" method will be invoked like\n' ' "__init__(self[, ...])", where *self* is the new ' 'instance and the\n' ' remaining arguments are the same as were passed to ' '"__new__()".\n' '\n' ' If "__new__()" does not return an instance of *cls*, ' 'then the new\n' ' instance\'s "__init__()" method will not be invoked.\n' '\n' ' "__new__()" is intended mainly to allow subclasses of ' 'immutable\n' ' types (like int, str, or tuple) to customize instance ' 'creation. It\n' ' is also commonly overridden in custom metaclasses in ' 'order to\n' ' customize class creation.\n' '\n' 'object.__init__(self[, ...])\n' '\n' ' Called after the instance has been created (by ' '"__new__()"), but\n' ' before it is returned to the caller. The arguments are ' 'those\n' ' passed to the class constructor expression. If a base ' 'class has an\n' ' "__init__()" method, the derived class\'s "__init__()" ' 'method, if\n' ' any, must explicitly call it to ensure proper ' 'initialization of the\n' ' base class part of the instance; for example:\n' ' "BaseClass.__init__(self, [args...])".\n' '\n' ' Because "__new__()" and "__init__()" work together in ' 'constructing\n' ' objects ("__new__()" to create it, and "__init__()" to ' 'customise\n' ' it), no non-"None" value may be returned by ' '"__init__()"; doing so\n' ' will cause a "TypeError" to be raised at runtime.\n' '\n' 'object.__del__(self)\n' '\n' ' Called when the instance is about to be destroyed. This ' 'is also\n' ' called a destructor. If a base class has a "__del__()" ' 'method, the\n' ' derived class\'s "__del__()" method, if any, must ' 'explicitly call it\n' ' to ensure proper deletion of the base class part of the ' 'instance.\n' ' Note that it is possible (though not recommended!) for ' 'the\n' ' "__del__()" method to postpone destruction of the ' 'instance by\n' ' creating a new reference to it. It may then be called ' 'at a later\n' ' time when this new reference is deleted. It is not ' 'guaranteed that\n' ' "__del__()" methods are called for objects that still ' 'exist when\n' ' the interpreter exits.\n' '\n' ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' 'the former\n' ' decrements the reference count for "x" by one, and the ' 'latter is\n' ' only called when "x"\'s reference count reaches zero. ' 'Some common\n' ' situations that may prevent the reference count of an ' 'object from\n' ' going to zero include: circular references between ' 'objects (e.g.,\n' ' a doubly-linked list or a tree data structure with ' 'parent and\n' ' child pointers); a reference to the object on the ' 'stack frame of\n' ' a function that caught an exception (the traceback ' 'stored in\n' ' "sys.exc_traceback" keeps the stack frame alive); or a ' 'reference\n' ' to the object on the stack frame that raised an ' 'unhandled\n' ' exception in interactive mode (the traceback stored ' 'in\n' ' "sys.last_traceback" keeps the stack frame alive). ' 'The first\n' ' situation can only be remedied by explicitly breaking ' 'the cycles;\n' ' the latter two situations can be resolved by storing ' '"None" in\n' ' "sys.exc_traceback" or "sys.last_traceback". Circular ' 'references\n' ' which are garbage are detected when the option cycle ' 'detector is\n' " enabled (it's on by default), but can only be cleaned " 'up if there\n' ' are no Python-level "__del__()" methods involved. ' 'Refer to the\n' ' documentation for the "gc" module for more information ' 'about how\n' ' "__del__()" methods are handled by the cycle ' 'detector,\n' ' particularly the description of the "garbage" value.\n' '\n' ' Warning: Due to the precarious circumstances under ' 'which\n' ' "__del__()" methods are invoked, exceptions that occur ' 'during\n' ' their execution are ignored, and a warning is printed ' 'to\n' ' "sys.stderr" instead. Also, when "__del__()" is ' 'invoked in\n' ' response to a module being deleted (e.g., when ' 'execution of the\n' ' program is done), other globals referenced by the ' '"__del__()"\n' ' method may already have been deleted or in the process ' 'of being\n' ' torn down (e.g. the import machinery shutting down). ' 'For this\n' ' reason, "__del__()" methods should do the absolute ' 'minimum needed\n' ' to maintain external invariants. Starting with ' 'version 1.5,\n' ' Python guarantees that globals whose name begins with ' 'a single\n' ' underscore are deleted from their module before other ' 'globals are\n' ' deleted; if no other references to such globals exist, ' 'this may\n' ' help in assuring that imported modules are still ' 'available at the\n' ' time when the "__del__()" method is called.\n' '\n' ' See also the "-R" command-line option.\n' '\n' 'object.__repr__(self)\n' '\n' ' Called by the "repr()" built-in function and by string ' 'conversions\n' ' (reverse quotes) to compute the "official" string ' 'representation of\n' ' an object. If at all possible, this should look like a ' 'valid\n' ' Python expression that could be used to recreate an ' 'object with the\n' ' same value (given an appropriate environment). If this ' 'is not\n' ' possible, a string of the form "<...some useful ' 'description...>"\n' ' should be returned. The return value must be a string ' 'object. If a\n' ' class defines "__repr__()" but not "__str__()", then ' '"__repr__()"\n' ' is also used when an "informal" string representation of ' 'instances\n' ' of that class is required.\n' '\n' ' This is typically used for debugging, so it is important ' 'that the\n' ' representation is information-rich and unambiguous.\n' '\n' 'object.__str__(self)\n' '\n' ' Called by the "str()" built-in function and by the ' '"print"\n' ' statement to compute the "informal" string ' 'representation of an\n' ' object. This differs from "__repr__()" in that it does ' 'not have to\n' ' be a valid Python expression: a more convenient or ' 'concise\n' ' representation may be used instead. The return value ' 'must be a\n' ' string object.\n' '\n' 'object.__lt__(self, other)\n' 'object.__le__(self, other)\n' 'object.__eq__(self, other)\n' 'object.__ne__(self, other)\n' 'object.__gt__(self, other)\n' 'object.__ge__(self, other)\n' '\n' ' New in version 2.1.\n' '\n' ' These are the so-called "rich comparison" methods, and ' 'are called\n' ' for comparison operators in preference to "__cmp__()" ' 'below. The\n' ' correspondence between operator symbols and method names ' 'is as\n' ' follows: "xy" call ' '"x.__ne__(y)",\n' ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' '"x.__ge__(y)".\n' '\n' ' A rich comparison method may return the singleton ' '"NotImplemented"\n' ' if it does not implement the operation for a given pair ' 'of\n' ' arguments. By convention, "False" and "True" are ' 'returned for a\n' ' successful comparison. However, these methods can return ' 'any value,\n' ' so if the comparison operator is used in a Boolean ' 'context (e.g.,\n' ' in the condition of an "if" statement), Python will call ' '"bool()"\n' ' on the value to determine if the result is true or ' 'false.\n' '\n' ' There are no implied relationships among the comparison ' 'operators.\n' ' The truth of "x==y" does not imply that "x!=y" is ' 'false.\n' ' Accordingly, when defining "__eq__()", one should also ' 'define\n' ' "__ne__()" so that the operators will behave as ' 'expected. See the\n' ' paragraph on "__hash__()" for some important notes on ' 'creating\n' ' *hashable* objects which support custom comparison ' 'operations and\n' ' are usable as dictionary keys.\n' '\n' ' There are no swapped-argument versions of these methods ' '(to be used\n' ' when the left argument does not support the operation ' 'but the right\n' ' argument does); rather, "__lt__()" and "__gt__()" are ' "each other's\n" ' reflection, "__le__()" and "__ge__()" are each other\'s ' 'reflection,\n' ' and "__eq__()" and "__ne__()" are their own reflection.\n' '\n' ' Arguments to rich comparison methods are never coerced.\n' '\n' ' To automatically generate ordering operations from a ' 'single root\n' ' operation, see "functools.total_ordering()".\n' '\n' 'object.__cmp__(self, other)\n' '\n' ' Called by comparison operations if rich comparison (see ' 'above) is\n' ' not defined. Should return a negative integer if "self ' '< other",\n' ' zero if "self == other", a positive integer if "self > ' 'other". If\n' ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' 'defined,\n' ' class instances are compared by object identity ' '("address"). See\n' ' also the description of "__hash__()" for some important ' 'notes on\n' ' creating *hashable* objects which support custom ' 'comparison\n' ' operations and are usable as dictionary keys. (Note: ' 'the\n' ' restriction that exceptions are not propagated by ' '"__cmp__()" has\n' ' been removed since Python 1.5.)\n' '\n' 'object.__rcmp__(self, other)\n' '\n' ' Changed in version 2.1: No longer supported.\n' '\n' 'object.__hash__(self)\n' '\n' ' Called by built-in function "hash()" and for operations ' 'on members\n' ' of hashed collections including "set", "frozenset", and ' '"dict".\n' ' "__hash__()" should return an integer. The only ' 'required property\n' ' is that objects which compare equal have the same hash ' 'value; it is\n' ' advised to mix together the hash values of the ' 'components of the\n' ' object that also play a part in comparison of objects by ' 'packing\n' ' them into a tuple and hashing the tuple. Example:\n' '\n' ' def __hash__(self):\n' ' return hash((self.name, self.nick, self.color))\n' '\n' ' If a class does not define a "__cmp__()" or "__eq__()" ' 'method it\n' ' should not define a "__hash__()" operation either; if it ' 'defines\n' ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' 'instances will\n' ' not be usable in hashed collections. If a class defines ' 'mutable\n' ' objects and implements a "__cmp__()" or "__eq__()" ' 'method, it\n' ' should not implement "__hash__()", since hashable ' 'collection\n' " implementations require that an object's hash value is " 'immutable\n' " (if the object's hash value changes, it will be in the " 'wrong hash\n' ' bucket).\n' '\n' ' User-defined classes have "__cmp__()" and "__hash__()" ' 'methods by\n' ' default; with them, all objects compare unequal (except ' 'with\n' ' themselves) and "x.__hash__()" returns a result derived ' 'from\n' ' "id(x)".\n' '\n' ' Classes which inherit a "__hash__()" method from a ' 'parent class but\n' ' change the meaning of "__cmp__()" or "__eq__()" such ' 'that the hash\n' ' value returned is no longer appropriate (e.g. by ' 'switching to a\n' ' value-based concept of equality instead of the default ' 'identity\n' ' based equality) can explicitly flag themselves as being ' 'unhashable\n' ' by setting "__hash__ = None" in the class definition. ' 'Doing so\n' ' means that not only will instances of the class raise ' 'an\n' ' appropriate "TypeError" when a program attempts to ' 'retrieve their\n' ' hash value, but they will also be correctly identified ' 'as\n' ' unhashable when checking "isinstance(obj, ' 'collections.Hashable)"\n' ' (unlike classes which define their own "__hash__()" to ' 'explicitly\n' ' raise "TypeError").\n' '\n' ' Changed in version 2.5: "__hash__()" may now also return ' 'a long\n' ' integer object; the 32-bit integer is then derived from ' 'the hash of\n' ' that object.\n' '\n' ' Changed in version 2.6: "__hash__" may now be set to ' '"None" to\n' ' explicitly flag instances of a class as unhashable.\n' '\n' 'object.__nonzero__(self)\n' '\n' ' Called to implement truth value testing and the built-in ' 'operation\n' ' "bool()"; should return "False" or "True", or their ' 'integer\n' ' equivalents "0" or "1". When this method is not ' 'defined,\n' ' "__len__()" is called, if it is defined, and the object ' 'is\n' ' considered true if its result is nonzero. If a class ' 'defines\n' ' neither "__len__()" nor "__nonzero__()", all its ' 'instances are\n' ' considered true.\n' '\n' 'object.__unicode__(self)\n' '\n' ' Called to implement "unicode()" built-in; should return ' 'a Unicode\n' ' object. When this method is not defined, string ' 'conversion is\n' ' attempted, and the result of string conversion is ' 'converted to\n' ' Unicode using the system default encoding.\n', 'debugger': '\n' '"pdb" --- The Python Debugger\n' '*****************************\n' '\n' '**Source code:** Lib/pdb.py\n' '\n' '======================================================================\n' '\n' 'The module "pdb" defines an interactive source code debugger ' 'for\n' 'Python programs. It supports setting (conditional) breakpoints ' 'and\n' 'single stepping at the source line level, inspection of stack ' 'frames,\n' 'source code listing, and evaluation of arbitrary Python code in ' 'the\n' 'context of any stack frame. It also supports post-mortem ' 'debugging\n' 'and can be called under program control.\n' '\n' 'The debugger is extensible --- it is actually defined as the ' 'class\n' '"Pdb". This is currently undocumented but easily understood by ' 'reading\n' 'the source. The extension interface uses the modules "bdb" and ' '"cmd".\n' '\n' 'The debugger\'s prompt is "(Pdb)". Typical usage to run a ' 'program under\n' 'control of the debugger is:\n' '\n' ' >>> import pdb\n' ' >>> import mymodule\n' " >>> pdb.run('mymodule.test()')\n" ' > (0)?()\n' ' (Pdb) continue\n' ' > (1)?()\n' ' (Pdb) continue\n' " NameError: 'spam'\n" ' > (1)?()\n' ' (Pdb)\n' '\n' '"pdb.py" can also be invoked as a script to debug other ' 'scripts. For\n' 'example:\n' '\n' ' python -m pdb myscript.py\n' '\n' 'When invoked as a script, pdb will automatically enter ' 'post-mortem\n' 'debugging if the program being debugged exits abnormally. After ' 'post-\n' 'mortem debugging (or after normal exit of the program), pdb ' 'will\n' "restart the program. Automatic restarting preserves pdb's state " '(such\n' 'as breakpoints) and in most cases is more useful than quitting ' 'the\n' "debugger upon program's exit.\n" '\n' 'New in version 2.4: Restarting post-mortem behavior added.\n' '\n' 'The typical usage to break into the debugger from a running ' 'program is\n' 'to insert\n' '\n' ' import pdb; pdb.set_trace()\n' '\n' 'at the location you want to break into the debugger. You can ' 'then\n' 'step through the code following this statement, and continue ' 'running\n' 'without the debugger using the "c" command.\n' '\n' 'The typical usage to inspect a crashed program is:\n' '\n' ' >>> import pdb\n' ' >>> import mymodule\n' ' >>> mymodule.test()\n' ' Traceback (most recent call last):\n' ' File "", line 1, in \n' ' File "./mymodule.py", line 4, in test\n' ' test2()\n' ' File "./mymodule.py", line 3, in test2\n' ' print spam\n' ' NameError: spam\n' ' >>> pdb.pm()\n' ' > ./mymodule.py(3)test2()\n' ' -> print spam\n' ' (Pdb)\n' '\n' 'The module defines the following functions; each enters the ' 'debugger\n' 'in a slightly different way:\n' '\n' 'pdb.run(statement[, globals[, locals]])\n' '\n' ' Execute the *statement* (given as a string) under debugger ' 'control.\n' ' The debugger prompt appears before any code is executed; you ' 'can\n' ' set breakpoints and type "continue", or you can step through ' 'the\n' ' statement using "step" or "next" (all these commands are ' 'explained\n' ' below). The optional *globals* and *locals* arguments ' 'specify the\n' ' environment in which the code is executed; by default the\n' ' dictionary of the module "__main__" is used. (See the ' 'explanation\n' ' of the "exec" statement or the "eval()" built-in function.)\n' '\n' 'pdb.runeval(expression[, globals[, locals]])\n' '\n' ' Evaluate the *expression* (given as a string) under debugger\n' ' control. When "runeval()" returns, it returns the value of ' 'the\n' ' expression. Otherwise this function is similar to "run()".\n' '\n' 'pdb.runcall(function[, argument, ...])\n' '\n' ' Call the *function* (a function or method object, not a ' 'string)\n' ' with the given arguments. When "runcall()" returns, it ' 'returns\n' ' whatever the function call returned. The debugger prompt ' 'appears\n' ' as soon as the function is entered.\n' '\n' 'pdb.set_trace()\n' '\n' ' Enter the debugger at the calling stack frame. This is ' 'useful to\n' ' hard-code a breakpoint at a given point in a program, even if ' 'the\n' ' code is not otherwise being debugged (e.g. when an assertion\n' ' fails).\n' '\n' 'pdb.post_mortem([traceback])\n' '\n' ' Enter post-mortem debugging of the given *traceback* object. ' 'If no\n' ' *traceback* is given, it uses the one of the exception that ' 'is\n' ' currently being handled (an exception must be being handled ' 'if the\n' ' default is to be used).\n' '\n' 'pdb.pm()\n' '\n' ' Enter post-mortem debugging of the traceback found in\n' ' "sys.last_traceback".\n' '\n' 'The "run*" functions and "set_trace()" are aliases for ' 'instantiating\n' 'the "Pdb" class and calling the method of the same name. If you ' 'want\n' 'to access further features, you have to do this yourself:\n' '\n' "class pdb.Pdb(completekey='tab', stdin=None, stdout=None, " 'skip=None)\n' '\n' ' "Pdb" is the debugger class.\n' '\n' ' The *completekey*, *stdin* and *stdout* arguments are passed ' 'to the\n' ' underlying "cmd.Cmd" class; see the description there.\n' '\n' ' The *skip* argument, if given, must be an iterable of ' 'glob-style\n' ' module name patterns. The debugger will not step into frames ' 'that\n' ' originate in a module that matches one of these patterns. ' '[1]\n' '\n' ' Example call to enable tracing with *skip*:\n' '\n' " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" '\n' ' New in version 2.7: The *skip* argument.\n' '\n' ' run(statement[, globals[, locals]])\n' ' runeval(expression[, globals[, locals]])\n' ' runcall(function[, argument, ...])\n' ' set_trace()\n' '\n' ' See the documentation for the functions explained above.\n', 'del': '\n' 'The "del" statement\n' '*******************\n' '\n' ' del_stmt ::= "del" target_list\n' '\n' 'Deletion is recursively defined very similar to the way assignment ' 'is\n' 'defined. Rather than spelling it out in full details, here are some\n' 'hints.\n' '\n' 'Deletion of a target list recursively deletes each target, from left\n' 'to right.\n' '\n' 'Deletion of a name removes the binding of that name from the local ' 'or\n' 'global namespace, depending on whether the name occurs in a "global"\n' 'statement in the same code block. If the name is unbound, a\n' '"NameError" exception will be raised.\n' '\n' 'It is illegal to delete a name from the local namespace if it occurs\n' 'as a free variable in a nested block.\n' '\n' 'Deletion of attribute references, subscriptions and slicings is ' 'passed\n' 'to the primary object involved; deletion of a slicing is in general\n' 'equivalent to assignment of an empty slice of the right type (but ' 'even\n' 'this is determined by the sliced object).\n', 'dict': '\n' 'Dictionary displays\n' '*******************\n' '\n' 'A dictionary display is a possibly empty series of key/datum pairs\n' 'enclosed in curly braces:\n' '\n' ' dict_display ::= "{" [key_datum_list | dict_comprehension] ' '"}"\n' ' key_datum_list ::= key_datum ("," key_datum)* [","]\n' ' key_datum ::= expression ":" expression\n' ' dict_comprehension ::= expression ":" expression comp_for\n' '\n' 'A dictionary display yields a new dictionary object.\n' '\n' 'If a comma-separated sequence of key/datum pairs is given, they are\n' 'evaluated from left to right to define the entries of the ' 'dictionary:\n' 'each key object is used as a key into the dictionary to store the\n' 'corresponding datum. This means that you can specify the same key\n' "multiple times in the key/datum list, and the final dictionary's " 'value\n' 'for that key will be the last one given.\n' '\n' 'A dict comprehension, in contrast to list and set comprehensions,\n' 'needs two expressions separated with a colon followed by the usual\n' '"for" and "if" clauses. When the comprehension is run, the ' 'resulting\n' 'key and value elements are inserted in the new dictionary in the ' 'order\n' 'they are produced.\n' '\n' 'Restrictions on the types of the key values are listed earlier in\n' 'section The standard type hierarchy. (To summarize, the key type\n' 'should be *hashable*, which excludes all mutable objects.) Clashes\n' 'between duplicate keys are not detected; the last datum (textually\n' 'rightmost in the display) stored for a given key value prevails.\n', 'dynamic-features': '\n' 'Interaction with dynamic features\n' '*********************************\n' '\n' 'There are several cases where Python statements are ' 'illegal when used\n' 'in conjunction with nested scopes that contain free ' 'variables.\n' '\n' 'If a variable is referenced in an enclosing scope, it is ' 'illegal to\n' 'delete the name. An error will be reported at compile ' 'time.\n' '\n' 'If the wild card form of import --- "import *" --- is ' 'used in a\n' 'function and the function contains or is a nested block ' 'with free\n' 'variables, the compiler will raise a "SyntaxError".\n' '\n' 'If "exec" is used in a function and the function ' 'contains or is a\n' 'nested block with free variables, the compiler will ' 'raise a\n' '"SyntaxError" unless the exec explicitly specifies the ' 'local namespace\n' 'for the "exec". (In other words, "exec obj" would be ' 'illegal, but\n' '"exec obj in ns" would be legal.)\n' '\n' 'The "eval()", "execfile()", and "input()" functions and ' 'the "exec"\n' 'statement do not have access to the full environment for ' 'resolving\n' 'names. Names may be resolved in the local and global ' 'namespaces of\n' 'the caller. Free variables are not resolved in the ' 'nearest enclosing\n' 'namespace, but in the global namespace. [1] The "exec" ' 'statement and\n' 'the "eval()" and "execfile()" functions have optional ' 'arguments to\n' 'override the global and local namespace. If only one ' 'namespace is\n' 'specified, it is used for both.\n', 'else': '\n' 'The "if" statement\n' '******************\n' '\n' 'The "if" statement is used for conditional execution:\n' '\n' ' if_stmt ::= "if" expression ":" suite\n' ' ( "elif" expression ":" suite )*\n' ' ["else" ":" suite]\n' '\n' 'It selects exactly one of the suites by evaluating the expressions ' 'one\n' 'by one until one is found to be true (see section Boolean ' 'operations\n' 'for the definition of true and false); then that suite is executed\n' '(and no other part of the "if" statement is executed or evaluated).\n' 'If all expressions are false, the suite of the "else" clause, if\n' 'present, is executed.\n', 'exceptions': '\n' 'Exceptions\n' '**********\n' '\n' 'Exceptions are a means of breaking out of the normal flow of ' 'control\n' 'of a code block in order to handle errors or other ' 'exceptional\n' 'conditions. An exception is *raised* at the point where the ' 'error is\n' 'detected; it may be *handled* by the surrounding code block or ' 'by any\n' 'code block that directly or indirectly invoked the code block ' 'where\n' 'the error occurred.\n' '\n' 'The Python interpreter raises an exception when it detects a ' 'run-time\n' 'error (such as division by zero). A Python program can also\n' 'explicitly raise an exception with the "raise" statement. ' 'Exception\n' 'handlers are specified with the "try" ... "except" statement. ' 'The\n' '"finally" clause of such a statement can be used to specify ' 'cleanup\n' 'code which does not handle the exception, but is executed ' 'whether an\n' 'exception occurred or not in the preceding code.\n' '\n' 'Python uses the "termination" model of error handling: an ' 'exception\n' 'handler can find out what happened and continue execution at ' 'an outer\n' 'level, but it cannot repair the cause of the error and retry ' 'the\n' 'failing operation (except by re-entering the offending piece ' 'of code\n' 'from the top).\n' '\n' 'When an exception is not handled at all, the interpreter ' 'terminates\n' 'execution of the program, or returns to its interactive main ' 'loop. In\n' 'either case, it prints a stack backtrace, except when the ' 'exception is\n' '"SystemExit".\n' '\n' 'Exceptions are identified by class instances. The "except" ' 'clause is\n' 'selected depending on the class of the instance: it must ' 'reference the\n' 'class of the instance or a base class thereof. The instance ' 'can be\n' 'received by the handler and can carry additional information ' 'about the\n' 'exceptional condition.\n' '\n' 'Exceptions can also be identified by strings, in which case ' 'the\n' '"except" clause is selected by object identity. An arbitrary ' 'value\n' 'can be raised along with the identifying string which can be ' 'passed to\n' 'the handler.\n' '\n' 'Note: Messages to exceptions are not part of the Python API. ' 'Their\n' ' contents may change from one version of Python to the next ' 'without\n' ' warning and should not be relied on by code which will run ' 'under\n' ' multiple versions of the interpreter.\n' '\n' 'See also the description of the "try" statement in section The ' 'try\n' 'statement and "raise" statement in section The raise ' 'statement.\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] This limitation occurs because the code that is executed ' 'by\n' ' these operations is not available at the time the module ' 'is\n' ' compiled.\n', 'exec': '\n' 'The "exec" statement\n' '********************\n' '\n' ' exec_stmt ::= "exec" or_expr ["in" expression ["," expression]]\n' '\n' 'This statement supports dynamic execution of Python code. The ' 'first\n' 'expression should evaluate to either a Unicode string, a *Latin-1*\n' 'encoded string, an open file object, a code object, or a tuple. If ' 'it\n' 'is a string, the string is parsed as a suite of Python statements\n' 'which is then executed (unless a syntax error occurs). [1] If it is ' 'an\n' 'open file, the file is parsed until EOF and executed. If it is a ' 'code\n' 'object, it is simply executed. For the interpretation of a tuple, ' 'see\n' "below. In all cases, the code that's executed is expected to be " 'valid\n' 'as file input (see section File input). Be aware that the "return"\n' 'and "yield" statements may not be used outside of function ' 'definitions\n' 'even within the context of code passed to the "exec" statement.\n' '\n' 'In all cases, if the optional parts are omitted, the code is ' 'executed\n' 'in the current scope. If only the first expression after "in" is\n' 'specified, it should be a dictionary, which will be used for both ' 'the\n' 'global and the local variables. If two expressions are given, they\n' 'are used for the global and local variables, respectively. If\n' 'provided, *locals* can be any mapping object. Remember that at ' 'module\n' 'level, globals and locals are the same dictionary. If two separate\n' 'objects are given as *globals* and *locals*, the code will be ' 'executed\n' 'as if it were embedded in a class definition.\n' '\n' 'The first expression may also be a tuple of length 2 or 3. In this\n' 'case, the optional parts must be omitted. The form "exec(expr,\n' 'globals)" is equivalent to "exec expr in globals", while the form\n' '"exec(expr, globals, locals)" is equivalent to "exec expr in ' 'globals,\n' 'locals". The tuple form of "exec" provides compatibility with ' 'Python\n' '3, where "exec" is a function rather than a statement.\n' '\n' 'Changed in version 2.4: Formerly, *locals* was required to be a\n' 'dictionary.\n' '\n' 'As a side effect, an implementation may insert additional keys into\n' 'the dictionaries given besides those corresponding to variable ' 'names\n' 'set by the executed code. For example, the current implementation ' 'may\n' 'add a reference to the dictionary of the built-in module ' '"__builtin__"\n' 'under the key "__builtins__" (!).\n' '\n' "**Programmer's hints:** dynamic evaluation of expressions is " 'supported\n' 'by the built-in function "eval()". The built-in functions ' '"globals()"\n' 'and "locals()" return the current global and local dictionary,\n' 'respectively, which may be useful to pass around for use by "exec".\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] Note that the parser only accepts the Unix-style end of line\n' ' convention. If you are reading the code from a file, make sure ' 'to\n' ' use *universal newlines* mode to convert Windows or Mac-style\n' ' newlines.\n', 'execmodel': '\n' 'Execution model\n' '***************\n' '\n' '\n' 'Naming and binding\n' '==================\n' '\n' '*Names* refer to objects. Names are introduced by name ' 'binding\n' 'operations. Each occurrence of a name in the program text ' 'refers to\n' 'the *binding* of that name established in the innermost ' 'function block\n' 'containing the use.\n' '\n' 'A *block* is a piece of Python program text that is executed as ' 'a\n' 'unit. The following are blocks: a module, a function body, and ' 'a class\n' 'definition. Each command typed interactively is a block. A ' 'script\n' 'file (a file given as standard input to the interpreter or ' 'specified\n' 'on the interpreter command line the first argument) is a code ' 'block.\n' 'A script command (a command specified on the interpreter ' 'command line\n' "with the '**-c**' option) is a code block. The file read by " 'the\n' 'built-in function "execfile()" is a code block. The string ' 'argument\n' 'passed to the built-in function "eval()" and to the "exec" ' 'statement\n' 'is a code block. The expression read and evaluated by the ' 'built-in\n' 'function "input()" is a code block.\n' '\n' 'A code block is executed in an *execution frame*. A frame ' 'contains\n' 'some administrative information (used for debugging) and ' 'determines\n' "where and how execution continues after the code block's " 'execution has\n' 'completed.\n' '\n' 'A *scope* defines the visibility of a name within a block. If ' 'a local\n' 'variable is defined in a block, its scope includes that block. ' 'If the\n' 'definition occurs in a function block, the scope extends to any ' 'blocks\n' 'contained within the defining one, unless a contained block ' 'introduces\n' 'a different binding for the name. The scope of names defined ' 'in a\n' 'class block is limited to the class block; it does not extend ' 'to the\n' 'code blocks of methods -- this includes generator expressions ' 'since\n' 'they are implemented using a function scope. This means that ' 'the\n' 'following will fail:\n' '\n' ' class A:\n' ' a = 42\n' ' b = list(a + i for i in range(10))\n' '\n' 'When a name is used in a code block, it is resolved using the ' 'nearest\n' 'enclosing scope. The set of all such scopes visible to a code ' 'block\n' "is called the block's *environment*.\n" '\n' 'If a name is bound in a block, it is a local variable of that ' 'block.\n' 'If a name is bound at the module level, it is a global ' 'variable. (The\n' 'variables of the module code block are local and global.) If ' 'a\n' 'variable is used in a code block but not defined there, it is a ' '*free\n' 'variable*.\n' '\n' 'When a name is not found at all, a "NameError" exception is ' 'raised.\n' 'If the name refers to a local variable that has not been bound, ' 'a\n' '"UnboundLocalError" exception is raised. "UnboundLocalError" ' 'is a\n' 'subclass of "NameError".\n' '\n' 'The following constructs bind names: formal parameters to ' 'functions,\n' '"import" statements, class and function definitions (these bind ' 'the\n' 'class or function name in the defining block), and targets that ' 'are\n' 'identifiers if occurring in an assignment, "for" loop header, ' 'in the\n' 'second position of an "except" clause header or after "as" in a ' '"with"\n' 'statement. The "import" statement of the form "from ... import ' '*"\n' 'binds all names defined in the imported module, except those ' 'beginning\n' 'with an underscore. This form may only be used at the module ' 'level.\n' '\n' 'A target occurring in a "del" statement is also considered ' 'bound for\n' 'this purpose (though the actual semantics are to unbind the ' 'name). It\n' 'is illegal to unbind a name that is referenced by an enclosing ' 'scope;\n' 'the compiler will report a "SyntaxError".\n' '\n' 'Each assignment or import statement occurs within a block ' 'defined by a\n' 'class or function definition or at the module level (the ' 'top-level\n' 'code block).\n' '\n' 'If a name binding operation occurs anywhere within a code ' 'block, all\n' 'uses of the name within the block are treated as references to ' 'the\n' 'current block. This can lead to errors when a name is used ' 'within a\n' 'block before it is bound. This rule is subtle. Python lacks\n' 'declarations and allows name binding operations to occur ' 'anywhere\n' 'within a code block. The local variables of a code block can ' 'be\n' 'determined by scanning the entire text of the block for name ' 'binding\n' 'operations.\n' '\n' 'If the global statement occurs within a block, all uses of the ' 'name\n' 'specified in the statement refer to the binding of that name in ' 'the\n' 'top-level namespace. Names are resolved in the top-level ' 'namespace by\n' 'searching the global namespace, i.e. the namespace of the ' 'module\n' 'containing the code block, and the builtins namespace, the ' 'namespace\n' 'of the module "__builtin__". The global namespace is searched ' 'first.\n' 'If the name is not found there, the builtins namespace is ' 'searched.\n' 'The global statement must precede all uses of the name.\n' '\n' 'The builtins namespace associated with the execution of a code ' 'block\n' 'is actually found by looking up the name "__builtins__" in its ' 'global\n' 'namespace; this should be a dictionary or a module (in the ' 'latter case\n' "the module's dictionary is used). By default, when in the " '"__main__"\n' 'module, "__builtins__" is the built-in module "__builtin__" ' '(note: no\n' '\'s\'); when in any other module, "__builtins__" is an alias ' 'for the\n' 'dictionary of the "__builtin__" module itself. "__builtins__" ' 'can be\n' 'set to a user-created dictionary to create a weak form of ' 'restricted\n' 'execution.\n' '\n' '**CPython implementation detail:** Users should not touch\n' '"__builtins__"; it is strictly an implementation detail. ' 'Users\n' 'wanting to override values in the builtins namespace should ' '"import"\n' 'the "__builtin__" (no \'s\') module and modify its attributes\n' 'appropriately.\n' '\n' 'The namespace for a module is automatically created the first ' 'time a\n' 'module is imported. The main module for a script is always ' 'called\n' '"__main__".\n' '\n' 'The "global" statement has the same scope as a name binding ' 'operation\n' 'in the same block. If the nearest enclosing scope for a free ' 'variable\n' 'contains a global statement, the free variable is treated as a ' 'global.\n' '\n' 'A class definition is an executable statement that may use and ' 'define\n' 'names. These references follow the normal rules for name ' 'resolution.\n' 'The namespace of the class definition becomes the attribute ' 'dictionary\n' 'of the class. Names defined at the class scope are not visible ' 'in\n' 'methods.\n' '\n' '\n' 'Interaction with dynamic features\n' '---------------------------------\n' '\n' 'There are several cases where Python statements are illegal ' 'when used\n' 'in conjunction with nested scopes that contain free variables.\n' '\n' 'If a variable is referenced in an enclosing scope, it is ' 'illegal to\n' 'delete the name. An error will be reported at compile time.\n' '\n' 'If the wild card form of import --- "import *" --- is used in ' 'a\n' 'function and the function contains or is a nested block with ' 'free\n' 'variables, the compiler will raise a "SyntaxError".\n' '\n' 'If "exec" is used in a function and the function contains or is ' 'a\n' 'nested block with free variables, the compiler will raise a\n' '"SyntaxError" unless the exec explicitly specifies the local ' 'namespace\n' 'for the "exec". (In other words, "exec obj" would be illegal, ' 'but\n' '"exec obj in ns" would be legal.)\n' '\n' 'The "eval()", "execfile()", and "input()" functions and the ' '"exec"\n' 'statement do not have access to the full environment for ' 'resolving\n' 'names. Names may be resolved in the local and global ' 'namespaces of\n' 'the caller. Free variables are not resolved in the nearest ' 'enclosing\n' 'namespace, but in the global namespace. [1] The "exec" ' 'statement and\n' 'the "eval()" and "execfile()" functions have optional arguments ' 'to\n' 'override the global and local namespace. If only one namespace ' 'is\n' 'specified, it is used for both.\n' '\n' '\n' 'Exceptions\n' '==========\n' '\n' 'Exceptions are a means of breaking out of the normal flow of ' 'control\n' 'of a code block in order to handle errors or other exceptional\n' 'conditions. An exception is *raised* at the point where the ' 'error is\n' 'detected; it may be *handled* by the surrounding code block or ' 'by any\n' 'code block that directly or indirectly invoked the code block ' 'where\n' 'the error occurred.\n' '\n' 'The Python interpreter raises an exception when it detects a ' 'run-time\n' 'error (such as division by zero). A Python program can also\n' 'explicitly raise an exception with the "raise" statement. ' 'Exception\n' 'handlers are specified with the "try" ... "except" statement. ' 'The\n' '"finally" clause of such a statement can be used to specify ' 'cleanup\n' 'code which does not handle the exception, but is executed ' 'whether an\n' 'exception occurred or not in the preceding code.\n' '\n' 'Python uses the "termination" model of error handling: an ' 'exception\n' 'handler can find out what happened and continue execution at an ' 'outer\n' 'level, but it cannot repair the cause of the error and retry ' 'the\n' 'failing operation (except by re-entering the offending piece of ' 'code\n' 'from the top).\n' '\n' 'When an exception is not handled at all, the interpreter ' 'terminates\n' 'execution of the program, or returns to its interactive main ' 'loop. In\n' 'either case, it prints a stack backtrace, except when the ' 'exception is\n' '"SystemExit".\n' '\n' 'Exceptions are identified by class instances. The "except" ' 'clause is\n' 'selected depending on the class of the instance: it must ' 'reference the\n' 'class of the instance or a base class thereof. The instance ' 'can be\n' 'received by the handler and can carry additional information ' 'about the\n' 'exceptional condition.\n' '\n' 'Exceptions can also be identified by strings, in which case ' 'the\n' '"except" clause is selected by object identity. An arbitrary ' 'value\n' 'can be raised along with the identifying string which can be ' 'passed to\n' 'the handler.\n' '\n' 'Note: Messages to exceptions are not part of the Python API. ' 'Their\n' ' contents may change from one version of Python to the next ' 'without\n' ' warning and should not be relied on by code which will run ' 'under\n' ' multiple versions of the interpreter.\n' '\n' 'See also the description of the "try" statement in section The ' 'try\n' 'statement and "raise" statement in section The raise ' 'statement.\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] This limitation occurs because the code that is executed ' 'by\n' ' these operations is not available at the time the module ' 'is\n' ' compiled.\n', 'exprlists': '\n' 'Expression lists\n' '****************\n' '\n' ' expression_list ::= expression ( "," expression )* [","]\n' '\n' 'An expression list containing at least one comma yields a ' 'tuple. The\n' 'length of the tuple is the number of expressions in the list. ' 'The\n' 'expressions are evaluated from left to right.\n' '\n' 'The trailing comma is required only to create a single tuple ' '(a.k.a. a\n' '*singleton*); it is optional in all other cases. A single ' 'expression\n' "without a trailing comma doesn't create a tuple, but rather " 'yields the\n' 'value of that expression. (To create an empty tuple, use an ' 'empty pair\n' 'of parentheses: "()".)\n', 'floating': '\n' 'Floating point literals\n' '***********************\n' '\n' 'Floating point literals are described by the following lexical\n' 'definitions:\n' '\n' ' floatnumber ::= pointfloat | exponentfloat\n' ' pointfloat ::= [intpart] fraction | intpart "."\n' ' exponentfloat ::= (intpart | pointfloat) exponent\n' ' intpart ::= digit+\n' ' fraction ::= "." digit+\n' ' exponent ::= ("e" | "E") ["+" | "-"] digit+\n' '\n' 'Note that the integer and exponent parts of floating point ' 'numbers can\n' 'look like octal integers, but are interpreted using radix 10. ' 'For\n' 'example, "077e010" is legal, and denotes the same number as ' '"77e10".\n' 'The allowed range of floating point literals is implementation-\n' 'dependent. Some examples of floating point literals:\n' '\n' ' 3.14 10. .001 1e100 3.14e-10 0e0\n' '\n' 'Note that numeric literals do not include a sign; a phrase like ' '"-1"\n' 'is actually an expression composed of the unary operator "-" and ' 'the\n' 'literal "1".\n', 'for': '\n' 'The "for" statement\n' '*******************\n' '\n' 'The "for" statement is used to iterate over the elements of a ' 'sequence\n' '(such as a string, tuple or list) or other iterable object:\n' '\n' ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n' ' ["else" ":" suite]\n' '\n' 'The expression list is evaluated once; it should yield an iterable\n' 'object. An iterator is created for the result of the\n' '"expression_list". The suite is then executed once for each item\n' 'provided by the iterator, in the order of ascending indices. Each\n' 'item in turn is assigned to the target list using the standard rules\n' 'for assignments, and then the suite is executed. When the items are\n' 'exhausted (which is immediately when the sequence is empty), the ' 'suite\n' 'in the "else" clause, if present, is executed, and the loop\n' 'terminates.\n' '\n' 'A "break" statement executed in the first suite terminates the loop\n' 'without executing the "else" clause\'s suite. A "continue" ' 'statement\n' 'executed in the first suite skips the rest of the suite and ' 'continues\n' 'with the next item, or with the "else" clause if there was no next\n' 'item.\n' '\n' 'The suite may assign to the variable(s) in the target list; this ' 'does\n' 'not affect the next item assigned to it.\n' '\n' 'The target list is not deleted when the loop is finished, but if the\n' 'sequence is empty, it will not have been assigned to at all by the\n' 'loop. Hint: the built-in function "range()" returns a sequence of\n' 'integers suitable to emulate the effect of Pascal\'s "for i := a to ' 'b\n' 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' '\n' 'Note: There is a subtlety when the sequence is being modified by the\n' ' loop (this can only occur for mutable sequences, i.e. lists). An\n' ' internal counter is used to keep track of which item is used next,\n' ' and this is incremented on each iteration. When this counter has\n' ' reached the length of the sequence the loop terminates. This ' 'means\n' ' that if the suite deletes the current (or a previous) item from ' 'the\n' ' sequence, the next item will be skipped (since it gets the index ' 'of\n' ' the current item which has already been treated). Likewise, if ' 'the\n' ' suite inserts an item in the sequence before the current item, the\n' ' current item will be treated again the next time through the loop.\n' ' This can lead to nasty bugs that can be avoided by making a\n' ' temporary copy using a slice of the whole sequence, e.g.,\n' '\n' ' for x in a[:]:\n' ' if x < 0: a.remove(x)\n', 'formatstrings': '\n' 'Format String Syntax\n' '********************\n' '\n' 'The "str.format()" method and the "Formatter" class share ' 'the same\n' 'syntax for format strings (although in the case of ' '"Formatter",\n' 'subclasses can define their own format string syntax).\n' '\n' 'Format strings contain "replacement fields" surrounded by ' 'curly braces\n' '"{}". Anything that is not contained in braces is ' 'considered literal\n' 'text, which is copied unchanged to the output. If you need ' 'to include\n' 'a brace character in the literal text, it can be escaped by ' 'doubling:\n' '"{{" and "}}".\n' '\n' 'The grammar for a replacement field is as follows:\n' '\n' ' replacement_field ::= "{" [field_name] ["!" ' 'conversion] [":" format_spec] "}"\n' ' field_name ::= arg_name ("." attribute_name | ' '"[" element_index "]")*\n' ' arg_name ::= [identifier | integer]\n' ' attribute_name ::= identifier\n' ' element_index ::= integer | index_string\n' ' index_string ::= +\n' ' conversion ::= "r" | "s"\n' ' format_spec ::= \n' '\n' 'In less formal terms, the replacement field can start with ' 'a\n' '*field_name* that specifies the object whose value is to be ' 'formatted\n' 'and inserted into the output instead of the replacement ' 'field. The\n' '*field_name* is optionally followed by a *conversion* ' 'field, which is\n' 'preceded by an exclamation point "\'!\'", and a ' '*format_spec*, which is\n' 'preceded by a colon "\':\'". These specify a non-default ' 'format for the\n' 'replacement value.\n' '\n' 'See also the Format Specification Mini-Language section.\n' '\n' 'The *field_name* itself begins with an *arg_name* that is ' 'either a\n' "number or a keyword. If it's a number, it refers to a " 'positional\n' "argument, and if it's a keyword, it refers to a named " 'keyword\n' 'argument. If the numerical arg_names in a format string ' 'are 0, 1, 2,\n' '... in sequence, they can all be omitted (not just some) ' 'and the\n' 'numbers 0, 1, 2, ... will be automatically inserted in that ' 'order.\n' 'Because *arg_name* is not quote-delimited, it is not ' 'possible to\n' 'specify arbitrary dictionary keys (e.g., the strings ' '"\'10\'" or\n' '"\':-]\'") within a format string. The *arg_name* can be ' 'followed by any\n' 'number of index or attribute expressions. An expression of ' 'the form\n' '"\'.name\'" selects the named attribute using "getattr()", ' 'while an\n' 'expression of the form "\'[index]\'" does an index lookup ' 'using\n' '"__getitem__()".\n' '\n' 'Changed in version 2.7: The positional argument specifiers ' 'can be\n' 'omitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n' '\n' 'Some simple format string examples:\n' '\n' ' "First, thou shalt count to {0}" # References first ' 'positional argument\n' ' "Bring me a {}" # Implicitly ' 'references the first positional argument\n' ' "From {} to {}" # Same as "From {0} to ' '{1}"\n' ' "My quest is {name}" # References keyword ' "argument 'name'\n" ' "Weight in tons {0.weight}" # \'weight\' attribute ' 'of first positional arg\n' ' "Units destroyed: {players[0]}" # First element of ' "keyword argument 'players'.\n" '\n' 'The *conversion* field causes a type coercion before ' 'formatting.\n' 'Normally, the job of formatting a value is done by the ' '"__format__()"\n' 'method of the value itself. However, in some cases it is ' 'desirable to\n' 'force a type to be formatted as a string, overriding its ' 'own\n' 'definition of formatting. By converting the value to a ' 'string before\n' 'calling "__format__()", the normal formatting logic is ' 'bypassed.\n' '\n' 'Two conversion flags are currently supported: "\'!s\'" ' 'which calls\n' '"str()" on the value, and "\'!r\'" which calls "repr()".\n' '\n' 'Some examples:\n' '\n' ' "Harold\'s a clever {0!s}" # Calls str() on the ' 'argument first\n' ' "Bring out the holy {name!r}" # Calls repr() on the ' 'argument first\n' '\n' 'The *format_spec* field contains a specification of how the ' 'value\n' 'should be presented, including such details as field width, ' 'alignment,\n' 'padding, decimal precision and so on. Each value type can ' 'define its\n' 'own "formatting mini-language" or interpretation of the ' '*format_spec*.\n' '\n' 'Most built-in types support a common formatting ' 'mini-language, which\n' 'is described in the next section.\n' '\n' 'A *format_spec* field can also include nested replacement ' 'fields\n' 'within it. These nested replacement fields may contain a ' 'field name,\n' 'conversion flag and format specification, but deeper ' 'nesting is not\n' 'allowed. The replacement fields within the format_spec ' 'are\n' 'substituted before the *format_spec* string is interpreted. ' 'This\n' 'allows the formatting of a value to be dynamically ' 'specified.\n' '\n' 'See the Format examples section for some examples.\n' '\n' '\n' 'Format Specification Mini-Language\n' '==================================\n' '\n' '"Format specifications" are used within replacement fields ' 'contained\n' 'within a format string to define how individual values are ' 'presented\n' '(see Format String Syntax). They can also be passed ' 'directly to the\n' 'built-in "format()" function. Each formattable type may ' 'define how\n' 'the format specification is to be interpreted.\n' '\n' 'Most built-in types implement the following options for ' 'format\n' 'specifications, although some of the formatting options are ' 'only\n' 'supported by the numeric types.\n' '\n' 'A general convention is that an empty format string ("""") ' 'produces\n' 'the same result as if you had called "str()" on the value. ' 'A non-empty\n' 'format string typically modifies the result.\n' '\n' 'The general form of a *standard format specifier* is:\n' '\n' ' format_spec ::= ' '[[fill]align][sign][#][0][width][,][.precision][type]\n' ' fill ::= \n' ' align ::= "<" | ">" | "=" | "^"\n' ' sign ::= "+" | "-" | " "\n' ' width ::= integer\n' ' precision ::= integer\n' ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" ' '| "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' '\n' 'If a valid *align* value is specified, it can be preceded ' 'by a *fill*\n' 'character that can be any character and defaults to a space ' 'if\n' 'omitted. It is not possible to use a literal curly brace ' '(""{"" or\n' '""}"") as the *fill* character when using the ' '"str.format()" method.\n' 'However, it is possible to insert a curly brace with a ' 'nested\n' "replacement field. This limitation doesn't affect the " '"format()"\n' 'function.\n' '\n' 'The meaning of the various alignment options is as ' 'follows:\n' '\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | Option | ' 'Meaning ' '|\n' ' ' '+===========+============================================================+\n' ' | "\'<\'" | Forces the field to be left-aligned ' 'within the available |\n' ' | | space (this is the default for most ' 'objects). |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'>\'" | Forces the field to be right-aligned ' 'within the available |\n' ' | | space (this is the default for ' 'numbers). |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'=\'" | Forces the padding to be placed after ' 'the sign (if any) |\n' ' | | but before the digits. This is used for ' 'printing fields |\n' " | | in the form '+000000120'. This alignment " 'option is only |\n' ' | | valid for numeric types. It becomes the ' "default when '0' |\n" ' | | immediately precedes the field ' 'width. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'^\'" | Forces the field to be centered within ' 'the available |\n' ' | | ' 'space. ' '|\n' ' ' '+-----------+------------------------------------------------------------+\n' '\n' 'Note that unless a minimum field width is defined, the ' 'field width\n' 'will always be the same size as the data to fill it, so ' 'that the\n' 'alignment option has no meaning in this case.\n' '\n' 'The *sign* option is only valid for number types, and can ' 'be one of\n' 'the following:\n' '\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | Option | ' 'Meaning ' '|\n' ' ' '+===========+============================================================+\n' ' | "\'+\'" | indicates that a sign should be used for ' 'both positive as |\n' ' | | well as negative ' 'numbers. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'-\'" | indicates that a sign should be used ' 'only for negative |\n' ' | | numbers (this is the default ' 'behavior). |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | space | indicates that a leading space should be ' 'used on positive |\n' ' | | numbers, and a minus sign on negative ' 'numbers. |\n' ' ' '+-----------+------------------------------------------------------------+\n' '\n' 'The "\'#\'" option is only valid for integers, and only for ' 'binary,\n' 'octal, or hexadecimal output. If present, it specifies ' 'that the\n' 'output will be prefixed by "\'0b\'", "\'0o\'", or "\'0x\'", ' 'respectively.\n' '\n' 'The "\',\'" option signals the use of a comma for a ' 'thousands separator.\n' 'For a locale aware separator, use the "\'n\'" integer ' 'presentation type\n' 'instead.\n' '\n' 'Changed in version 2.7: Added the "\',\'" option (see also ' '**PEP 378**).\n' '\n' '*width* is a decimal integer defining the minimum field ' 'width. If not\n' 'specified, then the field width will be determined by the ' 'content.\n' '\n' 'When no explicit alignment is given, preceding the *width* ' 'field by a\n' 'zero ("\'0\'") character enables sign-aware zero-padding ' 'for numeric\n' 'types. This is equivalent to a *fill* character of "\'0\'" ' 'with an\n' '*alignment* type of "\'=\'".\n' '\n' 'The *precision* is a decimal number indicating how many ' 'digits should\n' 'be displayed after the decimal point for a floating point ' 'value\n' 'formatted with "\'f\'" and "\'F\'", or before and after the ' 'decimal point\n' 'for a floating point value formatted with "\'g\'" or ' '"\'G\'". For non-\n' 'number types the field indicates the maximum field size - ' 'in other\n' 'words, how many characters will be used from the field ' 'content. The\n' '*precision* is not allowed for integer values.\n' '\n' 'Finally, the *type* determines how the data should be ' 'presented.\n' '\n' 'The available string presentation types are:\n' '\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | Type | ' 'Meaning ' '|\n' ' ' '+===========+============================================================+\n' ' | "\'s\'" | String format. This is the default type ' 'for strings and |\n' ' | | may be ' 'omitted. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | None | The same as ' '"\'s\'". |\n' ' ' '+-----------+------------------------------------------------------------+\n' '\n' 'The available integer presentation types are:\n' '\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | Type | ' 'Meaning ' '|\n' ' ' '+===========+============================================================+\n' ' | "\'b\'" | Binary format. Outputs the number in ' 'base 2. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'c\'" | Character. Converts the integer to the ' 'corresponding |\n' ' | | unicode character before ' 'printing. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'d\'" | Decimal Integer. Outputs the number in ' 'base 10. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'o\'" | Octal format. Outputs the number in base ' '8. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'x\'" | Hex format. Outputs the number in base ' '16, using lower- |\n' ' | | case letters for the digits above ' '9. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'X\'" | Hex format. Outputs the number in base ' '16, using upper- |\n' ' | | case letters for the digits above ' '9. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'n\'" | Number. This is the same as "\'d\'", ' 'except that it uses the |\n' ' | | current locale setting to insert the ' 'appropriate number |\n' ' | | separator ' 'characters. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | None | The same as ' '"\'d\'". |\n' ' ' '+-----------+------------------------------------------------------------+\n' '\n' 'In addition to the above presentation types, integers can ' 'be formatted\n' 'with the floating point presentation types listed below ' '(except "\'n\'"\n' 'and "None"). When doing so, "float()" is used to convert ' 'the integer\n' 'to a floating point number before formatting.\n' '\n' 'The available presentation types for floating point and ' 'decimal values\n' 'are:\n' '\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | Type | ' 'Meaning ' '|\n' ' ' '+===========+============================================================+\n' ' | "\'e\'" | Exponent notation. Prints the number in ' 'scientific |\n' " | | notation using the letter 'e' to indicate " 'the exponent. |\n' ' | | The default precision is ' '"6". |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'E\'" | Exponent notation. Same as "\'e\'" ' 'except it uses an upper |\n' " | | case 'E' as the separator " 'character. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'f\'" | Fixed point. Displays the number as a ' 'fixed-point number. |\n' ' | | The default precision is ' '"6". |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'F\'" | Fixed point. Same as ' '"\'f\'". |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'g\'" | General format. For a given precision ' '"p >= 1", this |\n' ' | | rounds the number to "p" significant ' 'digits and then |\n' ' | | formats the result in either fixed-point ' 'format or in |\n' ' | | scientific notation, depending on its ' 'magnitude. The |\n' ' | | precise rules are as follows: suppose that ' 'the result |\n' ' | | formatted with presentation type "\'e\'" ' 'and precision "p-1" |\n' ' | | would have exponent "exp". Then if "-4 <= ' 'exp < p", the |\n' ' | | number is formatted with presentation type ' '"\'f\'" and |\n' ' | | precision "p-1-exp". Otherwise, the ' 'number is formatted |\n' ' | | with presentation type "\'e\'" and ' 'precision "p-1". In both |\n' ' | | cases insignificant trailing zeros are ' 'removed from the |\n' ' | | significand, and the decimal point is also ' 'removed if |\n' ' | | there are no remaining digits following ' 'it. Positive and |\n' ' | | negative infinity, positive and negative ' 'zero, and nans, |\n' ' | | are formatted as "inf", "-inf", "0", "-0" ' 'and "nan" |\n' ' | | respectively, regardless of the ' 'precision. A precision of |\n' ' | | "0" is treated as equivalent to a ' 'precision of "1". The |\n' ' | | default precision is ' '"6". |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'G\'" | General format. Same as "\'g\'" except ' 'switches to "\'E\'" if |\n' ' | | the number gets too large. The ' 'representations of infinity |\n' ' | | and NaN are uppercased, ' 'too. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'n\'" | Number. This is the same as "\'g\'", ' 'except that it uses the |\n' ' | | current locale setting to insert the ' 'appropriate number |\n' ' | | separator ' 'characters. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | "\'%\'" | Percentage. Multiplies the number by 100 ' 'and displays in |\n' ' | | fixed ("\'f\'") format, followed by a ' 'percent sign. |\n' ' ' '+-----------+------------------------------------------------------------+\n' ' | None | The same as ' '"\'g\'". |\n' ' ' '+-----------+------------------------------------------------------------+\n' '\n' '\n' 'Format examples\n' '===============\n' '\n' 'This section contains examples of the "str.format()" syntax ' 'and\n' 'comparison with the old "%"-formatting.\n' '\n' 'In most of the cases the syntax is similar to the old ' '"%"-formatting,\n' 'with the addition of the "{}" and with ":" used instead of ' '"%". For\n' 'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n' '\n' 'The new format syntax also supports new and different ' 'options, shown\n' 'in the follow examples.\n' '\n' 'Accessing arguments by position:\n' '\n' " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" " 'a, b, c'\n" " >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only\n" " 'a, b, c'\n" " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" " 'c, b, a'\n" " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " 'argument sequence\n' " 'c, b, a'\n" " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " 'indices can be repeated\n' " 'abracadabra'\n" '\n' 'Accessing arguments by name:\n' '\n' " >>> 'Coordinates: {latitude}, " "{longitude}'.format(latitude='37.24N', " "longitude='-115.81W')\n" " 'Coordinates: 37.24N, -115.81W'\n" " >>> coord = {'latitude': '37.24N', 'longitude': " "'-115.81W'}\n" " >>> 'Coordinates: {latitude}, " "{longitude}'.format(**coord)\n" " 'Coordinates: 37.24N, -115.81W'\n" '\n' "Accessing arguments' attributes:\n" '\n' ' >>> c = 3-5j\n' " >>> ('The complex number {0} is formed from the real " "part {0.real} '\n" " ... 'and the imaginary part {0.imag}.').format(c)\n" " 'The complex number (3-5j) is formed from the real part " "3.0 and the imaginary part -5.0.'\n" ' >>> class Point(object):\n' ' ... def __init__(self, x, y):\n' ' ... self.x, self.y = x, y\n' ' ... def __str__(self):\n' " ... return 'Point({self.x}, " "{self.y})'.format(self=self)\n" ' ...\n' ' >>> str(Point(4, 2))\n' " 'Point(4, 2)'\n" '\n' "Accessing arguments' items:\n" '\n' ' >>> coord = (3, 5)\n' " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" " 'X: 3; Y: 5'\n" '\n' 'Replacing "%s" and "%r":\n' '\n' ' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' '{!s}".format(\'test1\', \'test2\')\n' ' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n' '\n' 'Aligning the text and specifying a width:\n' '\n' " >>> '{:<30}'.format('left aligned')\n" " 'left aligned '\n" " >>> '{:>30}'.format('right aligned')\n" " ' right aligned'\n" " >>> '{:^30}'.format('centered')\n" " ' centered '\n" " >>> '{:*^30}'.format('centered') # use '*' as a fill " 'char\n' " '***********centered***********'\n" '\n' 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' '\n' " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " 'always\n' " '+3.140000; -3.140000'\n" " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " 'for positive numbers\n' " ' 3.140000; -3.140000'\n" " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the " "minus -- same as '{:f}; {:f}'\n" " '3.140000; -3.140000'\n" '\n' 'Replacing "%x" and "%o" and converting the value to ' 'different bases:\n' '\n' ' >>> # format also supports binary numbers\n' ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' '{0:b}".format(42)\n' " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" ' >>> # with 0x, 0o, or 0b as prefix:\n' ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' '{0:#b}".format(42)\n' " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" '\n' 'Using the comma as a thousands separator:\n' '\n' " >>> '{:,}'.format(1234567890)\n" " '1,234,567,890'\n" '\n' 'Expressing a percentage:\n' '\n' ' >>> points = 19.5\n' ' >>> total = 22\n' " >>> 'Correct answers: {:.2%}'.format(points/total)\n" " 'Correct answers: 88.64%'\n" '\n' 'Using type-specific formatting:\n' '\n' ' >>> import datetime\n' ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" " '2010-07-04 12:15:58'\n" '\n' 'Nesting arguments and more complex examples:\n' '\n' " >>> for align, text in zip('<^>', ['left', 'center', " "'right']):\n" " ... '{0:{fill}{align}16}'.format(text, fill=align, " 'align=align)\n' ' ...\n' " 'left<<<<<<<<<<<<'\n" " '^^^^^center^^^^^'\n" " '>>>>>>>>>>>right'\n" ' >>>\n' ' >>> octets = [192, 168, 0, 1]\n' " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" " 'C0A80001'\n" ' >>> int(_, 16)\n' ' 3232235521\n' ' >>>\n' ' >>> width = 5\n' ' >>> for num in range(5,12):\n' " ... for base in 'dXob':\n" " ... print '{0:{width}{base}}'.format(num, " 'base=base, width=width),\n' ' ... print\n' ' ...\n' ' 5 5 5 101\n' ' 6 6 6 110\n' ' 7 7 7 111\n' ' 8 8 10 1000\n' ' 9 9 11 1001\n' ' 10 A 12 1010\n' ' 11 B 13 1011\n', 'function': '\n' 'Function definitions\n' '********************\n' '\n' 'A function definition defines a user-defined function object ' '(see\n' 'section The standard type hierarchy):\n' '\n' ' decorated ::= decorators (classdef | funcdef)\n' ' decorators ::= decorator+\n' ' decorator ::= "@" dotted_name ["(" [argument_list [","]] ' '")"] NEWLINE\n' ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' '":" suite\n' ' dotted_name ::= identifier ("." identifier)*\n' ' parameter_list ::= (defparameter ",")*\n' ' ( "*" identifier ["," "**" identifier]\n' ' | "**" identifier\n' ' | defparameter [","] )\n' ' defparameter ::= parameter ["=" expression]\n' ' sublist ::= parameter ("," parameter)* [","]\n' ' parameter ::= identifier | "(" sublist ")"\n' ' funcname ::= identifier\n' '\n' 'A function definition is an executable statement. Its execution ' 'binds\n' 'the function name in the current local namespace to a function ' 'object\n' '(a wrapper around the executable code for the function). This\n' 'function object contains a reference to the current global ' 'namespace\n' 'as the global namespace to be used when the function is called.\n' '\n' 'The function definition does not execute the function body; this ' 'gets\n' 'executed only when the function is called. [3]\n' '\n' 'A function definition may be wrapped by one or more *decorator*\n' 'expressions. Decorator expressions are evaluated when the ' 'function is\n' 'defined, in the scope that contains the function definition. ' 'The\n' 'result must be a callable, which is invoked with the function ' 'object\n' 'as the only argument. The returned value is bound to the ' 'function name\n' 'instead of the function object. Multiple decorators are applied ' 'in\n' 'nested fashion. For example, the following code:\n' '\n' ' @f1(arg)\n' ' @f2\n' ' def func(): pass\n' '\n' 'is equivalent to:\n' '\n' ' def func(): pass\n' ' func = f1(arg)(f2(func))\n' '\n' 'When one or more top-level *parameters* have the form ' '*parameter* "="\n' '*expression*, the function is said to have "default parameter ' 'values."\n' 'For a parameter with a default value, the corresponding ' '*argument* may\n' "be omitted from a call, in which case the parameter's default " 'value is\n' 'substituted. If a parameter has a default value, all following\n' 'parameters must also have a default value --- this is a ' 'syntactic\n' 'restriction that is not expressed by the grammar.\n' '\n' '**Default parameter values are evaluated when the function ' 'definition\n' 'is executed.** This means that the expression is evaluated ' 'once, when\n' 'the function is defined, and that the same "pre-computed" value ' 'is\n' 'used for each call. This is especially important to understand ' 'when a\n' 'default parameter is a mutable object, such as a list or a ' 'dictionary:\n' 'if the function modifies the object (e.g. by appending an item ' 'to a\n' 'list), the default value is in effect modified. This is ' 'generally not\n' 'what was intended. A way around this is to use "None" as the\n' 'default, and explicitly test for it in the body of the function, ' 'e.g.:\n' '\n' ' def whats_on_the_telly(penguin=None):\n' ' if penguin is None:\n' ' penguin = []\n' ' penguin.append("property of the zoo")\n' ' return penguin\n' '\n' 'Function call semantics are described in more detail in section ' 'Calls.\n' 'A function call always assigns values to all parameters ' 'mentioned in\n' 'the parameter list, either from position arguments, from ' 'keyword\n' 'arguments, or from default values. If the form ""*identifier"" ' 'is\n' 'present, it is initialized to a tuple receiving any excess ' 'positional\n' 'parameters, defaulting to the empty tuple. If the form\n' '""**identifier"" is present, it is initialized to a new ' 'dictionary\n' 'receiving any excess keyword arguments, defaulting to a new ' 'empty\n' 'dictionary.\n' '\n' 'It is also possible to create anonymous functions (functions not ' 'bound\n' 'to a name), for immediate use in expressions. This uses lambda\n' 'expressions, described in section Lambdas. Note that the ' 'lambda\n' 'expression is merely a shorthand for a simplified function ' 'definition;\n' 'a function defined in a ""def"" statement can be passed around ' 'or\n' 'assigned to another name just like a function defined by a ' 'lambda\n' 'expression. The ""def"" form is actually more powerful since ' 'it\n' 'allows the execution of multiple statements.\n' '\n' "**Programmer's note:** Functions are first-class objects. A " '""def""\n' 'form executed inside a function definition defines a local ' 'function\n' 'that can be returned or passed around. Free variables used in ' 'the\n' 'nested function can access the local variables of the function\n' 'containing the def. See section Naming and binding for ' 'details.\n', 'global': '\n' 'The "global" statement\n' '**********************\n' '\n' ' global_stmt ::= "global" identifier ("," identifier)*\n' '\n' 'The "global" statement is a declaration which holds for the ' 'entire\n' 'current code block. It means that the listed identifiers are to ' 'be\n' 'interpreted as globals. It would be impossible to assign to a ' 'global\n' 'variable without "global", although free variables may refer to\n' 'globals without being declared global.\n' '\n' 'Names listed in a "global" statement must not be used in the same ' 'code\n' 'block textually preceding that "global" statement.\n' '\n' 'Names listed in a "global" statement must not be defined as ' 'formal\n' 'parameters or in a "for" loop control target, "class" definition,\n' 'function definition, or "import" statement.\n' '\n' '**CPython implementation detail:** The current implementation does ' 'not\n' 'enforce the latter two restrictions, but programs should not ' 'abuse\n' 'this freedom, as future implementations may enforce them or ' 'silently\n' 'change the meaning of the program.\n' '\n' '**Programmer\'s note:** "global" is a directive to the parser. ' 'It\n' 'applies only to code parsed at the same time as the "global"\n' 'statement. In particular, a "global" statement contained in an ' '"exec"\n' 'statement does not affect the code block *containing* the "exec"\n' 'statement, and code contained in an "exec" statement is unaffected ' 'by\n' '"global" statements in the code containing the "exec" statement. ' 'The\n' 'same applies to the "eval()", "execfile()" and "compile()" ' 'functions.\n', 'id-classes': '\n' 'Reserved classes of identifiers\n' '*******************************\n' '\n' 'Certain classes of identifiers (besides keywords) have ' 'special\n' 'meanings. These classes are identified by the patterns of ' 'leading and\n' 'trailing underscore characters:\n' '\n' '"_*"\n' ' Not imported by "from module import *". The special ' 'identifier "_"\n' ' is used in the interactive interpreter to store the result ' 'of the\n' ' last evaluation; it is stored in the "__builtin__" module. ' 'When\n' ' not in interactive mode, "_" has no special meaning and is ' 'not\n' ' defined. See section The import statement.\n' '\n' ' Note: The name "_" is often used in conjunction with\n' ' internationalization; refer to the documentation for the\n' ' "gettext" module for more information on this ' 'convention.\n' '\n' '"__*__"\n' ' System-defined names. These names are defined by the ' 'interpreter\n' ' and its implementation (including the standard library). ' 'Current\n' ' system names are discussed in the Special method names ' 'section and\n' ' elsewhere. More will likely be defined in future versions ' 'of\n' ' Python. *Any* use of "__*__" names, in any context, that ' 'does not\n' ' follow explicitly documented use, is subject to breakage ' 'without\n' ' warning.\n' '\n' '"__*"\n' ' Class-private names. Names in this category, when used ' 'within the\n' ' context of a class definition, are re-written to use a ' 'mangled form\n' ' to help avoid name clashes between "private" attributes of ' 'base and\n' ' derived classes. See section Identifiers (Names).\n', 'identifiers': '\n' 'Identifiers and keywords\n' '************************\n' '\n' 'Identifiers (also referred to as *names*) are described by ' 'the\n' 'following lexical definitions:\n' '\n' ' identifier ::= (letter|"_") (letter | digit | "_")*\n' ' letter ::= lowercase | uppercase\n' ' lowercase ::= "a"..."z"\n' ' uppercase ::= "A"..."Z"\n' ' digit ::= "0"..."9"\n' '\n' 'Identifiers are unlimited in length. Case is significant.\n' '\n' '\n' 'Keywords\n' '========\n' '\n' 'The following identifiers are used as reserved words, or ' '*keywords* of\n' 'the language, and cannot be used as ordinary identifiers. ' 'They must\n' 'be spelled exactly as written here:\n' '\n' ' and del from not while\n' ' as elif global or with\n' ' assert else if pass yield\n' ' break except import print\n' ' class exec in raise\n' ' continue finally is return\n' ' def for lambda try\n' '\n' 'Changed in version 2.4: "None" became a constant and is now ' 'recognized\n' 'by the compiler as a name for the built-in object "None". ' 'Although it\n' 'is not a keyword, you cannot assign a different object to ' 'it.\n' '\n' 'Changed in version 2.5: Using "as" and "with" as identifiers ' 'triggers\n' 'a warning. To use them as keywords, enable the ' '"with_statement"\n' 'future feature .\n' '\n' 'Changed in version 2.6: "as" and "with" are full keywords.\n' '\n' '\n' 'Reserved classes of identifiers\n' '===============================\n' '\n' 'Certain classes of identifiers (besides keywords) have ' 'special\n' 'meanings. These classes are identified by the patterns of ' 'leading and\n' 'trailing underscore characters:\n' '\n' '"_*"\n' ' Not imported by "from module import *". The special ' 'identifier "_"\n' ' is used in the interactive interpreter to store the result ' 'of the\n' ' last evaluation; it is stored in the "__builtin__" ' 'module. When\n' ' not in interactive mode, "_" has no special meaning and is ' 'not\n' ' defined. See section The import statement.\n' '\n' ' Note: The name "_" is often used in conjunction with\n' ' internationalization; refer to the documentation for ' 'the\n' ' "gettext" module for more information on this ' 'convention.\n' '\n' '"__*__"\n' ' System-defined names. These names are defined by the ' 'interpreter\n' ' and its implementation (including the standard library). ' 'Current\n' ' system names are discussed in the Special method names ' 'section and\n' ' elsewhere. More will likely be defined in future versions ' 'of\n' ' Python. *Any* use of "__*__" names, in any context, that ' 'does not\n' ' follow explicitly documented use, is subject to breakage ' 'without\n' ' warning.\n' '\n' '"__*"\n' ' Class-private names. Names in this category, when used ' 'within the\n' ' context of a class definition, are re-written to use a ' 'mangled form\n' ' to help avoid name clashes between "private" attributes of ' 'base and\n' ' derived classes. See section Identifiers (Names).\n', 'if': '\n' 'The "if" statement\n' '******************\n' '\n' 'The "if" statement is used for conditional execution:\n' '\n' ' if_stmt ::= "if" expression ":" suite\n' ' ( "elif" expression ":" suite )*\n' ' ["else" ":" suite]\n' '\n' 'It selects exactly one of the suites by evaluating the expressions ' 'one\n' 'by one until one is found to be true (see section Boolean operations\n' 'for the definition of true and false); then that suite is executed\n' '(and no other part of the "if" statement is executed or evaluated).\n' 'If all expressions are false, the suite of the "else" clause, if\n' 'present, is executed.\n', 'imaginary': '\n' 'Imaginary literals\n' '******************\n' '\n' 'Imaginary literals are described by the following lexical ' 'definitions:\n' '\n' ' imagnumber ::= (floatnumber | intpart) ("j" | "J")\n' '\n' 'An imaginary literal yields a complex number with a real part ' 'of 0.0.\n' 'Complex numbers are represented as a pair of floating point ' 'numbers\n' 'and have the same restrictions on their range. To create a ' 'complex\n' 'number with a nonzero real part, add a floating point number to ' 'it,\n' 'e.g., "(3+4j)". Some examples of imaginary literals:\n' '\n' ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j\n', 'import': '\n' 'The "import" statement\n' '**********************\n' '\n' ' import_stmt ::= "import" module ["as" name] ( "," module ' '["as" name] )*\n' ' | "from" relative_module "import" identifier ' '["as" name]\n' ' ( "," identifier ["as" name] )*\n' ' | "from" relative_module "import" "(" ' 'identifier ["as" name]\n' ' ( "," identifier ["as" name] )* [","] ")"\n' ' | "from" module "import" "*"\n' ' module ::= (identifier ".")* identifier\n' ' relative_module ::= "."* module | "."+\n' ' name ::= identifier\n' '\n' 'Import statements are executed in two steps: (1) find a module, ' 'and\n' 'initialize it if necessary; (2) define a name or names in the ' 'local\n' 'namespace (of the scope where the "import" statement occurs). The\n' 'statement comes in two forms differing on whether it uses the ' '"from"\n' 'keyword. The first form (without "from") repeats these steps for ' 'each\n' 'identifier in the list. The form with "from" performs step (1) ' 'once,\n' 'and then performs step (2) repeatedly.\n' '\n' 'To understand how step (1) occurs, one must first understand how\n' 'Python handles hierarchical naming of modules. To help organize\n' 'modules and provide a hierarchy in naming, Python has a concept ' 'of\n' 'packages. A package can contain other packages and modules while\n' 'modules cannot contain other modules or packages. From a file ' 'system\n' 'perspective, packages are directories and modules are files.\n' '\n' 'Once the name of the module is known (unless otherwise specified, ' 'the\n' 'term "module" will refer to both packages and modules), searching ' 'for\n' 'the module or package can begin. The first place checked is\n' '"sys.modules", the cache of all modules that have been imported\n' 'previously. If the module is found there then it is used in step ' '(2)\n' 'of import.\n' '\n' 'If the module is not found in the cache, then "sys.meta_path" is\n' 'searched (the specification for "sys.meta_path" can be found in ' '**PEP\n' '302**). The object is a list of *finder* objects which are queried ' 'in\n' 'order as to whether they know how to load the module by calling ' 'their\n' '"find_module()" method with the name of the module. If the module\n' 'happens to be contained within a package (as denoted by the ' 'existence\n' 'of a dot in the name), then a second argument to "find_module()" ' 'is\n' 'given as the value of the "__path__" attribute from the parent ' 'package\n' '(everything up to the last dot in the name of the module being\n' 'imported). If a finder can find the module it returns a *loader*\n' '(discussed later) or returns "None".\n' '\n' 'If none of the finders on "sys.meta_path" are able to find the ' 'module\n' 'then some implicitly defined finders are queried. Implementations ' 'of\n' 'Python vary in what implicit meta path finders are defined. The ' 'one\n' 'they all do define, though, is one that handles "sys.path_hooks",\n' '"sys.path_importer_cache", and "sys.path".\n' '\n' 'The implicit finder searches for the requested module in the ' '"paths"\n' 'specified in one of two places ("paths" do not have to be file ' 'system\n' 'paths). If the module being imported is supposed to be contained\n' 'within a package then the second argument passed to ' '"find_module()",\n' '"__path__" on the parent package, is used as the source of paths. ' 'If\n' 'the module is not contained in a package then "sys.path" is used ' 'as\n' 'the source of paths.\n' '\n' 'Once the source of paths is chosen it is iterated over to find a\n' 'finder that can handle that path. The dict at\n' '"sys.path_importer_cache" caches finders for paths and is checked ' 'for\n' 'a finder. If the path does not have a finder cached then\n' '"sys.path_hooks" is searched by calling each object in the list ' 'with a\n' 'single argument of the path, returning a finder or raises\n' '"ImportError". If a finder is returned then it is cached in\n' '"sys.path_importer_cache" and then used for that path entry. If ' 'no\n' 'finder can be found but the path exists then a value of "None" is\n' 'stored in "sys.path_importer_cache" to signify that an implicit, ' 'file-\n' 'based finder that handles modules stored as individual files ' 'should be\n' 'used for that path. If the path does not exist then a finder ' 'which\n' 'always returns "None" is placed in the cache for the path.\n' '\n' 'If no finder can find the module then "ImportError" is raised.\n' 'Otherwise some finder returned a loader whose "load_module()" ' 'method\n' 'is called with the name of the module to load (see **PEP 302** for ' 'the\n' 'original definition of loaders). A loader has several ' 'responsibilities\n' 'to perform on a module it loads. First, if the module already ' 'exists\n' 'in "sys.modules" (a possibility if the loader is called outside of ' 'the\n' 'import machinery) then it is to use that module for initialization ' 'and\n' 'not a new module. But if the module does not exist in ' '"sys.modules"\n' 'then it is to be added to that dict before initialization begins. ' 'If\n' 'an error occurs during loading of the module and it was added to\n' '"sys.modules" it is to be removed from the dict. If an error ' 'occurs\n' 'but the module was already in "sys.modules" it is left in the ' 'dict.\n' '\n' 'The loader must set several attributes on the module. "__name__" ' 'is to\n' 'be set to the name of the module. "__file__" is to be the "path" ' 'to\n' 'the file unless the module is built-in (and thus listed in\n' '"sys.builtin_module_names") in which case the attribute is not ' 'set. If\n' 'what is being imported is a package then "__path__" is to be set ' 'to a\n' 'list of paths to be searched when looking for modules and ' 'packages\n' 'contained within the package being imported. "__package__" is ' 'optional\n' 'but should be set to the name of package that contains the module ' 'or\n' 'package (the empty string is used for module not contained in a\n' 'package). "__loader__" is also optional but should be set to the\n' 'loader object that is loading the module.\n' '\n' 'If an error occurs during loading then the loader raises ' '"ImportError"\n' 'if some other exception is not already being propagated. Otherwise ' 'the\n' 'loader returns the module that was loaded and initialized.\n' '\n' 'When step (1) finishes without raising an exception, step (2) can\n' 'begin.\n' '\n' 'The first form of "import" statement binds the module name in the\n' 'local namespace to the module object, and then goes on to import ' 'the\n' 'next identifier, if any. If the module name is followed by "as", ' 'the\n' 'name following "as" is used as the local name for the module.\n' '\n' 'The "from" form does not bind the module name: it goes through ' 'the\n' 'list of identifiers, looks each one of them up in the module found ' 'in\n' 'step (1), and binds the name in the local namespace to the object ' 'thus\n' 'found. As with the first form of "import", an alternate local ' 'name\n' 'can be supplied by specifying ""as" localname". If a name is not\n' 'found, "ImportError" is raised. If the list of identifiers is\n' 'replaced by a star ("\'*\'"), all public names defined in the ' 'module are\n' 'bound in the local namespace of the "import" statement..\n' '\n' 'The *public names* defined by a module are determined by checking ' 'the\n' 'module\'s namespace for a variable named "__all__"; if defined, it ' 'must\n' 'be a sequence of strings which are names defined or imported by ' 'that\n' 'module. The names given in "__all__" are all considered public ' 'and\n' 'are required to exist. If "__all__" is not defined, the set of ' 'public\n' "names includes all names found in the module's namespace which do " 'not\n' 'begin with an underscore character ("\'_\'"). "__all__" should ' 'contain\n' 'the entire public API. It is intended to avoid accidentally ' 'exporting\n' 'items that are not part of the API (such as library modules which ' 'were\n' 'imported and used within the module).\n' '\n' 'The "from" form with "*" may only occur in a module scope. If ' 'the\n' 'wild card form of import --- "import *" --- is used in a function ' 'and\n' 'the function contains or is a nested block with free variables, ' 'the\n' 'compiler will raise a "SyntaxError".\n' '\n' 'When specifying what module to import you do not have to specify ' 'the\n' 'absolute name of the module. When a module or package is ' 'contained\n' 'within another package it is possible to make a relative import ' 'within\n' 'the same top package without having to mention the package name. ' 'By\n' 'using leading dots in the specified module or package after "from" ' 'you\n' 'can specify how high to traverse up the current package hierarchy\n' 'without specifying exact names. One leading dot means the current\n' 'package where the module making the import exists. Two dots means ' 'up\n' 'one package level. Three dots is up two levels, etc. So if you ' 'execute\n' '"from . import mod" from a module in the "pkg" package then you ' 'will\n' 'end up importing "pkg.mod". If you execute "from ..subpkg2 import ' 'mod"\n' 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n' 'specification for relative imports is contained within **PEP ' '328**.\n' '\n' '"importlib.import_module()" is provided to support applications ' 'that\n' 'determine which modules need to be loaded dynamically.\n' '\n' '\n' 'Future statements\n' '=================\n' '\n' 'A *future statement* is a directive to the compiler that a ' 'particular\n' 'module should be compiled using syntax or semantics that will be\n' 'available in a specified future release of Python. The future\n' 'statement is intended to ease migration to future versions of ' 'Python\n' 'that introduce incompatible changes to the language. It allows ' 'use of\n' 'the new features on a per-module basis before the release in which ' 'the\n' 'feature becomes standard.\n' '\n' ' future_statement ::= "from" "__future__" "import" feature ["as" ' 'name]\n' ' ("," feature ["as" name])*\n' ' | "from" "__future__" "import" "(" feature ' '["as" name]\n' ' ("," feature ["as" name])* [","] ")"\n' ' feature ::= identifier\n' ' name ::= identifier\n' '\n' 'A future statement must appear near the top of the module. The ' 'only\n' 'lines that can appear before a future statement are:\n' '\n' '* the module docstring (if any),\n' '\n' '* comments,\n' '\n' '* blank lines, and\n' '\n' '* other future statements.\n' '\n' 'The features recognized by Python 2.6 are "unicode_literals",\n' '"print_function", "absolute_import", "division", "generators",\n' '"nested_scopes" and "with_statement". "generators", ' '"with_statement",\n' '"nested_scopes" are redundant in Python version 2.6 and above ' 'because\n' 'they are always enabled.\n' '\n' 'A future statement is recognized and treated specially at compile\n' 'time: Changes to the semantics of core constructs are often\n' 'implemented by generating different code. It may even be the ' 'case\n' 'that a new feature introduces new incompatible syntax (such as a ' 'new\n' 'reserved word), in which case the compiler may need to parse the\n' 'module differently. Such decisions cannot be pushed off until\n' 'runtime.\n' '\n' 'For any given release, the compiler knows which feature names ' 'have\n' 'been defined, and raises a compile-time error if a future ' 'statement\n' 'contains a feature not known to it.\n' '\n' 'The direct runtime semantics are the same as for any import ' 'statement:\n' 'there is a standard module "__future__", described later, and it ' 'will\n' 'be imported in the usual way at the time the future statement is\n' 'executed.\n' '\n' 'The interesting runtime semantics depend on the specific feature\n' 'enabled by the future statement.\n' '\n' 'Note that there is nothing special about the statement:\n' '\n' ' import __future__ [as name]\n' '\n' "That is not a future statement; it's an ordinary import statement " 'with\n' 'no special semantics or syntax restrictions.\n' '\n' 'Code compiled by an "exec" statement or calls to the built-in\n' 'functions "compile()" and "execfile()" that occur in a module "M"\n' 'containing a future statement will, by default, use the new ' 'syntax or\n' 'semantics associated with the future statement. This can, ' 'starting\n' 'with Python 2.2 be controlled by optional arguments to "compile()" ' '---\n' 'see the documentation of that function for details.\n' '\n' 'A future statement typed at an interactive interpreter prompt ' 'will\n' 'take effect for the rest of the interpreter session. If an\n' 'interpreter is started with the "-i" option, is passed a script ' 'name\n' 'to execute, and the script includes a future statement, it will be ' 'in\n' 'effect in the interactive session started after the script is\n' 'executed.\n' '\n' 'See also:\n' '\n' ' **PEP 236** - Back to the __future__\n' ' The original proposal for the __future__ mechanism.\n', 'in': '\n' 'Membership test operations\n' '**************************\n' '\n' 'The operators "in" and "not in" test for membership. "x in s"\n' 'evaluates to "True" if *x* is a member of *s*, and "False" otherwise.\n' '"x not in s" returns the negation of "x in s". All built-in ' 'sequences\n' 'and set types support this as well as dictionary, for which "in" ' 'tests\n' 'whether the dictionary has a given key. For container types such as\n' 'list, tuple, set, frozenset, dict, or collections.deque, the\n' 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n' 'y)".\n' '\n' 'For the string and bytes types, "x in y" is "True" if and only if *x*\n' 'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n' 'Empty strings are always considered to be a substring of any other\n' 'string, so """ in "abc"" will return "True".\n' '\n' 'For user-defined classes which define the "__contains__()" method, "x\n' 'in y" returns "True" if "y.__contains__(x)" returns a true value, and\n' '"False" otherwise.\n' '\n' 'For user-defined classes which do not define "__contains__()" but do\n' 'define "__iter__()", "x in y" is "True" if some value "z" with "x ==\n' 'z" is produced while iterating over "y". If an exception is raised\n' 'during the iteration, it is as if "in" raised that exception.\n' '\n' 'Lastly, the old-style iteration protocol is tried: if a class defines\n' '"__getitem__()", "x in y" is "True" if and only if there is a non-\n' 'negative integer index *i* such that "x == y[i]", and all lower\n' 'integer indices do not raise "IndexError" exception. (If any other\n' 'exception is raised, it is as if "in" raised that exception).\n' '\n' 'The operator "not in" is defined to have the inverse true value of\n' '"in".\n', 'integers': '\n' 'Integer and long integer literals\n' '*********************************\n' '\n' 'Integer and long integer literals are described by the ' 'following\n' 'lexical definitions:\n' '\n' ' longinteger ::= integer ("l" | "L")\n' ' integer ::= decimalinteger | octinteger | hexinteger | ' 'bininteger\n' ' decimalinteger ::= nonzerodigit digit* | "0"\n' ' octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+\n' ' hexinteger ::= "0" ("x" | "X") hexdigit+\n' ' bininteger ::= "0" ("b" | "B") bindigit+\n' ' nonzerodigit ::= "1"..."9"\n' ' octdigit ::= "0"..."7"\n' ' bindigit ::= "0" | "1"\n' ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' '\n' 'Although both lower case "\'l\'" and upper case "\'L\'" are ' 'allowed as\n' 'suffix for long integers, it is strongly recommended to always ' 'use\n' '"\'L\'", since the letter "\'l\'" looks too much like the digit ' '"\'1\'".\n' '\n' 'Plain integer literals that are above the largest representable ' 'plain\n' 'integer (e.g., 2147483647 when using 32-bit arithmetic) are ' 'accepted\n' 'as if they were long integers instead. [1] There is no limit ' 'for long\n' 'integer literals apart from what can be stored in available ' 'memory.\n' '\n' 'Some examples of plain integer literals (first row) and long ' 'integer\n' 'literals (second and third rows):\n' '\n' ' 7 2147483647 0177\n' ' 3L 79228162514264337593543950336L 0377L 0x100000000L\n' ' 79228162514264337593543950336 0xdeadbeef\n', 'lambda': '\n' 'Lambdas\n' '*******\n' '\n' ' lambda_expr ::= "lambda" [parameter_list]: expression\n' ' old_lambda_expr ::= "lambda" [parameter_list]: old_expression\n' '\n' 'Lambda expressions (sometimes called lambda forms) have the same\n' 'syntactic position as expressions. They are a shorthand to ' 'create\n' 'anonymous functions; the expression "lambda arguments: ' 'expression"\n' 'yields a function object. The unnamed object behaves like a ' 'function\n' 'object defined with\n' '\n' ' def name(arguments):\n' ' return expression\n' '\n' 'See section Function definitions for the syntax of parameter ' 'lists.\n' 'Note that functions created with lambda expressions cannot ' 'contain\n' 'statements.\n', 'lists': '\n' 'List displays\n' '*************\n' '\n' 'A list display is a possibly empty series of expressions enclosed ' 'in\n' 'square brackets:\n' '\n' ' list_display ::= "[" [expression_list | ' 'list_comprehension] "]"\n' ' list_comprehension ::= expression list_for\n' ' list_for ::= "for" target_list "in" ' 'old_expression_list [list_iter]\n' ' old_expression_list ::= old_expression [("," old_expression)+ ' '[","]]\n' ' old_expression ::= or_test | old_lambda_expr\n' ' list_iter ::= list_for | list_if\n' ' list_if ::= "if" old_expression [list_iter]\n' '\n' 'A list display yields a new list object. Its contents are ' 'specified\n' 'by providing either a list of expressions or a list comprehension.\n' 'When a comma-separated list of expressions is supplied, its ' 'elements\n' 'are evaluated from left to right and placed into the list object ' 'in\n' 'that order. When a list comprehension is supplied, it consists of ' 'a\n' 'single expression followed by at least one "for" clause and zero ' 'or\n' 'more "for" or "if" clauses. In this case, the elements of the new\n' 'list are those that would be produced by considering each of the ' '"for"\n' 'or "if" clauses a block, nesting from left to right, and ' 'evaluating\n' 'the expression to produce a list element each time the innermost ' 'block\n' 'is reached [1].\n', 'naming': '\n' 'Naming and binding\n' '******************\n' '\n' '*Names* refer to objects. Names are introduced by name binding\n' 'operations. Each occurrence of a name in the program text refers ' 'to\n' 'the *binding* of that name established in the innermost function ' 'block\n' 'containing the use.\n' '\n' 'A *block* is a piece of Python program text that is executed as a\n' 'unit. The following are blocks: a module, a function body, and a ' 'class\n' 'definition. Each command typed interactively is a block. A ' 'script\n' 'file (a file given as standard input to the interpreter or ' 'specified\n' 'on the interpreter command line the first argument) is a code ' 'block.\n' 'A script command (a command specified on the interpreter command ' 'line\n' "with the '**-c**' option) is a code block. The file read by the\n" 'built-in function "execfile()" is a code block. The string ' 'argument\n' 'passed to the built-in function "eval()" and to the "exec" ' 'statement\n' 'is a code block. The expression read and evaluated by the ' 'built-in\n' 'function "input()" is a code block.\n' '\n' 'A code block is executed in an *execution frame*. A frame ' 'contains\n' 'some administrative information (used for debugging) and ' 'determines\n' "where and how execution continues after the code block's execution " 'has\n' 'completed.\n' '\n' 'A *scope* defines the visibility of a name within a block. If a ' 'local\n' 'variable is defined in a block, its scope includes that block. If ' 'the\n' 'definition occurs in a function block, the scope extends to any ' 'blocks\n' 'contained within the defining one, unless a contained block ' 'introduces\n' 'a different binding for the name. The scope of names defined in ' 'a\n' 'class block is limited to the class block; it does not extend to ' 'the\n' 'code blocks of methods -- this includes generator expressions ' 'since\n' 'they are implemented using a function scope. This means that the\n' 'following will fail:\n' '\n' ' class A:\n' ' a = 42\n' ' b = list(a + i for i in range(10))\n' '\n' 'When a name is used in a code block, it is resolved using the ' 'nearest\n' 'enclosing scope. The set of all such scopes visible to a code ' 'block\n' "is called the block's *environment*.\n" '\n' 'If a name is bound in a block, it is a local variable of that ' 'block.\n' 'If a name is bound at the module level, it is a global variable. ' '(The\n' 'variables of the module code block are local and global.) If a\n' 'variable is used in a code block but not defined there, it is a ' '*free\n' 'variable*.\n' '\n' 'When a name is not found at all, a "NameError" exception is ' 'raised.\n' 'If the name refers to a local variable that has not been bound, a\n' '"UnboundLocalError" exception is raised. "UnboundLocalError" is ' 'a\n' 'subclass of "NameError".\n' '\n' 'The following constructs bind names: formal parameters to ' 'functions,\n' '"import" statements, class and function definitions (these bind ' 'the\n' 'class or function name in the defining block), and targets that ' 'are\n' 'identifiers if occurring in an assignment, "for" loop header, in ' 'the\n' 'second position of an "except" clause header or after "as" in a ' '"with"\n' 'statement. The "import" statement of the form "from ... import ' '*"\n' 'binds all names defined in the imported module, except those ' 'beginning\n' 'with an underscore. This form may only be used at the module ' 'level.\n' '\n' 'A target occurring in a "del" statement is also considered bound ' 'for\n' 'this purpose (though the actual semantics are to unbind the ' 'name). It\n' 'is illegal to unbind a name that is referenced by an enclosing ' 'scope;\n' 'the compiler will report a "SyntaxError".\n' '\n' 'Each assignment or import statement occurs within a block defined ' 'by a\n' 'class or function definition or at the module level (the ' 'top-level\n' 'code block).\n' '\n' 'If a name binding operation occurs anywhere within a code block, ' 'all\n' 'uses of the name within the block are treated as references to ' 'the\n' 'current block. This can lead to errors when a name is used within ' 'a\n' 'block before it is bound. This rule is subtle. Python lacks\n' 'declarations and allows name binding operations to occur anywhere\n' 'within a code block. The local variables of a code block can be\n' 'determined by scanning the entire text of the block for name ' 'binding\n' 'operations.\n' '\n' 'If the global statement occurs within a block, all uses of the ' 'name\n' 'specified in the statement refer to the binding of that name in ' 'the\n' 'top-level namespace. Names are resolved in the top-level namespace ' 'by\n' 'searching the global namespace, i.e. the namespace of the module\n' 'containing the code block, and the builtins namespace, the ' 'namespace\n' 'of the module "__builtin__". The global namespace is searched ' 'first.\n' 'If the name is not found there, the builtins namespace is ' 'searched.\n' 'The global statement must precede all uses of the name.\n' '\n' 'The builtins namespace associated with the execution of a code ' 'block\n' 'is actually found by looking up the name "__builtins__" in its ' 'global\n' 'namespace; this should be a dictionary or a module (in the latter ' 'case\n' "the module's dictionary is used). By default, when in the " '"__main__"\n' 'module, "__builtins__" is the built-in module "__builtin__" (note: ' 'no\n' '\'s\'); when in any other module, "__builtins__" is an alias for ' 'the\n' 'dictionary of the "__builtin__" module itself. "__builtins__" can ' 'be\n' 'set to a user-created dictionary to create a weak form of ' 'restricted\n' 'execution.\n' '\n' '**CPython implementation detail:** Users should not touch\n' '"__builtins__"; it is strictly an implementation detail. Users\n' 'wanting to override values in the builtins namespace should ' '"import"\n' 'the "__builtin__" (no \'s\') module and modify its attributes\n' 'appropriately.\n' '\n' 'The namespace for a module is automatically created the first time ' 'a\n' 'module is imported. The main module for a script is always ' 'called\n' '"__main__".\n' '\n' 'The "global" statement has the same scope as a name binding ' 'operation\n' 'in the same block. If the nearest enclosing scope for a free ' 'variable\n' 'contains a global statement, the free variable is treated as a ' 'global.\n' '\n' 'A class definition is an executable statement that may use and ' 'define\n' 'names. These references follow the normal rules for name ' 'resolution.\n' 'The namespace of the class definition becomes the attribute ' 'dictionary\n' 'of the class. Names defined at the class scope are not visible ' 'in\n' 'methods.\n' '\n' '\n' 'Interaction with dynamic features\n' '=================================\n' '\n' 'There are several cases where Python statements are illegal when ' 'used\n' 'in conjunction with nested scopes that contain free variables.\n' '\n' 'If a variable is referenced in an enclosing scope, it is illegal ' 'to\n' 'delete the name. An error will be reported at compile time.\n' '\n' 'If the wild card form of import --- "import *" --- is used in a\n' 'function and the function contains or is a nested block with free\n' 'variables, the compiler will raise a "SyntaxError".\n' '\n' 'If "exec" is used in a function and the function contains or is a\n' 'nested block with free variables, the compiler will raise a\n' '"SyntaxError" unless the exec explicitly specifies the local ' 'namespace\n' 'for the "exec". (In other words, "exec obj" would be illegal, ' 'but\n' '"exec obj in ns" would be legal.)\n' '\n' 'The "eval()", "execfile()", and "input()" functions and the ' '"exec"\n' 'statement do not have access to the full environment for ' 'resolving\n' 'names. Names may be resolved in the local and global namespaces ' 'of\n' 'the caller. Free variables are not resolved in the nearest ' 'enclosing\n' 'namespace, but in the global namespace. [1] The "exec" statement ' 'and\n' 'the "eval()" and "execfile()" functions have optional arguments ' 'to\n' 'override the global and local namespace. If only one namespace ' 'is\n' 'specified, it is used for both.\n', 'numbers': '\n' 'Numeric literals\n' '****************\n' '\n' 'There are four types of numeric literals: plain integers, long\n' 'integers, floating point numbers, and imaginary numbers. There ' 'are no\n' 'complex literals (complex numbers can be formed by adding a real\n' 'number and an imaginary number).\n' '\n' 'Note that numeric literals do not include a sign; a phrase like ' '"-1"\n' 'is actually an expression composed of the unary operator \'"-"\' ' 'and the\n' 'literal "1".\n', 'numeric-types': '\n' 'Emulating numeric types\n' '***********************\n' '\n' 'The following methods can be defined to emulate numeric ' 'objects.\n' 'Methods corresponding to operations that are not supported ' 'by the\n' 'particular kind of number implemented (e.g., bitwise ' 'operations for\n' 'non-integral numbers) should be left undefined.\n' '\n' 'object.__add__(self, other)\n' 'object.__sub__(self, other)\n' 'object.__mul__(self, other)\n' 'object.__floordiv__(self, other)\n' 'object.__mod__(self, other)\n' 'object.__divmod__(self, other)\n' 'object.__pow__(self, other[, modulo])\n' 'object.__lshift__(self, other)\n' 'object.__rshift__(self, other)\n' 'object.__and__(self, other)\n' 'object.__xor__(self, other)\n' 'object.__or__(self, other)\n' '\n' ' These methods are called to implement the binary ' 'arithmetic\n' ' operations ("+", "-", "*", "//", "%", "divmod()", ' '"pow()", "**",\n' ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' 'the\n' ' expression "x + y", where *x* is an instance of a class ' 'that has an\n' ' "__add__()" method, "x.__add__(y)" is called. The ' '"__divmod__()"\n' ' method should be the equivalent to using ' '"__floordiv__()" and\n' ' "__mod__()"; it should not be related to "__truediv__()" ' '(described\n' ' below). Note that "__pow__()" should be defined to ' 'accept an\n' ' optional third argument if the ternary version of the ' 'built-in\n' ' "pow()" function is to be supported.\n' '\n' ' If one of those methods does not support the operation ' 'with the\n' ' supplied arguments, it should return "NotImplemented".\n' '\n' 'object.__div__(self, other)\n' 'object.__truediv__(self, other)\n' '\n' ' The division operator ("/") is implemented by these ' 'methods. The\n' ' "__truediv__()" method is used when ' '"__future__.division" is in\n' ' effect, otherwise "__div__()" is used. If only one of ' 'these two\n' ' methods is defined, the object will not support division ' 'in the\n' ' alternate context; "TypeError" will be raised instead.\n' '\n' 'object.__radd__(self, other)\n' 'object.__rsub__(self, other)\n' 'object.__rmul__(self, other)\n' 'object.__rdiv__(self, other)\n' 'object.__rtruediv__(self, other)\n' 'object.__rfloordiv__(self, other)\n' 'object.__rmod__(self, other)\n' 'object.__rdivmod__(self, other)\n' 'object.__rpow__(self, other)\n' 'object.__rlshift__(self, other)\n' 'object.__rrshift__(self, other)\n' 'object.__rand__(self, other)\n' 'object.__rxor__(self, other)\n' 'object.__ror__(self, other)\n' '\n' ' These methods are called to implement the binary ' 'arithmetic\n' ' operations ("+", "-", "*", "/", "%", "divmod()", ' '"pow()", "**",\n' ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' 'operands.\n' ' These functions are only called if the left operand does ' 'not\n' ' support the corresponding operation and the operands are ' 'of\n' ' different types. [2] For instance, to evaluate the ' 'expression "x -\n' ' y", where *y* is an instance of a class that has an ' '"__rsub__()"\n' ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' 'returns\n' ' *NotImplemented*.\n' '\n' ' Note that ternary "pow()" will not try calling ' '"__rpow__()" (the\n' ' coercion rules would become too complicated).\n' '\n' " Note: If the right operand's type is a subclass of the " 'left\n' " operand's type and that subclass provides the " 'reflected method\n' ' for the operation, this method will be called before ' 'the left\n' " operand's non-reflected method. This behavior allows " 'subclasses\n' " to override their ancestors' operations.\n" '\n' 'object.__iadd__(self, other)\n' 'object.__isub__(self, other)\n' 'object.__imul__(self, other)\n' 'object.__idiv__(self, other)\n' 'object.__itruediv__(self, other)\n' 'object.__ifloordiv__(self, other)\n' 'object.__imod__(self, other)\n' 'object.__ipow__(self, other[, modulo])\n' 'object.__ilshift__(self, other)\n' 'object.__irshift__(self, other)\n' 'object.__iand__(self, other)\n' 'object.__ixor__(self, other)\n' 'object.__ior__(self, other)\n' '\n' ' These methods are called to implement the augmented ' 'arithmetic\n' ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", ' '"<<=",\n' ' ">>=", "&=", "^=", "|="). These methods should attempt ' 'to do the\n' ' operation in-place (modifying *self*) and return the ' 'result (which\n' ' could be, but does not have to be, *self*). If a ' 'specific method\n' ' is not defined, the augmented assignment falls back to ' 'the normal\n' ' methods. For instance, to execute the statement "x += ' 'y", where\n' ' *x* is an instance of a class that has an "__iadd__()" ' 'method,\n' ' "x.__iadd__(y)" is called. If *x* is an instance of a ' 'class that\n' ' does not define a "__iadd__()" method, "x.__add__(y)" ' 'and\n' ' "y.__radd__(x)" are considered, as with the evaluation ' 'of "x + y".\n' '\n' 'object.__neg__(self)\n' 'object.__pos__(self)\n' 'object.__abs__(self)\n' 'object.__invert__(self)\n' '\n' ' Called to implement the unary arithmetic operations ' '("-", "+",\n' ' "abs()" and "~").\n' '\n' 'object.__complex__(self)\n' 'object.__int__(self)\n' 'object.__long__(self)\n' 'object.__float__(self)\n' '\n' ' Called to implement the built-in functions "complex()", ' '"int()",\n' ' "long()", and "float()". Should return a value of the ' 'appropriate\n' ' type.\n' '\n' 'object.__oct__(self)\n' 'object.__hex__(self)\n' '\n' ' Called to implement the built-in functions "oct()" and ' '"hex()".\n' ' Should return a string value.\n' '\n' 'object.__index__(self)\n' '\n' ' Called to implement "operator.index()". Also called ' 'whenever\n' ' Python needs an integer object (such as in slicing). ' 'Must return\n' ' an integer (int or long).\n' '\n' ' New in version 2.5.\n' '\n' 'object.__coerce__(self, other)\n' '\n' ' Called to implement "mixed-mode" numeric arithmetic. ' 'Should either\n' ' return a 2-tuple containing *self* and *other* converted ' 'to a\n' ' common numeric type, or "None" if conversion is ' 'impossible. When\n' ' the common type would be the type of "other", it is ' 'sufficient to\n' ' return "None", since the interpreter will also ask the ' 'other object\n' ' to attempt a coercion (but sometimes, if the ' 'implementation of the\n' ' other type cannot be changed, it is useful to do the ' 'conversion to\n' ' the other type here). A return value of ' '"NotImplemented" is\n' ' equivalent to returning "None".\n', 'objects': '\n' 'Objects, values and types\n' '*************************\n' '\n' "*Objects* are Python's abstraction for data. All data in a " 'Python\n' 'program is represented by objects or by relations between ' 'objects. (In\n' 'a sense, and in conformance to Von Neumann\'s model of a "stored\n' 'program computer," code is also represented by objects.)\n' '\n' "Every object has an identity, a type and a value. An object's\n" '*identity* never changes once it has been created; you may think ' 'of it\n' 'as the object\'s address in memory. The \'"is"\' operator ' 'compares the\n' 'identity of two objects; the "id()" function returns an integer\n' 'representing its identity (currently implemented as its address). ' 'An\n' "object's *type* is also unchangeable. [1] An object's type " 'determines\n' 'the operations that the object supports (e.g., "does it have a\n' 'length?") and also defines the possible values for objects of ' 'that\n' 'type. The "type()" function returns an object\'s type (which is ' 'an\n' 'object itself). The *value* of some objects can change. ' 'Objects\n' 'whose value can change are said to be *mutable*; objects whose ' 'value\n' 'is unchangeable once they are created are called *immutable*. ' '(The\n' 'value of an immutable container object that contains a reference ' 'to a\n' "mutable object can change when the latter's value is changed; " 'however\n' 'the container is still considered immutable, because the ' 'collection of\n' 'objects it contains cannot be changed. So, immutability is not\n' 'strictly the same as having an unchangeable value, it is more ' 'subtle.)\n' "An object's mutability is determined by its type; for instance,\n" 'numbers, strings and tuples are immutable, while dictionaries ' 'and\n' 'lists are mutable.\n' '\n' 'Objects are never explicitly destroyed; however, when they ' 'become\n' 'unreachable they may be garbage-collected. An implementation is\n' 'allowed to postpone garbage collection or omit it altogether --- ' 'it is\n' 'a matter of implementation quality how garbage collection is\n' 'implemented, as long as no objects are collected that are still\n' 'reachable.\n' '\n' '**CPython implementation detail:** CPython currently uses a ' 'reference-\n' 'counting scheme with (optional) delayed detection of cyclically ' 'linked\n' 'garbage, which collects most objects as soon as they become\n' 'unreachable, but is not guaranteed to collect garbage containing\n' 'circular references. See the documentation of the "gc" module ' 'for\n' 'information on controlling the collection of cyclic garbage. ' 'Other\n' 'implementations act differently and CPython may change. Do not ' 'depend\n' 'on immediate finalization of objects when they become unreachable ' '(ex:\n' 'always close files).\n' '\n' "Note that the use of the implementation's tracing or debugging\n" 'facilities may keep objects alive that would normally be ' 'collectable.\n' 'Also note that catching an exception with a \'"try"..."except"\'\n' 'statement may keep objects alive.\n' '\n' 'Some objects contain references to "external" resources such as ' 'open\n' 'files or windows. It is understood that these resources are ' 'freed\n' 'when the object is garbage-collected, but since garbage ' 'collection is\n' 'not guaranteed to happen, such objects also provide an explicit ' 'way to\n' 'release the external resource, usually a "close()" method. ' 'Programs\n' 'are strongly recommended to explicitly close such objects. The\n' '\'"try"..."finally"\' statement provides a convenient way to do ' 'this.\n' '\n' 'Some objects contain references to other objects; these are ' 'called\n' '*containers*. Examples of containers are tuples, lists and\n' "dictionaries. The references are part of a container's value. " 'In\n' 'most cases, when we talk about the value of a container, we imply ' 'the\n' 'values, not the identities of the contained objects; however, ' 'when we\n' 'talk about the mutability of a container, only the identities of ' 'the\n' 'immediately contained objects are implied. So, if an immutable\n' 'container (like a tuple) contains a reference to a mutable ' 'object, its\n' 'value changes if that mutable object is changed.\n' '\n' 'Types affect almost all aspects of object behavior. Even the\n' 'importance of object identity is affected in some sense: for ' 'immutable\n' 'types, operations that compute new values may actually return a\n' 'reference to any existing object with the same type and value, ' 'while\n' 'for mutable objects this is not allowed. E.g., after "a = 1; b = ' '1",\n' '"a" and "b" may or may not refer to the same object with the ' 'value\n' 'one, depending on the implementation, but after "c = []; d = []", ' '"c"\n' 'and "d" are guaranteed to refer to two different, unique, newly\n' 'created empty lists. (Note that "c = d = []" assigns the same ' 'object\n' 'to both "c" and "d".)\n', 'operator-summary': '\n' 'Operator precedence\n' '*******************\n' '\n' 'The following table summarizes the operator precedences ' 'in Python,\n' 'from lowest precedence (least binding) to highest ' 'precedence (most\n' 'binding). Operators in the same box have the same ' 'precedence. Unless\n' 'the syntax is explicitly given, operators are binary. ' 'Operators in\n' 'the same box group left to right (except for ' 'comparisons, including\n' 'tests, which all have the same precedence and chain from ' 'left to right\n' '--- see section Comparisons --- and exponentiation, ' 'which groups from\n' 'right to left).\n' '\n' '+-------------------------------------------------+---------------------------------------+\n' '| Operator | ' 'Description |\n' '+=================================================+=======================================+\n' '| "lambda" | ' 'Lambda expression |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "if" -- "else" | ' 'Conditional expression |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "or" | ' 'Boolean OR |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "and" | ' 'Boolean AND |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "not" "x" | ' 'Boolean NOT |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "in", "not in", "is", "is not", "<", "<=", ">", | ' 'Comparisons, including membership |\n' '| ">=", "<>", "!=", "==" | ' 'tests and identity tests |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "|" | ' 'Bitwise OR |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "^" | ' 'Bitwise XOR |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "&" | ' 'Bitwise AND |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "<<", ">>" | ' 'Shifts |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "+", "-" | ' 'Addition and subtraction |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "*", "/", "//", "%" | ' 'Multiplication, division, remainder |\n' '| | ' '[7] |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "+x", "-x", "~x" | ' 'Positive, negative, bitwise NOT |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "**" | ' 'Exponentiation [8] |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "x[index]", "x[index:index]", | ' 'Subscription, slicing, call, |\n' '| "x(arguments...)", "x.attribute" | ' 'attribute reference |\n' '+-------------------------------------------------+---------------------------------------+\n' '| "(expressions...)", "[expressions...]", "{key: | ' 'Binding or tuple display, list |\n' '| value...}", "`expressions...`" | ' 'display, dictionary display, string |\n' '| | ' 'conversion |\n' '+-------------------------------------------------+---------------------------------------+\n' '\n' '-[ Footnotes ]-\n' '\n' '[1] In Python 2.3 and later releases, a list ' 'comprehension "leaks"\n' ' the control variables of each "for" it contains into ' 'the\n' ' containing scope. However, this behavior is ' 'deprecated, and\n' ' relying on it will not work in Python 3.\n' '\n' '[2] While "abs(x%y) < abs(y)" is true mathematically, ' 'for floats\n' ' it may not be true numerically due to roundoff. For ' 'example, and\n' ' assuming a platform on which a Python float is an ' 'IEEE 754 double-\n' ' precision number, in order that "-1e-100 % 1e100" ' 'have the same\n' ' sign as "1e100", the computed result is "-1e-100 + ' '1e100", which\n' ' is numerically exactly equal to "1e100". The ' 'function\n' ' "math.fmod()" returns a result whose sign matches ' 'the sign of the\n' ' first argument instead, and so returns "-1e-100" in ' 'this case.\n' ' Which approach is more appropriate depends on the ' 'application.\n' '\n' '[3] If x is very close to an exact integer multiple of ' "y, it's\n" ' possible for "floor(x/y)" to be one larger than ' '"(x-x%y)/y" due to\n' ' rounding. In such cases, Python returns the latter ' 'result, in\n' ' order to preserve that "divmod(x,y)[0] * y + x % y" ' 'be very close\n' ' to "x".\n' '\n' '[4] The Unicode standard distinguishes between *code ' 'points* (e.g.\n' ' U+0041) and *abstract characters* (e.g. "LATIN ' 'CAPITAL LETTER A").\n' ' While most abstract characters in Unicode are only ' 'represented\n' ' using one code point, there is a number of abstract ' 'characters\n' ' that can in addition be represented using a sequence ' 'of more than\n' ' one code point. For example, the abstract character ' '"LATIN\n' ' CAPITAL LETTER C WITH CEDILLA" can be represented as ' 'a single\n' ' *precomposed character* at code position U+00C7, or ' 'as a sequence\n' ' of a *base character* at code position U+0043 (LATIN ' 'CAPITAL\n' ' LETTER C), followed by a *combining character* at ' 'code position\n' ' U+0327 (COMBINING CEDILLA).\n' '\n' ' The comparison operators on unicode strings compare ' 'at the level\n' ' of Unicode code points. This may be ' 'counter-intuitive to humans.\n' ' For example, "u"\\u00C7" == u"\\u0043\\u0327"" is ' '"False", even\n' ' though both strings represent the same abstract ' 'character "LATIN\n' ' CAPITAL LETTER C WITH CEDILLA".\n' '\n' ' To compare strings at the level of abstract ' 'characters (that is,\n' ' in a way intuitive to humans), use ' '"unicodedata.normalize()".\n' '\n' '[5] Earlier versions of Python used lexicographic ' 'comparison of\n' ' the sorted (key, value) lists, but this was very ' 'expensive for the\n' ' common case of comparing for equality. An even ' 'earlier version of\n' ' Python compared dictionaries by identity only, but ' 'this caused\n' ' surprises because people expected to be able to test ' 'a dictionary\n' ' for emptiness by comparing it to "{}".\n' '\n' '[6] Due to automatic garbage-collection, free lists, and ' 'the\n' ' dynamic nature of descriptors, you may notice ' 'seemingly unusual\n' ' behaviour in certain uses of the "is" operator, like ' 'those\n' ' involving comparisons between instance methods, or ' 'constants.\n' ' Check their documentation for more info.\n' '\n' '[7] The "%" operator is also used for string formatting; ' 'the same\n' ' precedence applies.\n' '\n' '[8] The power operator "**" binds less tightly than an ' 'arithmetic\n' ' or bitwise unary operator on its right, that is, ' '"2**-1" is "0.5".\n', 'pass': '\n' 'The "pass" statement\n' '********************\n' '\n' ' pass_stmt ::= "pass"\n' '\n' '"pass" is a null operation --- when it is executed, nothing ' 'happens.\n' 'It is useful as a placeholder when a statement is required\n' 'syntactically, but no code needs to be executed, for example:\n' '\n' ' def f(arg): pass # a function that does nothing (yet)\n' '\n' ' class C: pass # a class with no methods (yet)\n', 'power': '\n' 'The power operator\n' '******************\n' '\n' 'The power operator binds more tightly than unary operators on its\n' 'left; it binds less tightly than unary operators on its right. ' 'The\n' 'syntax is:\n' '\n' ' power ::= primary ["**" u_expr]\n' '\n' 'Thus, in an unparenthesized sequence of power and unary operators, ' 'the\n' 'operators are evaluated from right to left (this does not ' 'constrain\n' 'the evaluation order for the operands): "-1**2" results in "-1".\n' '\n' 'The power operator has the same semantics as the built-in "pow()"\n' 'function, when called with two arguments: it yields its left ' 'argument\n' 'raised to the power of its right argument. The numeric arguments ' 'are\n' 'first converted to a common type. The result type is that of the\n' 'arguments after coercion.\n' '\n' 'With mixed operand types, the coercion rules for binary arithmetic\n' 'operators apply. For int and long int operands, the result has the\n' 'same type as the operands (after coercion) unless the second ' 'argument\n' 'is negative; in that case, all arguments are converted to float and ' 'a\n' 'float result is delivered. For example, "10**2" returns "100", but\n' '"10**-2" returns "0.01". (This last feature was added in Python ' '2.2.\n' 'In Python 2.1 and before, if both arguments were of integer types ' 'and\n' 'the second argument was negative, an exception was raised).\n' '\n' 'Raising "0.0" to a negative power results in a ' '"ZeroDivisionError".\n' 'Raising a negative number to a fractional power results in a\n' '"ValueError".\n', 'print': '\n' 'The "print" statement\n' '*********************\n' '\n' ' print_stmt ::= "print" ([expression ("," expression)* [","]]\n' ' | ">>" expression [("," expression)+ [","]])\n' '\n' '"print" evaluates each expression in turn and writes the resulting\n' 'object to standard output (see below). If an object is not a ' 'string,\n' 'it is first converted to a string using the rules for string\n' 'conversions. The (resulting or original) string is then written. ' 'A\n' 'space is written before each object is (converted and) written, ' 'unless\n' 'the output system believes it is positioned at the beginning of a\n' 'line. This is the case (1) when no characters have yet been ' 'written\n' 'to standard output, (2) when the last character written to ' 'standard\n' 'output is a whitespace character except "\' \'", or (3) when the ' 'last\n' 'write operation on standard output was not a "print" statement. ' '(In\n' 'some cases it may be functional to write an empty string to ' 'standard\n' 'output for this reason.)\n' '\n' 'Note: Objects which act like file objects but which are not the\n' ' built-in file objects often do not properly emulate this aspect ' 'of\n' " the file object's behavior, so it is best not to rely on this.\n" '\n' 'A "\'\\n\'" character is written at the end, unless the "print" ' 'statement\n' 'ends with a comma. This is the only action if the statement ' 'contains\n' 'just the keyword "print".\n' '\n' 'Standard output is defined as the file object named "stdout" in ' 'the\n' 'built-in module "sys". If no such object exists, or if it does ' 'not\n' 'have a "write()" method, a "RuntimeError" exception is raised.\n' '\n' '"print" also has an extended form, defined by the second portion ' 'of\n' 'the syntax described above. This form is sometimes referred to as\n' '""print" chevron." In this form, the first expression after the ' '">>"\n' 'must evaluate to a "file-like" object, specifically an object that ' 'has\n' 'a "write()" method as described above. With this extended form, ' 'the\n' 'subsequent expressions are printed to this file object. If the ' 'first\n' 'expression evaluates to "None", then "sys.stdout" is used as the ' 'file\n' 'for output.\n', 'raise': '\n' 'The "raise" statement\n' '*********************\n' '\n' ' raise_stmt ::= "raise" [expression ["," expression ["," ' 'expression]]]\n' '\n' 'If no expressions are present, "raise" re-raises the last ' 'exception\n' 'that was active in the current scope. If no exception is active ' 'in\n' 'the current scope, a "TypeError" exception is raised indicating ' 'that\n' 'this is an error (if running under IDLE, a "Queue.Empty" exception ' 'is\n' 'raised instead).\n' '\n' 'Otherwise, "raise" evaluates the expressions to get three objects,\n' 'using "None" as the value of omitted expressions. The first two\n' 'objects are used to determine the *type* and *value* of the ' 'exception.\n' '\n' 'If the first object is an instance, the type of the exception is ' 'the\n' 'class of the instance, the instance itself is the value, and the\n' 'second object must be "None".\n' '\n' 'If the first object is a class, it becomes the type of the ' 'exception.\n' 'The second object is used to determine the exception value: If it ' 'is\n' 'an instance of the class, the instance becomes the exception value. ' 'If\n' 'the second object is a tuple, it is used as the argument list for ' 'the\n' 'class constructor; if it is "None", an empty argument list is ' 'used,\n' 'and any other object is treated as a single argument to the\n' 'constructor. The instance so created by calling the constructor ' 'is\n' 'used as the exception value.\n' '\n' 'If a third object is present and not "None", it must be a ' 'traceback\n' 'object (see section The standard type hierarchy), and it is\n' 'substituted instead of the current location as the place where the\n' 'exception occurred. If the third object is present and not a\n' 'traceback object or "None", a "TypeError" exception is raised. ' 'The\n' 'three-expression form of "raise" is useful to re-raise an ' 'exception\n' 'transparently in an except clause, but "raise" with no expressions\n' 'should be preferred if the exception to be re-raised was the most\n' 'recently active exception in the current scope.\n' '\n' 'Additional information on exceptions can be found in section\n' 'Exceptions, and information about handling exceptions is in ' 'section\n' 'The try statement.\n', 'return': '\n' 'The "return" statement\n' '**********************\n' '\n' ' return_stmt ::= "return" [expression_list]\n' '\n' '"return" may only occur syntactically nested in a function ' 'definition,\n' 'not within a nested class definition.\n' '\n' 'If an expression list is present, it is evaluated, else "None" is\n' 'substituted.\n' '\n' '"return" leaves the current function call with the expression list ' '(or\n' '"None") as return value.\n' '\n' 'When "return" passes control out of a "try" statement with a ' '"finally"\n' 'clause, that "finally" clause is executed before really leaving ' 'the\n' 'function.\n' '\n' 'In a generator function, the "return" statement is not allowed to\n' 'include an "expression_list". In that context, a bare "return"\n' 'indicates that the generator is done and will cause ' '"StopIteration" to\n' 'be raised.\n', 'sequence-types': '\n' 'Emulating container types\n' '*************************\n' '\n' 'The following methods can be defined to implement ' 'container objects.\n' 'Containers usually are sequences (such as lists or tuples) ' 'or mappings\n' '(like dictionaries), but can represent other containers as ' 'well. The\n' 'first set of methods is used either to emulate a sequence ' 'or to\n' 'emulate a mapping; the difference is that for a sequence, ' 'the\n' 'allowable keys should be the integers *k* for which "0 <= ' 'k < N" where\n' '*N* is the length of the sequence, or slice objects, which ' 'define a\n' 'range of items. (For backwards compatibility, the method\n' '"__getslice__()" (see below) can also be defined to handle ' 'simple, but\n' 'not extended slices.) It is also recommended that mappings ' 'provide the\n' 'methods "keys()", "values()", "items()", "has_key()", ' '"get()",\n' '"clear()", "setdefault()", "iterkeys()", "itervalues()",\n' '"iteritems()", "pop()", "popitem()", "copy()", and ' '"update()" behaving\n' "similar to those for Python's standard dictionary " 'objects. The\n' '"UserDict" module provides a "DictMixin" class to help ' 'create those\n' 'methods from a base set of "__getitem__()", ' '"__setitem__()",\n' '"__delitem__()", and "keys()". Mutable sequences should ' 'provide\n' 'methods "append()", "count()", "index()", "extend()", ' '"insert()",\n' '"pop()", "remove()", "reverse()" and "sort()", like Python ' 'standard\n' 'list objects. Finally, sequence types should implement ' 'addition\n' '(meaning concatenation) and multiplication (meaning ' 'repetition) by\n' 'defining the methods "__add__()", "__radd__()", ' '"__iadd__()",\n' '"__mul__()", "__rmul__()" and "__imul__()" described ' 'below; they\n' 'should not define "__coerce__()" or other numerical ' 'operators. It is\n' 'recommended that both mappings and sequences implement ' 'the\n' '"__contains__()" method to allow efficient use of the "in" ' 'operator;\n' 'for mappings, "in" should be equivalent of "has_key()"; ' 'for sequences,\n' 'it should search through the values. It is further ' 'recommended that\n' 'both mappings and sequences implement the "__iter__()" ' 'method to allow\n' 'efficient iteration through the container; for mappings, ' '"__iter__()"\n' 'should be the same as "iterkeys()"; for sequences, it ' 'should iterate\n' 'through the values.\n' '\n' 'object.__len__(self)\n' '\n' ' Called to implement the built-in function "len()". ' 'Should return\n' ' the length of the object, an integer ">=" 0. Also, an ' 'object that\n' ' doesn\'t define a "__nonzero__()" method and whose ' '"__len__()"\n' ' method returns zero is considered to be false in a ' 'Boolean context.\n' '\n' ' **CPython implementation detail:** In CPython, the ' 'length is\n' ' required to be at most "sys.maxsize". If the length is ' 'larger than\n' ' "sys.maxsize" some features (such as "len()") may ' 'raise\n' ' "OverflowError". To prevent raising "OverflowError" by ' 'truth value\n' ' testing, an object must define a "__nonzero__()" ' 'method.\n' '\n' 'object.__getitem__(self, key)\n' '\n' ' Called to implement evaluation of "self[key]". For ' 'sequence types,\n' ' the accepted keys should be integers and slice ' 'objects. Note that\n' ' the special interpretation of negative indexes (if the ' 'class wishes\n' ' to emulate a sequence type) is up to the ' '"__getitem__()" method. If\n' ' *key* is of an inappropriate type, "TypeError" may be ' 'raised; if of\n' ' a value outside the set of indexes for the sequence ' '(after any\n' ' special interpretation of negative values), ' '"IndexError" should be\n' ' raised. For mapping types, if *key* is missing (not in ' 'the\n' ' container), "KeyError" should be raised.\n' '\n' ' Note: "for" loops expect that an "IndexError" will be ' 'raised for\n' ' illegal indexes to allow proper detection of the end ' 'of the\n' ' sequence.\n' '\n' 'object.__missing__(self, key)\n' '\n' ' Called by "dict"."__getitem__()" to implement ' '"self[key]" for dict\n' ' subclasses when key is not in the dictionary.\n' '\n' 'object.__setitem__(self, key, value)\n' '\n' ' Called to implement assignment to "self[key]". Same ' 'note as for\n' ' "__getitem__()". This should only be implemented for ' 'mappings if\n' ' the objects support changes to the values for keys, or ' 'if new keys\n' ' can be added, or for sequences if elements can be ' 'replaced. The\n' ' same exceptions should be raised for improper *key* ' 'values as for\n' ' the "__getitem__()" method.\n' '\n' 'object.__delitem__(self, key)\n' '\n' ' Called to implement deletion of "self[key]". Same note ' 'as for\n' ' "__getitem__()". This should only be implemented for ' 'mappings if\n' ' the objects support removal of keys, or for sequences ' 'if elements\n' ' can be removed from the sequence. The same exceptions ' 'should be\n' ' raised for improper *key* values as for the ' '"__getitem__()" method.\n' '\n' 'object.__iter__(self)\n' '\n' ' This method is called when an iterator is required for ' 'a container.\n' ' This method should return a new iterator object that ' 'can iterate\n' ' over all the objects in the container. For mappings, ' 'it should\n' ' iterate over the keys of the container, and should also ' 'be made\n' ' available as the method "iterkeys()".\n' '\n' ' Iterator objects also need to implement this method; ' 'they are\n' ' required to return themselves. For more information on ' 'iterator\n' ' objects, see Iterator Types.\n' '\n' 'object.__reversed__(self)\n' '\n' ' Called (if present) by the "reversed()" built-in to ' 'implement\n' ' reverse iteration. It should return a new iterator ' 'object that\n' ' iterates over all the objects in the container in ' 'reverse order.\n' '\n' ' If the "__reversed__()" method is not provided, the ' '"reversed()"\n' ' built-in will fall back to using the sequence protocol ' '("__len__()"\n' ' and "__getitem__()"). Objects that support the ' 'sequence protocol\n' ' should only provide "__reversed__()" if they can ' 'provide an\n' ' implementation that is more efficient than the one ' 'provided by\n' ' "reversed()".\n' '\n' ' New in version 2.6.\n' '\n' 'The membership test operators ("in" and "not in") are ' 'normally\n' 'implemented as an iteration through a sequence. However, ' 'container\n' 'objects can supply the following special method with a ' 'more efficient\n' 'implementation, which also does not require the object be ' 'a sequence.\n' '\n' 'object.__contains__(self, item)\n' '\n' ' Called to implement membership test operators. Should ' 'return true\n' ' if *item* is in *self*, false otherwise. For mapping ' 'objects, this\n' ' should consider the keys of the mapping rather than the ' 'values or\n' ' the key-item pairs.\n' '\n' ' For objects that don\'t define "__contains__()", the ' 'membership test\n' ' first tries iteration via "__iter__()", then the old ' 'sequence\n' ' iteration protocol via "__getitem__()", see this ' 'section in the\n' ' language reference.\n', 'shifting': '\n' 'Shifting operations\n' '*******************\n' '\n' 'The shifting operations have lower priority than the arithmetic\n' 'operations:\n' '\n' ' shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n' '\n' 'These operators accept plain or long integers as arguments. ' 'The\n' 'arguments are converted to a common type. They shift the first\n' 'argument to the left or right by the number of bits given by ' 'the\n' 'second argument.\n' '\n' 'A right shift by *n* bits is defined as division by "pow(2, ' 'n)". A\n' 'left shift by *n* bits is defined as multiplication with "pow(2, ' 'n)".\n' 'Negative shift counts raise a "ValueError" exception.\n' '\n' 'Note: In the current implementation, the right-hand operand is\n' ' required to be at most "sys.maxsize". If the right-hand ' 'operand is\n' ' larger than "sys.maxsize" an "OverflowError" exception is ' 'raised.\n', 'slicings': '\n' 'Slicings\n' '********\n' '\n' 'A slicing selects a range of items in a sequence object (e.g., ' 'a\n' 'string, tuple or list). Slicings may be used as expressions or ' 'as\n' 'targets in assignment or "del" statements. The syntax for a ' 'slicing:\n' '\n' ' slicing ::= simple_slicing | extended_slicing\n' ' simple_slicing ::= primary "[" short_slice "]"\n' ' extended_slicing ::= primary "[" slice_list "]"\n' ' slice_list ::= slice_item ("," slice_item)* [","]\n' ' slice_item ::= expression | proper_slice | ellipsis\n' ' proper_slice ::= short_slice | long_slice\n' ' short_slice ::= [lower_bound] ":" [upper_bound]\n' ' long_slice ::= short_slice ":" [stride]\n' ' lower_bound ::= expression\n' ' upper_bound ::= expression\n' ' stride ::= expression\n' ' ellipsis ::= "..."\n' '\n' 'There is ambiguity in the formal syntax here: anything that ' 'looks like\n' 'an expression list also looks like a slice list, so any ' 'subscription\n' 'can be interpreted as a slicing. Rather than further ' 'complicating the\n' 'syntax, this is disambiguated by defining that in this case the\n' 'interpretation as a subscription takes priority over the\n' 'interpretation as a slicing (this is the case if the slice list\n' 'contains no proper slice nor ellipses). Similarly, when the ' 'slice\n' 'list has exactly one short slice and no trailing comma, the\n' 'interpretation as a simple slicing takes priority over that as ' 'an\n' 'extended slicing.\n' '\n' 'The semantics for a simple slicing are as follows. The primary ' 'must\n' 'evaluate to a sequence object. The lower and upper bound ' 'expressions,\n' 'if present, must evaluate to plain integers; defaults are zero ' 'and the\n' '"sys.maxint", respectively. If either bound is negative, the\n' "sequence's length is added to it. The slicing now selects all " 'items\n' 'with index *k* such that "i <= k < j" where *i* and *j* are the\n' 'specified lower and upper bounds. This may be an empty ' 'sequence. It\n' 'is not an error if *i* or *j* lie outside the range of valid ' 'indexes\n' "(such items don't exist so they aren't selected).\n" '\n' 'The semantics for an extended slicing are as follows. The ' 'primary\n' 'must evaluate to a mapping object, and it is indexed with a key ' 'that\n' 'is constructed from the slice list, as follows. If the slice ' 'list\n' 'contains at least one comma, the key is a tuple containing the\n' 'conversion of the slice items; otherwise, the conversion of the ' 'lone\n' 'slice item is the key. The conversion of a slice item that is ' 'an\n' 'expression is that expression. The conversion of an ellipsis ' 'slice\n' 'item is the built-in "Ellipsis" object. The conversion of a ' 'proper\n' 'slice is a slice object (see section The standard type ' 'hierarchy)\n' 'whose "start", "stop" and "step" attributes are the values of ' 'the\n' 'expressions given as lower bound, upper bound and stride,\n' 'respectively, substituting "None" for missing expressions.\n', 'specialattrs': '\n' 'Special Attributes\n' '******************\n' '\n' 'The implementation adds a few special read-only attributes ' 'to several\n' 'object types, where they are relevant. Some of these are ' 'not reported\n' 'by the "dir()" built-in function.\n' '\n' 'object.__dict__\n' '\n' ' A dictionary or other mapping object used to store an ' "object's\n" ' (writable) attributes.\n' '\n' 'object.__methods__\n' '\n' ' Deprecated since version 2.2: Use the built-in function ' '"dir()" to\n' " get a list of an object's attributes. This attribute is " 'no longer\n' ' available.\n' '\n' 'object.__members__\n' '\n' ' Deprecated since version 2.2: Use the built-in function ' '"dir()" to\n' " get a list of an object's attributes. This attribute is " 'no longer\n' ' available.\n' '\n' 'instance.__class__\n' '\n' ' The class to which a class instance belongs.\n' '\n' 'class.__bases__\n' '\n' ' The tuple of base classes of a class object.\n' '\n' 'definition.__name__\n' '\n' ' The name of the class, type, function, method, ' 'descriptor, or\n' ' generator instance.\n' '\n' 'The following attributes are only supported by *new-style ' 'class*es.\n' '\n' 'class.__mro__\n' '\n' ' This attribute is a tuple of classes that are considered ' 'when\n' ' looking for base classes during method resolution.\n' '\n' 'class.mro()\n' '\n' ' This method can be overridden by a metaclass to customize ' 'the\n' ' method resolution order for its instances. It is called ' 'at class\n' ' instantiation, and its result is stored in "__mro__".\n' '\n' 'class.__subclasses__()\n' '\n' ' Each new-style class keeps a list of weak references to ' 'its\n' ' immediate subclasses. This method returns a list of all ' 'those\n' ' references still alive. Example:\n' '\n' ' >>> int.__subclasses__()\n' " []\n" '\n' '-[ Footnotes ]-\n' '\n' '[1] Additional information on these special methods may be ' 'found\n' ' in the Python Reference Manual (Basic customization).\n' '\n' '[2] As a consequence, the list "[1, 2]" is considered equal ' 'to\n' ' "[1.0, 2.0]", and similarly for tuples.\n' '\n' "[3] They must have since the parser can't tell the type of " 'the\n' ' operands.\n' '\n' '[4] Cased characters are those with general category ' 'property\n' ' being one of "Lu" (Letter, uppercase), "Ll" (Letter, ' 'lowercase),\n' ' or "Lt" (Letter, titlecase).\n' '\n' '[5] To format only a tuple you should therefore provide a\n' ' singleton tuple whose only element is the tuple to be ' 'formatted.\n' '\n' '[6] The advantage of leaving the newline on is that ' 'returning an\n' ' empty string is then an unambiguous EOF indication. It ' 'is also\n' ' possible (in cases where it might matter, for example, ' 'if you want\n' ' to make an exact copy of a file while scanning its ' 'lines) to tell\n' ' whether the last line of a file ended in a newline or ' 'not (yes\n' ' this happens!).\n', 'specialnames': '\n' 'Special method names\n' '********************\n' '\n' 'A class can implement certain operations that are invoked by ' 'special\n' 'syntax (such as arithmetic operations or subscripting and ' 'slicing) by\n' "defining methods with special names. This is Python's " 'approach to\n' '*operator overloading*, allowing classes to define their own ' 'behavior\n' 'with respect to language operators. For instance, if a ' 'class defines\n' 'a method named "__getitem__()", and "x" is an instance of ' 'this class,\n' 'then "x[i]" is roughly equivalent to "x.__getitem__(i)" for ' 'old-style\n' 'classes and "type(x).__getitem__(x, i)" for new-style ' 'classes. Except\n' 'where mentioned, attempts to execute an operation raise an ' 'exception\n' 'when no appropriate method is defined (typically ' '"AttributeError" or\n' '"TypeError").\n' '\n' 'When implementing a class that emulates any built-in type, ' 'it is\n' 'important that the emulation only be implemented to the ' 'degree that it\n' 'makes sense for the object being modelled. For example, ' 'some\n' 'sequences may work well with retrieval of individual ' 'elements, but\n' 'extracting a slice may not make sense. (One example of this ' 'is the\n' '"NodeList" interface in the W3C\'s Document Object Model.)\n' '\n' '\n' 'Basic customization\n' '===================\n' '\n' 'object.__new__(cls[, ...])\n' '\n' ' Called to create a new instance of class *cls*. ' '"__new__()" is a\n' ' static method (special-cased so you need not declare it ' 'as such)\n' ' that takes the class of which an instance was requested ' 'as its\n' ' first argument. The remaining arguments are those passed ' 'to the\n' ' object constructor expression (the call to the class). ' 'The return\n' ' value of "__new__()" should be the new object instance ' '(usually an\n' ' instance of *cls*).\n' '\n' ' Typical implementations create a new instance of the ' 'class by\n' ' invoking the superclass\'s "__new__()" method using\n' ' "super(currentclass, cls).__new__(cls[, ...])" with ' 'appropriate\n' ' arguments and then modifying the newly-created instance ' 'as\n' ' necessary before returning it.\n' '\n' ' If "__new__()" returns an instance of *cls*, then the ' 'new\n' ' instance\'s "__init__()" method will be invoked like\n' ' "__init__(self[, ...])", where *self* is the new instance ' 'and the\n' ' remaining arguments are the same as were passed to ' '"__new__()".\n' '\n' ' If "__new__()" does not return an instance of *cls*, then ' 'the new\n' ' instance\'s "__init__()" method will not be invoked.\n' '\n' ' "__new__()" is intended mainly to allow subclasses of ' 'immutable\n' ' types (like int, str, or tuple) to customize instance ' 'creation. It\n' ' is also commonly overridden in custom metaclasses in ' 'order to\n' ' customize class creation.\n' '\n' 'object.__init__(self[, ...])\n' '\n' ' Called after the instance has been created (by ' '"__new__()"), but\n' ' before it is returned to the caller. The arguments are ' 'those\n' ' passed to the class constructor expression. If a base ' 'class has an\n' ' "__init__()" method, the derived class\'s "__init__()" ' 'method, if\n' ' any, must explicitly call it to ensure proper ' 'initialization of the\n' ' base class part of the instance; for example:\n' ' "BaseClass.__init__(self, [args...])".\n' '\n' ' Because "__new__()" and "__init__()" work together in ' 'constructing\n' ' objects ("__new__()" to create it, and "__init__()" to ' 'customise\n' ' it), no non-"None" value may be returned by "__init__()"; ' 'doing so\n' ' will cause a "TypeError" to be raised at runtime.\n' '\n' 'object.__del__(self)\n' '\n' ' Called when the instance is about to be destroyed. This ' 'is also\n' ' called a destructor. If a base class has a "__del__()" ' 'method, the\n' ' derived class\'s "__del__()" method, if any, must ' 'explicitly call it\n' ' to ensure proper deletion of the base class part of the ' 'instance.\n' ' Note that it is possible (though not recommended!) for ' 'the\n' ' "__del__()" method to postpone destruction of the ' 'instance by\n' ' creating a new reference to it. It may then be called at ' 'a later\n' ' time when this new reference is deleted. It is not ' 'guaranteed that\n' ' "__del__()" methods are called for objects that still ' 'exist when\n' ' the interpreter exits.\n' '\n' ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' 'the former\n' ' decrements the reference count for "x" by one, and the ' 'latter is\n' ' only called when "x"\'s reference count reaches zero. ' 'Some common\n' ' situations that may prevent the reference count of an ' 'object from\n' ' going to zero include: circular references between ' 'objects (e.g.,\n' ' a doubly-linked list or a tree data structure with ' 'parent and\n' ' child pointers); a reference to the object on the stack ' 'frame of\n' ' a function that caught an exception (the traceback ' 'stored in\n' ' "sys.exc_traceback" keeps the stack frame alive); or a ' 'reference\n' ' to the object on the stack frame that raised an ' 'unhandled\n' ' exception in interactive mode (the traceback stored in\n' ' "sys.last_traceback" keeps the stack frame alive). The ' 'first\n' ' situation can only be remedied by explicitly breaking ' 'the cycles;\n' ' the latter two situations can be resolved by storing ' '"None" in\n' ' "sys.exc_traceback" or "sys.last_traceback". Circular ' 'references\n' ' which are garbage are detected when the option cycle ' 'detector is\n' " enabled (it's on by default), but can only be cleaned " 'up if there\n' ' are no Python-level "__del__()" methods involved. Refer ' 'to the\n' ' documentation for the "gc" module for more information ' 'about how\n' ' "__del__()" methods are handled by the cycle detector,\n' ' particularly the description of the "garbage" value.\n' '\n' ' Warning: Due to the precarious circumstances under which\n' ' "__del__()" methods are invoked, exceptions that occur ' 'during\n' ' their execution are ignored, and a warning is printed ' 'to\n' ' "sys.stderr" instead. Also, when "__del__()" is invoked ' 'in\n' ' response to a module being deleted (e.g., when ' 'execution of the\n' ' program is done), other globals referenced by the ' '"__del__()"\n' ' method may already have been deleted or in the process ' 'of being\n' ' torn down (e.g. the import machinery shutting down). ' 'For this\n' ' reason, "__del__()" methods should do the absolute ' 'minimum needed\n' ' to maintain external invariants. Starting with version ' '1.5,\n' ' Python guarantees that globals whose name begins with a ' 'single\n' ' underscore are deleted from their module before other ' 'globals are\n' ' deleted; if no other references to such globals exist, ' 'this may\n' ' help in assuring that imported modules are still ' 'available at the\n' ' time when the "__del__()" method is called.\n' '\n' ' See also the "-R" command-line option.\n' '\n' 'object.__repr__(self)\n' '\n' ' Called by the "repr()" built-in function and by string ' 'conversions\n' ' (reverse quotes) to compute the "official" string ' 'representation of\n' ' an object. If at all possible, this should look like a ' 'valid\n' ' Python expression that could be used to recreate an ' 'object with the\n' ' same value (given an appropriate environment). If this ' 'is not\n' ' possible, a string of the form "<...some useful ' 'description...>"\n' ' should be returned. The return value must be a string ' 'object. If a\n' ' class defines "__repr__()" but not "__str__()", then ' '"__repr__()"\n' ' is also used when an "informal" string representation of ' 'instances\n' ' of that class is required.\n' '\n' ' This is typically used for debugging, so it is important ' 'that the\n' ' representation is information-rich and unambiguous.\n' '\n' 'object.__str__(self)\n' '\n' ' Called by the "str()" built-in function and by the ' '"print"\n' ' statement to compute the "informal" string representation ' 'of an\n' ' object. This differs from "__repr__()" in that it does ' 'not have to\n' ' be a valid Python expression: a more convenient or ' 'concise\n' ' representation may be used instead. The return value must ' 'be a\n' ' string object.\n' '\n' 'object.__lt__(self, other)\n' 'object.__le__(self, other)\n' 'object.__eq__(self, other)\n' 'object.__ne__(self, other)\n' 'object.__gt__(self, other)\n' 'object.__ge__(self, other)\n' '\n' ' New in version 2.1.\n' '\n' ' These are the so-called "rich comparison" methods, and ' 'are called\n' ' for comparison operators in preference to "__cmp__()" ' 'below. The\n' ' correspondence between operator symbols and method names ' 'is as\n' ' follows: "xy" call ' '"x.__ne__(y)",\n' ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' '"x.__ge__(y)".\n' '\n' ' A rich comparison method may return the singleton ' '"NotImplemented"\n' ' if it does not implement the operation for a given pair ' 'of\n' ' arguments. By convention, "False" and "True" are returned ' 'for a\n' ' successful comparison. However, these methods can return ' 'any value,\n' ' so if the comparison operator is used in a Boolean ' 'context (e.g.,\n' ' in the condition of an "if" statement), Python will call ' '"bool()"\n' ' on the value to determine if the result is true or ' 'false.\n' '\n' ' There are no implied relationships among the comparison ' 'operators.\n' ' The truth of "x==y" does not imply that "x!=y" is false.\n' ' Accordingly, when defining "__eq__()", one should also ' 'define\n' ' "__ne__()" so that the operators will behave as ' 'expected. See the\n' ' paragraph on "__hash__()" for some important notes on ' 'creating\n' ' *hashable* objects which support custom comparison ' 'operations and\n' ' are usable as dictionary keys.\n' '\n' ' There are no swapped-argument versions of these methods ' '(to be used\n' ' when the left argument does not support the operation but ' 'the right\n' ' argument does); rather, "__lt__()" and "__gt__()" are ' "each other's\n" ' reflection, "__le__()" and "__ge__()" are each other\'s ' 'reflection,\n' ' and "__eq__()" and "__ne__()" are their own reflection.\n' '\n' ' Arguments to rich comparison methods are never coerced.\n' '\n' ' To automatically generate ordering operations from a ' 'single root\n' ' operation, see "functools.total_ordering()".\n' '\n' 'object.__cmp__(self, other)\n' '\n' ' Called by comparison operations if rich comparison (see ' 'above) is\n' ' not defined. Should return a negative integer if "self < ' 'other",\n' ' zero if "self == other", a positive integer if "self > ' 'other". If\n' ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' 'defined,\n' ' class instances are compared by object identity ' '("address"). See\n' ' also the description of "__hash__()" for some important ' 'notes on\n' ' creating *hashable* objects which support custom ' 'comparison\n' ' operations and are usable as dictionary keys. (Note: the\n' ' restriction that exceptions are not propagated by ' '"__cmp__()" has\n' ' been removed since Python 1.5.)\n' '\n' 'object.__rcmp__(self, other)\n' '\n' ' Changed in version 2.1: No longer supported.\n' '\n' 'object.__hash__(self)\n' '\n' ' Called by built-in function "hash()" and for operations ' 'on members\n' ' of hashed collections including "set", "frozenset", and ' '"dict".\n' ' "__hash__()" should return an integer. The only required ' 'property\n' ' is that objects which compare equal have the same hash ' 'value; it is\n' ' advised to mix together the hash values of the components ' 'of the\n' ' object that also play a part in comparison of objects by ' 'packing\n' ' them into a tuple and hashing the tuple. Example:\n' '\n' ' def __hash__(self):\n' ' return hash((self.name, self.nick, self.color))\n' '\n' ' If a class does not define a "__cmp__()" or "__eq__()" ' 'method it\n' ' should not define a "__hash__()" operation either; if it ' 'defines\n' ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' 'instances will\n' ' not be usable in hashed collections. If a class defines ' 'mutable\n' ' objects and implements a "__cmp__()" or "__eq__()" ' 'method, it\n' ' should not implement "__hash__()", since hashable ' 'collection\n' " implementations require that an object's hash value is " 'immutable\n' " (if the object's hash value changes, it will be in the " 'wrong hash\n' ' bucket).\n' '\n' ' User-defined classes have "__cmp__()" and "__hash__()" ' 'methods by\n' ' default; with them, all objects compare unequal (except ' 'with\n' ' themselves) and "x.__hash__()" returns a result derived ' 'from\n' ' "id(x)".\n' '\n' ' Classes which inherit a "__hash__()" method from a parent ' 'class but\n' ' change the meaning of "__cmp__()" or "__eq__()" such that ' 'the hash\n' ' value returned is no longer appropriate (e.g. by ' 'switching to a\n' ' value-based concept of equality instead of the default ' 'identity\n' ' based equality) can explicitly flag themselves as being ' 'unhashable\n' ' by setting "__hash__ = None" in the class definition. ' 'Doing so\n' ' means that not only will instances of the class raise an\n' ' appropriate "TypeError" when a program attempts to ' 'retrieve their\n' ' hash value, but they will also be correctly identified ' 'as\n' ' unhashable when checking "isinstance(obj, ' 'collections.Hashable)"\n' ' (unlike classes which define their own "__hash__()" to ' 'explicitly\n' ' raise "TypeError").\n' '\n' ' Changed in version 2.5: "__hash__()" may now also return ' 'a long\n' ' integer object; the 32-bit integer is then derived from ' 'the hash of\n' ' that object.\n' '\n' ' Changed in version 2.6: "__hash__" may now be set to ' '"None" to\n' ' explicitly flag instances of a class as unhashable.\n' '\n' 'object.__nonzero__(self)\n' '\n' ' Called to implement truth value testing and the built-in ' 'operation\n' ' "bool()"; should return "False" or "True", or their ' 'integer\n' ' equivalents "0" or "1". When this method is not ' 'defined,\n' ' "__len__()" is called, if it is defined, and the object ' 'is\n' ' considered true if its result is nonzero. If a class ' 'defines\n' ' neither "__len__()" nor "__nonzero__()", all its ' 'instances are\n' ' considered true.\n' '\n' 'object.__unicode__(self)\n' '\n' ' Called to implement "unicode()" built-in; should return a ' 'Unicode\n' ' object. When this method is not defined, string ' 'conversion is\n' ' attempted, and the result of string conversion is ' 'converted to\n' ' Unicode using the system default encoding.\n' '\n' '\n' 'Customizing attribute access\n' '============================\n' '\n' 'The following methods can be defined to customize the ' 'meaning of\n' 'attribute access (use of, assignment to, or deletion of ' '"x.name") for\n' 'class instances.\n' '\n' 'object.__getattr__(self, name)\n' '\n' ' Called when an attribute lookup has not found the ' 'attribute in the\n' ' usual places (i.e. it is not an instance attribute nor is ' 'it found\n' ' in the class tree for "self"). "name" is the attribute ' 'name. This\n' ' method should return the (computed) attribute value or ' 'raise an\n' ' "AttributeError" exception.\n' '\n' ' Note that if the attribute is found through the normal ' 'mechanism,\n' ' "__getattr__()" is not called. (This is an intentional ' 'asymmetry\n' ' between "__getattr__()" and "__setattr__()".) This is ' 'done both for\n' ' efficiency reasons and because otherwise "__getattr__()" ' 'would have\n' ' no way to access other attributes of the instance. Note ' 'that at\n' ' least for instance variables, you can fake total control ' 'by not\n' ' inserting any values in the instance attribute dictionary ' '(but\n' ' instead inserting them in another object). See the\n' ' "__getattribute__()" method below for a way to actually ' 'get total\n' ' control in new-style classes.\n' '\n' 'object.__setattr__(self, name, value)\n' '\n' ' Called when an attribute assignment is attempted. This ' 'is called\n' ' instead of the normal mechanism (i.e. store the value in ' 'the\n' ' instance dictionary). *name* is the attribute name, ' '*value* is the\n' ' value to be assigned to it.\n' '\n' ' If "__setattr__()" wants to assign to an instance ' 'attribute, it\n' ' should not simply execute "self.name = value" --- this ' 'would cause\n' ' a recursive call to itself. Instead, it should insert ' 'the value in\n' ' the dictionary of instance attributes, e.g., ' '"self.__dict__[name] =\n' ' value". For new-style classes, rather than accessing the ' 'instance\n' ' dictionary, it should call the base class method with the ' 'same\n' ' name, for example, "object.__setattr__(self, name, ' 'value)".\n' '\n' 'object.__delattr__(self, name)\n' '\n' ' Like "__setattr__()" but for attribute deletion instead ' 'of\n' ' assignment. This should only be implemented if "del ' 'obj.name" is\n' ' meaningful for the object.\n' '\n' '\n' 'More attribute access for new-style classes\n' '-------------------------------------------\n' '\n' 'The following methods only apply to new-style classes.\n' '\n' 'object.__getattribute__(self, name)\n' '\n' ' Called unconditionally to implement attribute accesses ' 'for\n' ' instances of the class. If the class also defines ' '"__getattr__()",\n' ' the latter will not be called unless "__getattribute__()" ' 'either\n' ' calls it explicitly or raises an "AttributeError". This ' 'method\n' ' should return the (computed) attribute value or raise an\n' ' "AttributeError" exception. In order to avoid infinite ' 'recursion in\n' ' this method, its implementation should always call the ' 'base class\n' ' method with the same name to access any attributes it ' 'needs, for\n' ' example, "object.__getattribute__(self, name)".\n' '\n' ' Note: This method may still be bypassed when looking up ' 'special\n' ' methods as the result of implicit invocation via ' 'language syntax\n' ' or built-in functions. See Special method lookup for ' 'new-style\n' ' classes.\n' '\n' '\n' 'Implementing Descriptors\n' '------------------------\n' '\n' 'The following methods only apply when an instance of the ' 'class\n' 'containing the method (a so-called *descriptor* class) ' 'appears in an\n' "*owner* class (the descriptor must be in either the owner's " 'class\n' 'dictionary or in the class dictionary for one of its ' 'parents). In the\n' 'examples below, "the attribute" refers to the attribute ' 'whose name is\n' 'the key of the property in the owner class\' "__dict__".\n' '\n' 'object.__get__(self, instance, owner)\n' '\n' ' Called to get the attribute of the owner class (class ' 'attribute\n' ' access) or of an instance of that class (instance ' 'attribute\n' ' access). *owner* is always the owner class, while ' '*instance* is the\n' ' instance that the attribute was accessed through, or ' '"None" when\n' ' the attribute is accessed through the *owner*. This ' 'method should\n' ' return the (computed) attribute value or raise an ' '"AttributeError"\n' ' exception.\n' '\n' 'object.__set__(self, instance, value)\n' '\n' ' Called to set the attribute on an instance *instance* of ' 'the owner\n' ' class to a new value, *value*.\n' '\n' 'object.__delete__(self, instance)\n' '\n' ' Called to delete the attribute on an instance *instance* ' 'of the\n' ' owner class.\n' '\n' '\n' 'Invoking Descriptors\n' '--------------------\n' '\n' 'In general, a descriptor is an object attribute with ' '"binding\n' 'behavior", one whose attribute access has been overridden by ' 'methods\n' 'in the descriptor protocol: "__get__()", "__set__()", and\n' '"__delete__()". If any of those methods are defined for an ' 'object, it\n' 'is said to be a descriptor.\n' '\n' 'The default behavior for attribute access is to get, set, or ' 'delete\n' "the attribute from an object's dictionary. For instance, " '"a.x" has a\n' 'lookup chain starting with "a.__dict__[\'x\']", then\n' '"type(a).__dict__[\'x\']", and continuing through the base ' 'classes of\n' '"type(a)" excluding metaclasses.\n' '\n' 'However, if the looked-up value is an object defining one of ' 'the\n' 'descriptor methods, then Python may override the default ' 'behavior and\n' 'invoke the descriptor method instead. Where this occurs in ' 'the\n' 'precedence chain depends on which descriptor methods were ' 'defined and\n' 'how they were called. Note that descriptors are only ' 'invoked for new\n' 'style objects or classes (ones that subclass "object()" or ' '"type()").\n' '\n' 'The starting point for descriptor invocation is a binding, ' '"a.x". How\n' 'the arguments are assembled depends on "a":\n' '\n' 'Direct Call\n' ' The simplest and least common call is when user code ' 'directly\n' ' invokes a descriptor method: "x.__get__(a)".\n' '\n' 'Instance Binding\n' ' If binding to a new-style object instance, "a.x" is ' 'transformed\n' ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' 'type(a))".\n' '\n' 'Class Binding\n' ' If binding to a new-style class, "A.x" is transformed ' 'into the\n' ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' '\n' 'Super Binding\n' ' If "a" is an instance of "super", then the binding ' '"super(B,\n' ' obj).m()" searches "obj.__class__.__mro__" for the base ' 'class "A"\n' ' immediately preceding "B" and then invokes the descriptor ' 'with the\n' ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n' '\n' 'For instance bindings, the precedence of descriptor ' 'invocation depends\n' 'on the which descriptor methods are defined. A descriptor ' 'can define\n' 'any combination of "__get__()", "__set__()" and ' '"__delete__()". If it\n' 'does not define "__get__()", then accessing the attribute ' 'will return\n' 'the descriptor object itself unless there is a value in the ' "object's\n" 'instance dictionary. If the descriptor defines "__set__()" ' 'and/or\n' '"__delete__()", it is a data descriptor; if it defines ' 'neither, it is\n' 'a non-data descriptor. Normally, data descriptors define ' 'both\n' '"__get__()" and "__set__()", while non-data descriptors have ' 'just the\n' '"__get__()" method. Data descriptors with "__set__()" and ' '"__get__()"\n' 'defined always override a redefinition in an instance ' 'dictionary. In\n' 'contrast, non-data descriptors can be overridden by ' 'instances.\n' '\n' 'Python methods (including "staticmethod()" and ' '"classmethod()") are\n' 'implemented as non-data descriptors. Accordingly, instances ' 'can\n' 'redefine and override methods. This allows individual ' 'instances to\n' 'acquire behaviors that differ from other instances of the ' 'same class.\n' '\n' 'The "property()" function is implemented as a data ' 'descriptor.\n' 'Accordingly, instances cannot override the behavior of a ' 'property.\n' '\n' '\n' '__slots__\n' '---------\n' '\n' 'By default, instances of both old and new-style classes have ' 'a\n' 'dictionary for attribute storage. This wastes space for ' 'objects\n' 'having very few instance variables. The space consumption ' 'can become\n' 'acute when creating large numbers of instances.\n' '\n' 'The default can be overridden by defining *__slots__* in a ' 'new-style\n' 'class definition. The *__slots__* declaration takes a ' 'sequence of\n' 'instance variables and reserves just enough space in each ' 'instance to\n' 'hold a value for each variable. Space is saved because ' '*__dict__* is\n' 'not created for each instance.\n' '\n' '__slots__\n' '\n' ' This class variable can be assigned a string, iterable, ' 'or sequence\n' ' of strings with variable names used by instances. If ' 'defined in a\n' ' new-style class, *__slots__* reserves space for the ' 'declared\n' ' variables and prevents the automatic creation of ' '*__dict__* and\n' ' *__weakref__* for each instance.\n' '\n' ' New in version 2.2.\n' '\n' 'Notes on using *__slots__*\n' '\n' '* When inheriting from a class without *__slots__*, the ' '*__dict__*\n' ' attribute of that class will always be accessible, so a ' '*__slots__*\n' ' definition in the subclass is meaningless.\n' '\n' '* Without a *__dict__* variable, instances cannot be ' 'assigned new\n' ' variables not listed in the *__slots__* definition. ' 'Attempts to\n' ' assign to an unlisted variable name raises ' '"AttributeError". If\n' ' dynamic assignment of new variables is desired, then add\n' ' "\'__dict__\'" to the sequence of strings in the ' '*__slots__*\n' ' declaration.\n' '\n' ' Changed in version 2.3: Previously, adding "\'__dict__\'" ' 'to the\n' ' *__slots__* declaration would not enable the assignment of ' 'new\n' ' attributes not specifically listed in the sequence of ' 'instance\n' ' variable names.\n' '\n' '* Without a *__weakref__* variable for each instance, ' 'classes\n' ' defining *__slots__* do not support weak references to ' 'its\n' ' instances. If weak reference support is needed, then add\n' ' "\'__weakref__\'" to the sequence of strings in the ' '*__slots__*\n' ' declaration.\n' '\n' ' Changed in version 2.3: Previously, adding ' '"\'__weakref__\'" to the\n' ' *__slots__* declaration would not enable support for weak\n' ' references.\n' '\n' '* *__slots__* are implemented at the class level by ' 'creating\n' ' descriptors (Implementing Descriptors) for each variable ' 'name. As a\n' ' result, class attributes cannot be used to set default ' 'values for\n' ' instance variables defined by *__slots__*; otherwise, the ' 'class\n' ' attribute would overwrite the descriptor assignment.\n' '\n' '* The action of a *__slots__* declaration is limited to the ' 'class\n' ' where it is defined. As a result, subclasses will have a ' '*__dict__*\n' ' unless they also define *__slots__* (which must only ' 'contain names\n' ' of any *additional* slots).\n' '\n' '* If a class defines a slot also defined in a base class, ' 'the\n' ' instance variable defined by the base class slot is ' 'inaccessible\n' ' (except by retrieving its descriptor directly from the ' 'base class).\n' ' This renders the meaning of the program undefined. In the ' 'future, a\n' ' check may be added to prevent this.\n' '\n' '* Nonempty *__slots__* does not work for classes derived ' 'from\n' ' "variable-length" built-in types such as "long", "str" and ' '"tuple".\n' '\n' '* Any non-string iterable may be assigned to *__slots__*. ' 'Mappings\n' ' may also be used; however, in the future, special meaning ' 'may be\n' ' assigned to the values corresponding to each key.\n' '\n' '* *__class__* assignment works only if both classes have the ' 'same\n' ' *__slots__*.\n' '\n' ' Changed in version 2.6: Previously, *__class__* assignment ' 'raised an\n' ' error if either new or old class had *__slots__*.\n' '\n' '\n' 'Customizing class creation\n' '==========================\n' '\n' 'By default, new-style classes are constructed using ' '"type()". A class\n' 'definition is read into a separate namespace and the value ' 'of class\n' 'name is bound to the result of "type(name, bases, dict)".\n' '\n' 'When the class definition is read, if *__metaclass__* is ' 'defined then\n' 'the callable assigned to it will be called instead of ' '"type()". This\n' 'allows classes or functions to be written which monitor or ' 'alter the\n' 'class creation process:\n' '\n' '* Modifying the class dictionary prior to the class being ' 'created.\n' '\n' '* Returning an instance of another class -- essentially ' 'performing\n' ' the role of a factory function.\n' '\n' "These steps will have to be performed in the metaclass's " '"__new__()"\n' 'method -- "type.__new__()" can then be called from this ' 'method to\n' 'create a class with different properties. This example adds ' 'a new\n' 'element to the class dictionary before creating the class:\n' '\n' ' class metacls(type):\n' ' def __new__(mcs, name, bases, dict):\n' " dict['foo'] = 'metacls was here'\n" ' return type.__new__(mcs, name, bases, dict)\n' '\n' 'You can of course also override other class methods (or add ' 'new\n' 'methods); for example defining a custom "__call__()" method ' 'in the\n' 'metaclass allows custom behavior when the class is called, ' 'e.g. not\n' 'always creating a new instance.\n' '\n' '__metaclass__\n' '\n' ' This variable can be any callable accepting arguments for ' '"name",\n' ' "bases", and "dict". Upon class creation, the callable ' 'is used\n' ' instead of the built-in "type()".\n' '\n' ' New in version 2.2.\n' '\n' 'The appropriate metaclass is determined by the following ' 'precedence\n' 'rules:\n' '\n' '* If "dict[\'__metaclass__\']" exists, it is used.\n' '\n' '* Otherwise, if there is at least one base class, its ' 'metaclass is\n' ' used (this looks for a *__class__* attribute first and if ' 'not found,\n' ' uses its type).\n' '\n' '* Otherwise, if a global variable named __metaclass__ ' 'exists, it is\n' ' used.\n' '\n' '* Otherwise, the old-style, classic metaclass ' '(types.ClassType) is\n' ' used.\n' '\n' 'The potential uses for metaclasses are boundless. Some ideas ' 'that have\n' 'been explored including logging, interface checking, ' 'automatic\n' 'delegation, automatic property creation, proxies, ' 'frameworks, and\n' 'automatic resource locking/synchronization.\n' '\n' '\n' 'Customizing instance and subclass checks\n' '========================================\n' '\n' 'New in version 2.6.\n' '\n' 'The following methods are used to override the default ' 'behavior of the\n' '"isinstance()" and "issubclass()" built-in functions.\n' '\n' 'In particular, the metaclass "abc.ABCMeta" implements these ' 'methods in\n' 'order to allow the addition of Abstract Base Classes (ABCs) ' 'as\n' '"virtual base classes" to any class or type (including ' 'built-in\n' 'types), including other ABCs.\n' '\n' 'class.__instancecheck__(self, instance)\n' '\n' ' Return true if *instance* should be considered a (direct ' 'or\n' ' indirect) instance of *class*. If defined, called to ' 'implement\n' ' "isinstance(instance, class)".\n' '\n' 'class.__subclasscheck__(self, subclass)\n' '\n' ' Return true if *subclass* should be considered a (direct ' 'or\n' ' indirect) subclass of *class*. If defined, called to ' 'implement\n' ' "issubclass(subclass, class)".\n' '\n' 'Note that these methods are looked up on the type ' '(metaclass) of a\n' 'class. They cannot be defined as class methods in the ' 'actual class.\n' 'This is consistent with the lookup of special methods that ' 'are called\n' 'on instances, only in this case the instance is itself a ' 'class.\n' '\n' 'See also:\n' '\n' ' **PEP 3119** - Introducing Abstract Base Classes\n' ' Includes the specification for customizing ' '"isinstance()" and\n' ' "issubclass()" behavior through "__instancecheck__()" ' 'and\n' ' "__subclasscheck__()", with motivation for this ' 'functionality in\n' ' the context of adding Abstract Base Classes (see the ' '"abc"\n' ' module) to the language.\n' '\n' '\n' 'Emulating callable objects\n' '==========================\n' '\n' 'object.__call__(self[, args...])\n' '\n' ' Called when the instance is "called" as a function; if ' 'this method\n' ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' ' "x.__call__(arg1, arg2, ...)".\n' '\n' '\n' 'Emulating container types\n' '=========================\n' '\n' 'The following methods can be defined to implement container ' 'objects.\n' 'Containers usually are sequences (such as lists or tuples) ' 'or mappings\n' '(like dictionaries), but can represent other containers as ' 'well. The\n' 'first set of methods is used either to emulate a sequence or ' 'to\n' 'emulate a mapping; the difference is that for a sequence, ' 'the\n' 'allowable keys should be the integers *k* for which "0 <= k ' '< N" where\n' '*N* is the length of the sequence, or slice objects, which ' 'define a\n' 'range of items. (For backwards compatibility, the method\n' '"__getslice__()" (see below) can also be defined to handle ' 'simple, but\n' 'not extended slices.) It is also recommended that mappings ' 'provide the\n' 'methods "keys()", "values()", "items()", "has_key()", ' '"get()",\n' '"clear()", "setdefault()", "iterkeys()", "itervalues()",\n' '"iteritems()", "pop()", "popitem()", "copy()", and ' '"update()" behaving\n' "similar to those for Python's standard dictionary objects. " 'The\n' '"UserDict" module provides a "DictMixin" class to help ' 'create those\n' 'methods from a base set of "__getitem__()", ' '"__setitem__()",\n' '"__delitem__()", and "keys()". Mutable sequences should ' 'provide\n' 'methods "append()", "count()", "index()", "extend()", ' '"insert()",\n' '"pop()", "remove()", "reverse()" and "sort()", like Python ' 'standard\n' 'list objects. Finally, sequence types should implement ' 'addition\n' '(meaning concatenation) and multiplication (meaning ' 'repetition) by\n' 'defining the methods "__add__()", "__radd__()", ' '"__iadd__()",\n' '"__mul__()", "__rmul__()" and "__imul__()" described below; ' 'they\n' 'should not define "__coerce__()" or other numerical ' 'operators. It is\n' 'recommended that both mappings and sequences implement the\n' '"__contains__()" method to allow efficient use of the "in" ' 'operator;\n' 'for mappings, "in" should be equivalent of "has_key()"; for ' 'sequences,\n' 'it should search through the values. It is further ' 'recommended that\n' 'both mappings and sequences implement the "__iter__()" ' 'method to allow\n' 'efficient iteration through the container; for mappings, ' '"__iter__()"\n' 'should be the same as "iterkeys()"; for sequences, it should ' 'iterate\n' 'through the values.\n' '\n' 'object.__len__(self)\n' '\n' ' Called to implement the built-in function "len()". ' 'Should return\n' ' the length of the object, an integer ">=" 0. Also, an ' 'object that\n' ' doesn\'t define a "__nonzero__()" method and whose ' '"__len__()"\n' ' method returns zero is considered to be false in a ' 'Boolean context.\n' '\n' ' **CPython implementation detail:** In CPython, the length ' 'is\n' ' required to be at most "sys.maxsize". If the length is ' 'larger than\n' ' "sys.maxsize" some features (such as "len()") may raise\n' ' "OverflowError". To prevent raising "OverflowError" by ' 'truth value\n' ' testing, an object must define a "__nonzero__()" method.\n' '\n' 'object.__getitem__(self, key)\n' '\n' ' Called to implement evaluation of "self[key]". For ' 'sequence types,\n' ' the accepted keys should be integers and slice objects. ' 'Note that\n' ' the special interpretation of negative indexes (if the ' 'class wishes\n' ' to emulate a sequence type) is up to the "__getitem__()" ' 'method. If\n' ' *key* is of an inappropriate type, "TypeError" may be ' 'raised; if of\n' ' a value outside the set of indexes for the sequence ' '(after any\n' ' special interpretation of negative values), "IndexError" ' 'should be\n' ' raised. For mapping types, if *key* is missing (not in ' 'the\n' ' container), "KeyError" should be raised.\n' '\n' ' Note: "for" loops expect that an "IndexError" will be ' 'raised for\n' ' illegal indexes to allow proper detection of the end of ' 'the\n' ' sequence.\n' '\n' 'object.__missing__(self, key)\n' '\n' ' Called by "dict"."__getitem__()" to implement "self[key]" ' 'for dict\n' ' subclasses when key is not in the dictionary.\n' '\n' 'object.__setitem__(self, key, value)\n' '\n' ' Called to implement assignment to "self[key]". Same note ' 'as for\n' ' "__getitem__()". This should only be implemented for ' 'mappings if\n' ' the objects support changes to the values for keys, or if ' 'new keys\n' ' can be added, or for sequences if elements can be ' 'replaced. The\n' ' same exceptions should be raised for improper *key* ' 'values as for\n' ' the "__getitem__()" method.\n' '\n' 'object.__delitem__(self, key)\n' '\n' ' Called to implement deletion of "self[key]". Same note ' 'as for\n' ' "__getitem__()". This should only be implemented for ' 'mappings if\n' ' the objects support removal of keys, or for sequences if ' 'elements\n' ' can be removed from the sequence. The same exceptions ' 'should be\n' ' raised for improper *key* values as for the ' '"__getitem__()" method.\n' '\n' 'object.__iter__(self)\n' '\n' ' This method is called when an iterator is required for a ' 'container.\n' ' This method should return a new iterator object that can ' 'iterate\n' ' over all the objects in the container. For mappings, it ' 'should\n' ' iterate over the keys of the container, and should also ' 'be made\n' ' available as the method "iterkeys()".\n' '\n' ' Iterator objects also need to implement this method; they ' 'are\n' ' required to return themselves. For more information on ' 'iterator\n' ' objects, see Iterator Types.\n' '\n' 'object.__reversed__(self)\n' '\n' ' Called (if present) by the "reversed()" built-in to ' 'implement\n' ' reverse iteration. It should return a new iterator ' 'object that\n' ' iterates over all the objects in the container in reverse ' 'order.\n' '\n' ' If the "__reversed__()" method is not provided, the ' '"reversed()"\n' ' built-in will fall back to using the sequence protocol ' '("__len__()"\n' ' and "__getitem__()"). Objects that support the sequence ' 'protocol\n' ' should only provide "__reversed__()" if they can provide ' 'an\n' ' implementation that is more efficient than the one ' 'provided by\n' ' "reversed()".\n' '\n' ' New in version 2.6.\n' '\n' 'The membership test operators ("in" and "not in") are ' 'normally\n' 'implemented as an iteration through a sequence. However, ' 'container\n' 'objects can supply the following special method with a more ' 'efficient\n' 'implementation, which also does not require the object be a ' 'sequence.\n' '\n' 'object.__contains__(self, item)\n' '\n' ' Called to implement membership test operators. Should ' 'return true\n' ' if *item* is in *self*, false otherwise. For mapping ' 'objects, this\n' ' should consider the keys of the mapping rather than the ' 'values or\n' ' the key-item pairs.\n' '\n' ' For objects that don\'t define "__contains__()", the ' 'membership test\n' ' first tries iteration via "__iter__()", then the old ' 'sequence\n' ' iteration protocol via "__getitem__()", see this section ' 'in the\n' ' language reference.\n' '\n' '\n' 'Additional methods for emulation of sequence types\n' '==================================================\n' '\n' 'The following optional methods can be defined to further ' 'emulate\n' 'sequence objects. Immutable sequences methods should at ' 'most only\n' 'define "__getslice__()"; mutable sequences might define all ' 'three\n' 'methods.\n' '\n' 'object.__getslice__(self, i, j)\n' '\n' ' Deprecated since version 2.0: Support slice objects as ' 'parameters\n' ' to the "__getitem__()" method. (However, built-in types ' 'in CPython\n' ' currently still implement "__getslice__()". Therefore, ' 'you have to\n' ' override it in derived classes when implementing ' 'slicing.)\n' '\n' ' Called to implement evaluation of "self[i:j]". The ' 'returned object\n' ' should be of the same type as *self*. Note that missing ' '*i* or *j*\n' ' in the slice expression are replaced by zero or ' '"sys.maxsize",\n' ' respectively. If negative indexes are used in the slice, ' 'the\n' ' length of the sequence is added to that index. If the ' 'instance does\n' ' not implement the "__len__()" method, an "AttributeError" ' 'is\n' ' raised. No guarantee is made that indexes adjusted this ' 'way are not\n' ' still negative. Indexes which are greater than the ' 'length of the\n' ' sequence are not modified. If no "__getslice__()" is ' 'found, a slice\n' ' object is created instead, and passed to "__getitem__()" ' 'instead.\n' '\n' 'object.__setslice__(self, i, j, sequence)\n' '\n' ' Called to implement assignment to "self[i:j]". Same notes ' 'for *i*\n' ' and *j* as for "__getslice__()".\n' '\n' ' This method is deprecated. If no "__setslice__()" is ' 'found, or for\n' ' extended slicing of the form "self[i:j:k]", a slice ' 'object is\n' ' created, and passed to "__setitem__()", instead of ' '"__setslice__()"\n' ' being called.\n' '\n' 'object.__delslice__(self, i, j)\n' '\n' ' Called to implement deletion of "self[i:j]". Same notes ' 'for *i* and\n' ' *j* as for "__getslice__()". This method is deprecated. ' 'If no\n' ' "__delslice__()" is found, or for extended slicing of the ' 'form\n' ' "self[i:j:k]", a slice object is created, and passed to\n' ' "__delitem__()", instead of "__delslice__()" being ' 'called.\n' '\n' 'Notice that these methods are only invoked when a single ' 'slice with a\n' 'single colon is used, and the slice method is available. ' 'For slice\n' 'operations involving extended slice notation, or in absence ' 'of the\n' 'slice methods, "__getitem__()", "__setitem__()" or ' '"__delitem__()" is\n' 'called with a slice object as argument.\n' '\n' 'The following example demonstrate how to make your program ' 'or module\n' 'compatible with earlier versions of Python (assuming that ' 'methods\n' '"__getitem__()", "__setitem__()" and "__delitem__()" support ' 'slice\n' 'objects as arguments):\n' '\n' ' class MyClass:\n' ' ...\n' ' def __getitem__(self, index):\n' ' ...\n' ' def __setitem__(self, index, value):\n' ' ...\n' ' def __delitem__(self, index):\n' ' ...\n' '\n' ' if sys.version_info < (2, 0):\n' " # They won't be defined if version is at least " '2.0 final\n' '\n' ' def __getslice__(self, i, j):\n' ' return self[max(0, i):max(0, j):]\n' ' def __setslice__(self, i, j, seq):\n' ' self[max(0, i):max(0, j):] = seq\n' ' def __delslice__(self, i, j):\n' ' del self[max(0, i):max(0, j):]\n' ' ...\n' '\n' 'Note the calls to "max()"; these are necessary because of ' 'the handling\n' 'of negative indices before the "__*slice__()" methods are ' 'called.\n' 'When negative indexes are used, the "__*item__()" methods ' 'receive them\n' 'as provided, but the "__*slice__()" methods get a "cooked" ' 'form of the\n' 'index values. For each negative index value, the length of ' 'the\n' 'sequence is added to the index before calling the method ' '(which may\n' 'still result in a negative index); this is the customary ' 'handling of\n' 'negative indexes by the built-in sequence types, and the ' '"__*item__()"\n' 'methods are expected to do this as well. However, since ' 'they should\n' 'already be doing that, negative indexes cannot be passed in; ' 'they must\n' 'be constrained to the bounds of the sequence before being ' 'passed to\n' 'the "__*item__()" methods. Calling "max(0, i)" conveniently ' 'returns\n' 'the proper value.\n' '\n' '\n' 'Emulating numeric types\n' '=======================\n' '\n' 'The following methods can be defined to emulate numeric ' 'objects.\n' 'Methods corresponding to operations that are not supported ' 'by the\n' 'particular kind of number implemented (e.g., bitwise ' 'operations for\n' 'non-integral numbers) should be left undefined.\n' '\n' 'object.__add__(self, other)\n' 'object.__sub__(self, other)\n' 'object.__mul__(self, other)\n' 'object.__floordiv__(self, other)\n' 'object.__mod__(self, other)\n' 'object.__divmod__(self, other)\n' 'object.__pow__(self, other[, modulo])\n' 'object.__lshift__(self, other)\n' 'object.__rshift__(self, other)\n' 'object.__and__(self, other)\n' 'object.__xor__(self, other)\n' 'object.__or__(self, other)\n' '\n' ' These methods are called to implement the binary ' 'arithmetic\n' ' operations ("+", "-", "*", "//", "%", "divmod()", ' '"pow()", "**",\n' ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' 'the\n' ' expression "x + y", where *x* is an instance of a class ' 'that has an\n' ' "__add__()" method, "x.__add__(y)" is called. The ' '"__divmod__()"\n' ' method should be the equivalent to using "__floordiv__()" ' 'and\n' ' "__mod__()"; it should not be related to "__truediv__()" ' '(described\n' ' below). Note that "__pow__()" should be defined to ' 'accept an\n' ' optional third argument if the ternary version of the ' 'built-in\n' ' "pow()" function is to be supported.\n' '\n' ' If one of those methods does not support the operation ' 'with the\n' ' supplied arguments, it should return "NotImplemented".\n' '\n' 'object.__div__(self, other)\n' 'object.__truediv__(self, other)\n' '\n' ' The division operator ("/") is implemented by these ' 'methods. The\n' ' "__truediv__()" method is used when "__future__.division" ' 'is in\n' ' effect, otherwise "__div__()" is used. If only one of ' 'these two\n' ' methods is defined, the object will not support division ' 'in the\n' ' alternate context; "TypeError" will be raised instead.\n' '\n' 'object.__radd__(self, other)\n' 'object.__rsub__(self, other)\n' 'object.__rmul__(self, other)\n' 'object.__rdiv__(self, other)\n' 'object.__rtruediv__(self, other)\n' 'object.__rfloordiv__(self, other)\n' 'object.__rmod__(self, other)\n' 'object.__rdivmod__(self, other)\n' 'object.__rpow__(self, other)\n' 'object.__rlshift__(self, other)\n' 'object.__rrshift__(self, other)\n' 'object.__rand__(self, other)\n' 'object.__rxor__(self, other)\n' 'object.__ror__(self, other)\n' '\n' ' These methods are called to implement the binary ' 'arithmetic\n' ' operations ("+", "-", "*", "/", "%", "divmod()", "pow()", ' '"**",\n' ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' 'operands.\n' ' These functions are only called if the left operand does ' 'not\n' ' support the corresponding operation and the operands are ' 'of\n' ' different types. [2] For instance, to evaluate the ' 'expression "x -\n' ' y", where *y* is an instance of a class that has an ' '"__rsub__()"\n' ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' 'returns\n' ' *NotImplemented*.\n' '\n' ' Note that ternary "pow()" will not try calling ' '"__rpow__()" (the\n' ' coercion rules would become too complicated).\n' '\n' " Note: If the right operand's type is a subclass of the " 'left\n' " operand's type and that subclass provides the reflected " 'method\n' ' for the operation, this method will be called before ' 'the left\n' " operand's non-reflected method. This behavior allows " 'subclasses\n' " to override their ancestors' operations.\n" '\n' 'object.__iadd__(self, other)\n' 'object.__isub__(self, other)\n' 'object.__imul__(self, other)\n' 'object.__idiv__(self, other)\n' 'object.__itruediv__(self, other)\n' 'object.__ifloordiv__(self, other)\n' 'object.__imod__(self, other)\n' 'object.__ipow__(self, other[, modulo])\n' 'object.__ilshift__(self, other)\n' 'object.__irshift__(self, other)\n' 'object.__iand__(self, other)\n' 'object.__ixor__(self, other)\n' 'object.__ior__(self, other)\n' '\n' ' These methods are called to implement the augmented ' 'arithmetic\n' ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", ' '"<<=",\n' ' ">>=", "&=", "^=", "|="). These methods should attempt ' 'to do the\n' ' operation in-place (modifying *self*) and return the ' 'result (which\n' ' could be, but does not have to be, *self*). If a ' 'specific method\n' ' is not defined, the augmented assignment falls back to ' 'the normal\n' ' methods. For instance, to execute the statement "x += ' 'y", where\n' ' *x* is an instance of a class that has an "__iadd__()" ' 'method,\n' ' "x.__iadd__(y)" is called. If *x* is an instance of a ' 'class that\n' ' does not define a "__iadd__()" method, "x.__add__(y)" ' 'and\n' ' "y.__radd__(x)" are considered, as with the evaluation of ' '"x + y".\n' '\n' 'object.__neg__(self)\n' 'object.__pos__(self)\n' 'object.__abs__(self)\n' 'object.__invert__(self)\n' '\n' ' Called to implement the unary arithmetic operations ("-", ' '"+",\n' ' "abs()" and "~").\n' '\n' 'object.__complex__(self)\n' 'object.__int__(self)\n' 'object.__long__(self)\n' 'object.__float__(self)\n' '\n' ' Called to implement the built-in functions "complex()", ' '"int()",\n' ' "long()", and "float()". Should return a value of the ' 'appropriate\n' ' type.\n' '\n' 'object.__oct__(self)\n' 'object.__hex__(self)\n' '\n' ' Called to implement the built-in functions "oct()" and ' '"hex()".\n' ' Should return a string value.\n' '\n' 'object.__index__(self)\n' '\n' ' Called to implement "operator.index()". Also called ' 'whenever\n' ' Python needs an integer object (such as in slicing). ' 'Must return\n' ' an integer (int or long).\n' '\n' ' New in version 2.5.\n' '\n' 'object.__coerce__(self, other)\n' '\n' ' Called to implement "mixed-mode" numeric arithmetic. ' 'Should either\n' ' return a 2-tuple containing *self* and *other* converted ' 'to a\n' ' common numeric type, or "None" if conversion is ' 'impossible. When\n' ' the common type would be the type of "other", it is ' 'sufficient to\n' ' return "None", since the interpreter will also ask the ' 'other object\n' ' to attempt a coercion (but sometimes, if the ' 'implementation of the\n' ' other type cannot be changed, it is useful to do the ' 'conversion to\n' ' the other type here). A return value of "NotImplemented" ' 'is\n' ' equivalent to returning "None".\n' '\n' '\n' 'Coercion rules\n' '==============\n' '\n' 'This section used to document the rules for coercion. As ' 'the language\n' 'has evolved, the coercion rules have become hard to ' 'document\n' 'precisely; documenting what one version of one particular\n' 'implementation does is undesirable. Instead, here are some ' 'informal\n' 'guidelines regarding coercion. In Python 3, coercion will ' 'not be\n' 'supported.\n' '\n' '* If the left operand of a % operator is a string or Unicode ' 'object,\n' ' no coercion takes place and the string formatting ' 'operation is\n' ' invoked instead.\n' '\n' '* It is no longer recommended to define a coercion ' 'operation. Mixed-\n' " mode operations on types that don't define coercion pass " 'the\n' ' original arguments to the operation.\n' '\n' '* New-style classes (those derived from "object") never ' 'invoke the\n' ' "__coerce__()" method in response to a binary operator; ' 'the only\n' ' time "__coerce__()" is invoked is when the built-in ' 'function\n' ' "coerce()" is called.\n' '\n' '* For most intents and purposes, an operator that returns\n' ' "NotImplemented" is treated the same as one that is not ' 'implemented\n' ' at all.\n' '\n' '* Below, "__op__()" and "__rop__()" are used to signify the ' 'generic\n' ' method names corresponding to an operator; "__iop__()" is ' 'used for\n' ' the corresponding in-place operator. For example, for the ' 'operator\n' ' \'"+"\', "__add__()" and "__radd__()" are used for the ' 'left and right\n' ' variant of the binary operator, and "__iadd__()" for the ' 'in-place\n' ' variant.\n' '\n' '* For objects *x* and *y*, first "x.__op__(y)" is tried. If ' 'this is\n' ' not implemented or returns "NotImplemented", ' '"y.__rop__(x)" is\n' ' tried. If this is also not implemented or returns ' '"NotImplemented",\n' ' a "TypeError" exception is raised. But see the following ' 'exception:\n' '\n' '* Exception to the previous item: if the left operand is an ' 'instance\n' ' of a built-in type or a new-style class, and the right ' 'operand is an\n' ' instance of a proper subclass of that type or class and ' 'overrides\n' ' the base\'s "__rop__()" method, the right operand\'s ' '"__rop__()"\n' ' method is tried *before* the left operand\'s "__op__()" ' 'method.\n' '\n' ' This is done so that a subclass can completely override ' 'binary\n' ' operators. Otherwise, the left operand\'s "__op__()" ' 'method would\n' ' always accept the right operand: when an instance of a ' 'given class\n' ' is expected, an instance of a subclass of that class is ' 'always\n' ' acceptable.\n' '\n' '* When either operand type defines a coercion, this coercion ' 'is\n' ' called before that type\'s "__op__()" or "__rop__()" ' 'method is\n' ' called, but no sooner. If the coercion returns an object ' 'of a\n' ' different type for the operand whose coercion is invoked, ' 'part of\n' ' the process is redone using the new object.\n' '\n' '* When an in-place operator (like \'"+="\') is used, if the ' 'left\n' ' operand implements "__iop__()", it is invoked without any ' 'coercion.\n' ' When the operation falls back to "__op__()" and/or ' '"__rop__()", the\n' ' normal coercion rules apply.\n' '\n' '* In "x + y", if *x* is a sequence that implements sequence\n' ' concatenation, sequence concatenation is invoked.\n' '\n' '* In "x * y", if one operand is a sequence that implements ' 'sequence\n' ' repetition, and the other is an integer ("int" or "long"), ' 'sequence\n' ' repetition is invoked.\n' '\n' '* Rich comparisons (implemented by methods "__eq__()" and so ' 'on)\n' ' never use coercion. Three-way comparison (implemented by\n' ' "__cmp__()") does use coercion under the same conditions ' 'as other\n' ' binary operations use it.\n' '\n' '* In the current implementation, the built-in numeric types ' '"int",\n' ' "long", "float", and "complex" do not use coercion. All ' 'these types\n' ' implement a "__coerce__()" method, for use by the ' 'built-in\n' ' "coerce()" function.\n' '\n' ' Changed in version 2.7: The complex type no longer makes ' 'implicit\n' ' calls to the "__coerce__()" method for mixed-type binary ' 'arithmetic\n' ' operations.\n' '\n' '\n' 'With Statement Context Managers\n' '===============================\n' '\n' 'New in version 2.5.\n' '\n' 'A *context manager* is an object that defines the runtime ' 'context to\n' 'be established when executing a "with" statement. The ' 'context manager\n' 'handles the entry into, and the exit from, the desired ' 'runtime context\n' 'for the execution of the block of code. Context managers ' 'are normally\n' 'invoked using the "with" statement (described in section The ' 'with\n' 'statement), but can also be used by directly invoking their ' 'methods.\n' '\n' 'Typical uses of context managers include saving and ' 'restoring various\n' 'kinds of global state, locking and unlocking resources, ' 'closing opened\n' 'files, etc.\n' '\n' 'For more information on context managers, see Context ' 'Manager Types.\n' '\n' 'object.__enter__(self)\n' '\n' ' Enter the runtime context related to this object. The ' '"with"\n' " statement will bind this method's return value to the " 'target(s)\n' ' specified in the "as" clause of the statement, if any.\n' '\n' 'object.__exit__(self, exc_type, exc_value, traceback)\n' '\n' ' Exit the runtime context related to this object. The ' 'parameters\n' ' describe the exception that caused the context to be ' 'exited. If the\n' ' context was exited without an exception, all three ' 'arguments will\n' ' be "None".\n' '\n' ' If an exception is supplied, and the method wishes to ' 'suppress the\n' ' exception (i.e., prevent it from being propagated), it ' 'should\n' ' return a true value. Otherwise, the exception will be ' 'processed\n' ' normally upon exit from this method.\n' '\n' ' Note that "__exit__()" methods should not reraise the ' 'passed-in\n' " exception; this is the caller's responsibility.\n" '\n' 'See also:\n' '\n' ' **PEP 343** - The "with" statement\n' ' The specification, background, and examples for the ' 'Python "with"\n' ' statement.\n' '\n' '\n' 'Special method lookup for old-style classes\n' '===========================================\n' '\n' 'For old-style classes, special methods are always looked up ' 'in exactly\n' 'the same way as any other method or attribute. This is the ' 'case\n' 'regardless of whether the method is being looked up ' 'explicitly as in\n' '"x.__getitem__(i)" or implicitly as in "x[i]".\n' '\n' 'This behaviour means that special methods may exhibit ' 'different\n' 'behaviour for different instances of a single old-style ' 'class if the\n' 'appropriate special attributes are set differently:\n' '\n' ' >>> class C:\n' ' ... pass\n' ' ...\n' ' >>> c1 = C()\n' ' >>> c2 = C()\n' ' >>> c1.__len__ = lambda: 5\n' ' >>> c2.__len__ = lambda: 9\n' ' >>> len(c1)\n' ' 5\n' ' >>> len(c2)\n' ' 9\n' '\n' '\n' 'Special method lookup for new-style classes\n' '===========================================\n' '\n' 'For new-style classes, implicit invocations of special ' 'methods are\n' "only guaranteed to work correctly if defined on an object's " 'type, not\n' "in the object's instance dictionary. That behaviour is the " 'reason why\n' 'the following code raises an exception (unlike the ' 'equivalent example\n' 'with old-style classes):\n' '\n' ' >>> class C(object):\n' ' ... pass\n' ' ...\n' ' >>> c = C()\n' ' >>> c.__len__ = lambda: 5\n' ' >>> len(c)\n' ' Traceback (most recent call last):\n' ' File "", line 1, in \n' " TypeError: object of type 'C' has no len()\n" '\n' 'The rationale behind this behaviour lies with a number of ' 'special\n' 'methods such as "__hash__()" and "__repr__()" that are ' 'implemented by\n' 'all objects, including type objects. If the implicit lookup ' 'of these\n' 'methods used the conventional lookup process, they would ' 'fail when\n' 'invoked on the type object itself:\n' '\n' ' >>> 1 .__hash__() == hash(1)\n' ' True\n' ' >>> int.__hash__() == hash(int)\n' ' Traceback (most recent call last):\n' ' File "", line 1, in \n' " TypeError: descriptor '__hash__' of 'int' object needs an " 'argument\n' '\n' 'Incorrectly attempting to invoke an unbound method of a ' 'class in this\n' "way is sometimes referred to as 'metaclass confusion', and " 'is avoided\n' 'by bypassing the instance when looking up special methods:\n' '\n' ' >>> type(1).__hash__(1) == hash(1)\n' ' True\n' ' >>> type(int).__hash__(int) == hash(int)\n' ' True\n' '\n' 'In addition to bypassing any instance attributes in the ' 'interest of\n' 'correctness, implicit special method lookup generally also ' 'bypasses\n' 'the "__getattribute__()" method even of the object\'s ' 'metaclass:\n' '\n' ' >>> class Meta(type):\n' ' ... def __getattribute__(*args):\n' ' ... print "Metaclass getattribute invoked"\n' ' ... return type.__getattribute__(*args)\n' ' ...\n' ' >>> class C(object):\n' ' ... __metaclass__ = Meta\n' ' ... def __len__(self):\n' ' ... return 10\n' ' ... def __getattribute__(*args):\n' ' ... print "Class getattribute invoked"\n' ' ... return object.__getattribute__(*args)\n' ' ...\n' ' >>> c = C()\n' ' >>> c.__len__() # Explicit lookup via ' 'instance\n' ' Class getattribute invoked\n' ' 10\n' ' >>> type(c).__len__(c) # Explicit lookup via ' 'type\n' ' Metaclass getattribute invoked\n' ' 10\n' ' >>> len(c) # Implicit lookup\n' ' 10\n' '\n' 'Bypassing the "__getattribute__()" machinery in this fashion ' 'provides\n' 'significant scope for speed optimisations within the ' 'interpreter, at\n' 'the cost of some flexibility in the handling of special ' 'methods (the\n' 'special method *must* be set on the class object itself in ' 'order to be\n' 'consistently invoked by the interpreter).\n' '\n' '-[ Footnotes ]-\n' '\n' "[1] It *is* possible in some cases to change an object's " 'type,\n' " under certain controlled conditions. It generally isn't " 'a good\n' ' idea though, since it can lead to some very strange ' 'behaviour if\n' ' it is handled incorrectly.\n' '\n' '[2] For operands of the same type, it is assumed that if the ' 'non-\n' ' reflected method (such as "__add__()") fails the ' 'operation is not\n' ' supported, which is why the reflected method is not ' 'called.\n', 'string-methods': '\n' 'String Methods\n' '**************\n' '\n' 'Below are listed the string methods which both 8-bit ' 'strings and\n' 'Unicode objects support. Some of them are also available ' 'on\n' '"bytearray" objects.\n' '\n' "In addition, Python's strings support the sequence type " 'methods\n' 'described in the Sequence Types --- str, unicode, list, ' 'tuple,\n' 'bytearray, buffer, xrange section. To output formatted ' 'strings use\n' 'template strings or the "%" operator described in the ' 'String\n' 'Formatting Operations section. Also, see the "re" module ' 'for string\n' 'functions based on regular expressions.\n' '\n' 'str.capitalize()\n' '\n' ' Return a copy of the string with its first character ' 'capitalized\n' ' and the rest lowercased.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.center(width[, fillchar])\n' '\n' ' Return centered in a string of length *width*. Padding ' 'is done\n' ' using the specified *fillchar* (default is a space).\n' '\n' ' Changed in version 2.4: Support for the *fillchar* ' 'argument.\n' '\n' 'str.count(sub[, start[, end]])\n' '\n' ' Return the number of non-overlapping occurrences of ' 'substring *sub*\n' ' in the range [*start*, *end*]. Optional arguments ' '*start* and\n' ' *end* are interpreted as in slice notation.\n' '\n' 'str.decode([encoding[, errors]])\n' '\n' ' Decodes the string using the codec registered for ' '*encoding*.\n' ' *encoding* defaults to the default string encoding. ' '*errors* may\n' ' be given to set a different error handling scheme. The ' 'default is\n' ' "\'strict\'", meaning that encoding errors raise ' '"UnicodeError".\n' ' Other possible values are "\'ignore\'", "\'replace\'" ' 'and any other\n' ' name registered via "codecs.register_error()", see ' 'section Codec\n' ' Base Classes.\n' '\n' ' New in version 2.2.\n' '\n' ' Changed in version 2.3: Support for other error ' 'handling schemes\n' ' added.\n' '\n' ' Changed in version 2.7: Support for keyword arguments ' 'added.\n' '\n' 'str.encode([encoding[, errors]])\n' '\n' ' Return an encoded version of the string. Default ' 'encoding is the\n' ' current default string encoding. *errors* may be given ' 'to set a\n' ' different error handling scheme. The default for ' '*errors* is\n' ' "\'strict\'", meaning that encoding errors raise a ' '"UnicodeError".\n' ' Other possible values are "\'ignore\'", "\'replace\'",\n' ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and any ' 'other name\n' ' registered via "codecs.register_error()", see section ' 'Codec Base\n' ' Classes. For a list of possible encodings, see section ' 'Standard\n' ' Encodings.\n' '\n' ' New in version 2.0.\n' '\n' ' Changed in version 2.3: Support for ' '"\'xmlcharrefreplace\'" and\n' ' "\'backslashreplace\'" and other error handling schemes ' 'added.\n' '\n' ' Changed in version 2.7: Support for keyword arguments ' 'added.\n' '\n' 'str.endswith(suffix[, start[, end]])\n' '\n' ' Return "True" if the string ends with the specified ' '*suffix*,\n' ' otherwise return "False". *suffix* can also be a tuple ' 'of suffixes\n' ' to look for. With optional *start*, test beginning at ' 'that\n' ' position. With optional *end*, stop comparing at that ' 'position.\n' '\n' ' Changed in version 2.5: Accept tuples as *suffix*.\n' '\n' 'str.expandtabs([tabsize])\n' '\n' ' Return a copy of the string where all tab characters ' 'are replaced\n' ' by one or more spaces, depending on the current column ' 'and the\n' ' given tab size. Tab positions occur every *tabsize* ' 'characters\n' ' (default is 8, giving tab positions at columns 0, 8, 16 ' 'and so on).\n' ' To expand the string, the current column is set to zero ' 'and the\n' ' string is examined character by character. If the ' 'character is a\n' ' tab ("\\t"), one or more space characters are inserted ' 'in the result\n' ' until the current column is equal to the next tab ' 'position. (The\n' ' tab character itself is not copied.) If the character ' 'is a newline\n' ' ("\\n") or return ("\\r"), it is copied and the current ' 'column is\n' ' reset to zero. Any other character is copied unchanged ' 'and the\n' ' current column is incremented by one regardless of how ' 'the\n' ' character is represented when printed.\n' '\n' " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" " '01 012 0123 01234'\n" " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" " '01 012 0123 01234'\n" '\n' 'str.find(sub[, start[, end]])\n' '\n' ' Return the lowest index in the string where substring ' '*sub* is\n' ' found within the slice "s[start:end]". Optional ' 'arguments *start*\n' ' and *end* are interpreted as in slice notation. Return ' '"-1" if\n' ' *sub* is not found.\n' '\n' ' Note: The "find()" method should be used only if you ' 'need to know\n' ' the position of *sub*. To check if *sub* is a ' 'substring or not,\n' ' use the "in" operator:\n' '\n' " >>> 'Py' in 'Python'\n" ' True\n' '\n' 'str.format(*args, **kwargs)\n' '\n' ' Perform a string formatting operation. The string on ' 'which this\n' ' method is called can contain literal text or ' 'replacement fields\n' ' delimited by braces "{}". Each replacement field ' 'contains either\n' ' the numeric index of a positional argument, or the name ' 'of a\n' ' keyword argument. Returns a copy of the string where ' 'each\n' ' replacement field is replaced with the string value of ' 'the\n' ' corresponding argument.\n' '\n' ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' " 'The sum of 1 + 2 is 3'\n" '\n' ' See Format String Syntax for a description of the ' 'various\n' ' formatting options that can be specified in format ' 'strings.\n' '\n' ' This method of string formatting is the new standard in ' 'Python 3,\n' ' and should be preferred to the "%" formatting described ' 'in String\n' ' Formatting Operations in new code.\n' '\n' ' New in version 2.6.\n' '\n' 'str.index(sub[, start[, end]])\n' '\n' ' Like "find()", but raise "ValueError" when the ' 'substring is not\n' ' found.\n' '\n' 'str.isalnum()\n' '\n' ' Return true if all characters in the string are ' 'alphanumeric and\n' ' there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isalpha()\n' '\n' ' Return true if all characters in the string are ' 'alphabetic and\n' ' there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isdigit()\n' '\n' ' Return true if all characters in the string are digits ' 'and there is\n' ' at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.islower()\n' '\n' ' Return true if all cased characters [4] in the string ' 'are lowercase\n' ' and there is at least one cased character, false ' 'otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isspace()\n' '\n' ' Return true if there are only whitespace characters in ' 'the string\n' ' and there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.istitle()\n' '\n' ' Return true if the string is a titlecased string and ' 'there is at\n' ' least one character, for example uppercase characters ' 'may only\n' ' follow uncased characters and lowercase characters only ' 'cased ones.\n' ' Return false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isupper()\n' '\n' ' Return true if all cased characters [4] in the string ' 'are uppercase\n' ' and there is at least one cased character, false ' 'otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.join(iterable)\n' '\n' ' Return a string which is the concatenation of the ' 'strings in\n' ' *iterable*. A "TypeError" will be raised if there are ' 'any non-\n' ' string values in *iterable*, including "bytes" ' 'objects. The\n' ' separator between elements is the string providing this ' 'method.\n' '\n' 'str.ljust(width[, fillchar])\n' '\n' ' Return the string left justified in a string of length ' '*width*.\n' ' Padding is done using the specified *fillchar* (default ' 'is a\n' ' space). The original string is returned if *width* is ' 'less than or\n' ' equal to "len(s)".\n' '\n' ' Changed in version 2.4: Support for the *fillchar* ' 'argument.\n' '\n' 'str.lower()\n' '\n' ' Return a copy of the string with all the cased ' 'characters [4]\n' ' converted to lowercase.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.lstrip([chars])\n' '\n' ' Return a copy of the string with leading characters ' 'removed. The\n' ' *chars* argument is a string specifying the set of ' 'characters to be\n' ' removed. If omitted or "None", the *chars* argument ' 'defaults to\n' ' removing whitespace. The *chars* argument is not a ' 'prefix; rather,\n' ' all combinations of its values are stripped:\n' '\n' " >>> ' spacious '.lstrip()\n" " 'spacious '\n" " >>> 'www.example.com'.lstrip('cmowz.')\n" " 'example.com'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* ' 'argument.\n' '\n' 'str.partition(sep)\n' '\n' ' Split the string at the first occurrence of *sep*, and ' 'return a\n' ' 3-tuple containing the part before the separator, the ' 'separator\n' ' itself, and the part after the separator. If the ' 'separator is not\n' ' found, return a 3-tuple containing the string itself, ' 'followed by\n' ' two empty strings.\n' '\n' ' New in version 2.5.\n' '\n' 'str.replace(old, new[, count])\n' '\n' ' Return a copy of the string with all occurrences of ' 'substring *old*\n' ' replaced by *new*. If the optional argument *count* is ' 'given, only\n' ' the first *count* occurrences are replaced.\n' '\n' 'str.rfind(sub[, start[, end]])\n' '\n' ' Return the highest index in the string where substring ' '*sub* is\n' ' found, such that *sub* is contained within ' '"s[start:end]".\n' ' Optional arguments *start* and *end* are interpreted as ' 'in slice\n' ' notation. Return "-1" on failure.\n' '\n' 'str.rindex(sub[, start[, end]])\n' '\n' ' Like "rfind()" but raises "ValueError" when the ' 'substring *sub* is\n' ' not found.\n' '\n' 'str.rjust(width[, fillchar])\n' '\n' ' Return the string right justified in a string of length ' '*width*.\n' ' Padding is done using the specified *fillchar* (default ' 'is a\n' ' space). The original string is returned if *width* is ' 'less than or\n' ' equal to "len(s)".\n' '\n' ' Changed in version 2.4: Support for the *fillchar* ' 'argument.\n' '\n' 'str.rpartition(sep)\n' '\n' ' Split the string at the last occurrence of *sep*, and ' 'return a\n' ' 3-tuple containing the part before the separator, the ' 'separator\n' ' itself, and the part after the separator. If the ' 'separator is not\n' ' found, return a 3-tuple containing two empty strings, ' 'followed by\n' ' the string itself.\n' '\n' ' New in version 2.5.\n' '\n' 'str.rsplit([sep[, maxsplit]])\n' '\n' ' Return a list of the words in the string, using *sep* ' 'as the\n' ' delimiter string. If *maxsplit* is given, at most ' '*maxsplit* splits\n' ' are done, the *rightmost* ones. If *sep* is not ' 'specified or\n' ' "None", any whitespace string is a separator. Except ' 'for splitting\n' ' from the right, "rsplit()" behaves like "split()" which ' 'is\n' ' described in detail below.\n' '\n' ' New in version 2.4.\n' '\n' 'str.rstrip([chars])\n' '\n' ' Return a copy of the string with trailing characters ' 'removed. The\n' ' *chars* argument is a string specifying the set of ' 'characters to be\n' ' removed. If omitted or "None", the *chars* argument ' 'defaults to\n' ' removing whitespace. The *chars* argument is not a ' 'suffix; rather,\n' ' all combinations of its values are stripped:\n' '\n' " >>> ' spacious '.rstrip()\n" " ' spacious'\n" " >>> 'mississippi'.rstrip('ipz')\n" " 'mississ'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* ' 'argument.\n' '\n' 'str.split([sep[, maxsplit]])\n' '\n' ' Return a list of the words in the string, using *sep* ' 'as the\n' ' delimiter string. If *maxsplit* is given, at most ' '*maxsplit*\n' ' splits are done (thus, the list will have at most ' '"maxsplit+1"\n' ' elements). If *maxsplit* is not specified or "-1", ' 'then there is\n' ' no limit on the number of splits (all possible splits ' 'are made).\n' '\n' ' If *sep* is given, consecutive delimiters are not ' 'grouped together\n' ' and are deemed to delimit empty strings (for example,\n' ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' '\'2\']"). The *sep* argument\n' ' may consist of multiple characters (for example,\n' ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' '\'3\']"). Splitting an\n' ' empty string with a specified separator returns ' '"[\'\']".\n' '\n' ' If *sep* is not specified or is "None", a different ' 'splitting\n' ' algorithm is applied: runs of consecutive whitespace ' 'are regarded\n' ' as a single separator, and the result will contain no ' 'empty strings\n' ' at the start or end if the string has leading or ' 'trailing\n' ' whitespace. Consequently, splitting an empty string or ' 'a string\n' ' consisting of just whitespace with a "None" separator ' 'returns "[]".\n' '\n' ' For example, "\' 1 2 3 \'.split()" returns "[\'1\', ' '\'2\', \'3\']", and\n' ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', ' '\'2 3 \']".\n' '\n' 'str.splitlines([keepends])\n' '\n' ' Return a list of the lines in the string, breaking at ' 'line\n' ' boundaries. This method uses the *universal newlines* ' 'approach to\n' ' splitting lines. Line breaks are not included in the ' 'resulting list\n' ' unless *keepends* is given and true.\n' '\n' ' Python recognizes ""\\r"", ""\\n"", and ""\\r\\n"" as ' 'line boundaries\n' ' for 8-bit strings.\n' '\n' ' For example:\n' '\n' " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" " ['ab c', '', 'de fg', 'kl']\n" " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines(True)\n" " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" '\n' ' Unlike "split()" when a delimiter string *sep* is ' 'given, this\n' ' method returns an empty list for the empty string, and ' 'a terminal\n' ' line break does not result in an extra line:\n' '\n' ' >>> "".splitlines()\n' ' []\n' ' >>> "One line\\n".splitlines()\n' " ['One line']\n" '\n' ' For comparison, "split(\'\\n\')" gives:\n' '\n' " >>> ''.split('\\n')\n" " ['']\n" " >>> 'Two lines\\n'.split('\\n')\n" " ['Two lines', '']\n" '\n' 'unicode.splitlines([keepends])\n' '\n' ' Return a list of the lines in the string, like ' '"str.splitlines()".\n' ' However, the Unicode method splits on the following ' 'line\n' ' boundaries, which are a superset of the *universal ' 'newlines*\n' ' recognized for 8-bit strings.\n' '\n' ' ' '+-------------------------+-------------------------------+\n' ' | Representation | ' 'Description |\n' ' ' '+=========================+===============================+\n' ' | "\\n" | Line ' 'Feed |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\r" | Carriage ' 'Return |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\r\\n" | Carriage Return + Line ' 'Feed |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\v" or "\\x0b" | Line ' 'Tabulation |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\f" or "\\x0c" | Form ' 'Feed |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\x1c" | File ' 'Separator |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\x1d" | Group ' 'Separator |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\x1e" | Record ' 'Separator |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\x85" | Next Line (C1 Control ' 'Code) |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\u2028" | Line ' 'Separator |\n' ' ' '+-------------------------+-------------------------------+\n' ' | "\\u2029" | Paragraph ' 'Separator |\n' ' ' '+-------------------------+-------------------------------+\n' '\n' ' Changed in version 2.7: "\\v" and "\\f" added to list ' 'of line\n' ' boundaries.\n' '\n' 'str.startswith(prefix[, start[, end]])\n' '\n' ' Return "True" if string starts with the *prefix*, ' 'otherwise return\n' ' "False". *prefix* can also be a tuple of prefixes to ' 'look for.\n' ' With optional *start*, test string beginning at that ' 'position.\n' ' With optional *end*, stop comparing string at that ' 'position.\n' '\n' ' Changed in version 2.5: Accept tuples as *prefix*.\n' '\n' 'str.strip([chars])\n' '\n' ' Return a copy of the string with the leading and ' 'trailing\n' ' characters removed. The *chars* argument is a string ' 'specifying the\n' ' set of characters to be removed. If omitted or "None", ' 'the *chars*\n' ' argument defaults to removing whitespace. The *chars* ' 'argument is\n' ' not a prefix or suffix; rather, all combinations of its ' 'values are\n' ' stripped:\n' '\n' " >>> ' spacious '.strip()\n" " 'spacious'\n" " >>> 'www.example.com'.strip('cmowz.')\n" " 'example'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* ' 'argument.\n' '\n' 'str.swapcase()\n' '\n' ' Return a copy of the string with uppercase characters ' 'converted to\n' ' lowercase and vice versa.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.title()\n' '\n' ' Return a titlecased version of the string where words ' 'start with an\n' ' uppercase character and the remaining characters are ' 'lowercase.\n' '\n' ' The algorithm uses a simple language-independent ' 'definition of a\n' ' word as groups of consecutive letters. The definition ' 'works in\n' ' many contexts but it means that apostrophes in ' 'contractions and\n' ' possessives form word boundaries, which may not be the ' 'desired\n' ' result:\n' '\n' ' >>> "they\'re bill\'s friends from the UK".title()\n' ' "They\'Re Bill\'S Friends From The Uk"\n' '\n' ' A workaround for apostrophes can be constructed using ' 'regular\n' ' expressions:\n' '\n' ' >>> import re\n' ' >>> def titlecase(s):\n' ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' ' ... lambda mo: ' 'mo.group(0)[0].upper() +\n' ' ... ' 'mo.group(0)[1:].lower(),\n' ' ... s)\n' ' ...\n' ' >>> titlecase("they\'re bill\'s friends.")\n' ' "They\'re Bill\'s Friends."\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.translate(table[, deletechars])\n' '\n' ' Return a copy of the string where all characters ' 'occurring in the\n' ' optional argument *deletechars* are removed, and the ' 'remaining\n' ' characters have been mapped through the given ' 'translation table,\n' ' which must be a string of length 256.\n' '\n' ' You can use the "maketrans()" helper function in the ' '"string"\n' ' module to create a translation table. For string ' 'objects, set the\n' ' *table* argument to "None" for translations that only ' 'delete\n' ' characters:\n' '\n' " >>> 'read this short text'.translate(None, 'aeiou')\n" " 'rd ths shrt txt'\n" '\n' ' New in version 2.6: Support for a "None" *table* ' 'argument.\n' '\n' ' For Unicode objects, the "translate()" method does not ' 'accept the\n' ' optional *deletechars* argument. Instead, it returns a ' 'copy of the\n' ' *s* where all characters have been mapped through the ' 'given\n' ' translation table which must be a mapping of Unicode ' 'ordinals to\n' ' Unicode ordinals, Unicode strings or "None". Unmapped ' 'characters\n' ' are left untouched. Characters mapped to "None" are ' 'deleted. Note,\n' ' a more flexible approach is to create a custom ' 'character mapping\n' ' codec using the "codecs" module (see "encodings.cp1251" ' 'for an\n' ' example).\n' '\n' 'str.upper()\n' '\n' ' Return a copy of the string with all the cased ' 'characters [4]\n' ' converted to uppercase. Note that ' '"str.upper().isupper()" might be\n' ' "False" if "s" contains uncased characters or if the ' 'Unicode\n' ' category of the resulting character(s) is not "Lu" ' '(Letter,\n' ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.zfill(width)\n' '\n' ' Return the numeric string left filled with zeros in a ' 'string of\n' ' length *width*. A sign prefix is handled correctly. ' 'The original\n' ' string is returned if *width* is less than or equal to ' '"len(s)".\n' '\n' ' New in version 2.2.2.\n' '\n' 'The following methods are present only on unicode ' 'objects:\n' '\n' 'unicode.isnumeric()\n' '\n' ' Return "True" if there are only numeric characters in ' 'S, "False"\n' ' otherwise. Numeric characters include digit characters, ' 'and all\n' ' characters that have the Unicode numeric value ' 'property, e.g.\n' ' U+2155, VULGAR FRACTION ONE FIFTH.\n' '\n' 'unicode.isdecimal()\n' '\n' ' Return "True" if there are only decimal characters in ' 'S, "False"\n' ' otherwise. Decimal characters include digit characters, ' 'and all\n' ' characters that can be used to form decimal-radix ' 'numbers, e.g.\n' ' U+0660, ARABIC-INDIC DIGIT ZERO.\n', 'strings': '\n' 'String literals\n' '***************\n' '\n' 'String literals are described by the following lexical ' 'definitions:\n' '\n' ' stringliteral ::= [stringprefix](shortstring | longstring)\n' ' stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" ' '| "uR"\n' ' | "b" | "B" | "br" | "Br" | "bR" | "BR"\n' ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' 'shortstringitem* \'"\'\n' ' longstring ::= "\'\'\'" longstringitem* "\'\'\'"\n' ' | \'"""\' longstringitem* \'"""\'\n' ' shortstringitem ::= shortstringchar | escapeseq\n' ' longstringitem ::= longstringchar | escapeseq\n' ' shortstringchar ::= \n' ' longstringchar ::= \n' ' escapeseq ::= "\\" \n' '\n' 'One syntactic restriction not indicated by these productions is ' 'that\n' 'whitespace is not allowed between the "stringprefix" and the rest ' 'of\n' 'the string literal. The source character set is defined by the\n' 'encoding declaration; it is ASCII if no encoding declaration is ' 'given\n' 'in the source file; see section Encoding declarations.\n' '\n' 'In plain English: String literals can be enclosed in matching ' 'single\n' 'quotes ("\'") or double quotes ("""). They can also be enclosed ' 'in\n' 'matching groups of three single or double quotes (these are ' 'generally\n' 'referred to as *triple-quoted strings*). The backslash ("\\")\n' 'character is used to escape characters that otherwise have a ' 'special\n' 'meaning, such as newline, backslash itself, or the quote ' 'character.\n' 'String literals may optionally be prefixed with a letter "\'r\'" ' 'or\n' '"\'R\'"; such strings are called *raw strings* and use different ' 'rules\n' 'for interpreting backslash escape sequences. A prefix of "\'u\'" ' 'or\n' '"\'U\'" makes the string a Unicode string. Unicode strings use ' 'the\n' 'Unicode character set as defined by the Unicode Consortium and ' 'ISO\n' '10646. Some additional escape sequences, described below, are\n' 'available in Unicode strings. A prefix of "\'b\'" or "\'B\'" is ' 'ignored in\n' 'Python 2; it indicates that the literal should become a bytes ' 'literal\n' 'in Python 3 (e.g. when code is automatically converted with ' '2to3). A\n' '"\'u\'" or "\'b\'" prefix may be followed by an "\'r\'" prefix.\n' '\n' 'In triple-quoted strings, unescaped newlines and quotes are ' 'allowed\n' '(and are retained), except that three unescaped quotes in a row\n' 'terminate the string. (A "quote" is the character used to open ' 'the\n' 'string, i.e. either "\'" or """.)\n' '\n' 'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences ' 'in\n' 'strings are interpreted according to rules similar to those used ' 'by\n' 'Standard C. The recognized escape sequences are:\n' '\n' '+-------------------+-----------------------------------+---------+\n' '| Escape Sequence | Meaning | Notes ' '|\n' '+===================+===================================+=========+\n' '| "\\newline" | Ignored ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\\\" | Backslash ("\\") ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\\'" | Single quote ("\'") ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\"" | Double quote (""") ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\a" | ASCII Bell (BEL) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\b" | ASCII Backspace (BS) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\f" | ASCII Formfeed (FF) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\n" | ASCII Linefeed (LF) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\N{name}" | Character named *name* in the ' '| |\n' '| | Unicode database (Unicode only) | ' '|\n' '+-------------------+-----------------------------------+---------+\n' '| "\\r" | ASCII Carriage Return (CR) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\t" | ASCII Horizontal Tab (TAB) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\uxxxx" | Character with 16-bit hex value | ' '(1) |\n' '| | *xxxx* (Unicode only) | ' '|\n' '+-------------------+-----------------------------------+---------+\n' '| "\\Uxxxxxxxx" | Character with 32-bit hex value | ' '(2) |\n' '| | *xxxxxxxx* (Unicode only) | ' '|\n' '+-------------------+-----------------------------------+---------+\n' '| "\\v" | ASCII Vertical Tab (VT) ' '| |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\ooo" | Character with octal value *ooo* | ' '(3,5) |\n' '+-------------------+-----------------------------------+---------+\n' '| "\\xhh" | Character with hex value *hh* | ' '(4,5) |\n' '+-------------------+-----------------------------------+---------+\n' '\n' 'Notes:\n' '\n' '1. Individual code units which form parts of a surrogate pair ' 'can\n' ' be encoded using this escape sequence.\n' '\n' '2. Any Unicode character can be encoded this way, but characters\n' ' outside the Basic Multilingual Plane (BMP) will be encoded ' 'using a\n' ' surrogate pair if Python is compiled to use 16-bit code units ' '(the\n' ' default).\n' '\n' '3. As in Standard C, up to three octal digits are accepted.\n' '\n' '4. Unlike in Standard C, exactly two hex digits are required.\n' '\n' '5. In a string literal, hexadecimal and octal escapes denote the\n' ' byte with the given value; it is not necessary that the byte\n' ' encodes a character in the source character set. In a Unicode\n' ' literal, these escapes denote a Unicode character with the ' 'given\n' ' value.\n' '\n' 'Unlike Standard C, all unrecognized escape sequences are left in ' 'the\n' 'string unchanged, i.e., *the backslash is left in the string*. ' '(This\n' 'behavior is useful when debugging: if an escape sequence is ' 'mistyped,\n' 'the resulting output is more easily recognized as broken.) It is ' 'also\n' 'important to note that the escape sequences marked as "(Unicode ' 'only)"\n' 'in the table above fall into the category of unrecognized escapes ' 'for\n' 'non-Unicode string literals.\n' '\n' 'When an "\'r\'" or "\'R\'" prefix is present, a character ' 'following a\n' 'backslash is included in the string without change, and *all\n' 'backslashes are left in the string*. For example, the string ' 'literal\n' '"r"\\n"" consists of two characters: a backslash and a lowercase ' '"\'n\'".\n' 'String quotes can be escaped with a backslash, but the backslash\n' 'remains in the string; for example, "r"\\""" is a valid string ' 'literal\n' 'consisting of two characters: a backslash and a double quote; ' '"r"\\""\n' 'is not a valid string literal (even a raw string cannot end in an ' 'odd\n' 'number of backslashes). Specifically, *a raw string cannot end ' 'in a\n' 'single backslash* (since the backslash would escape the ' 'following\n' 'quote character). Note also that a single backslash followed by ' 'a\n' 'newline is interpreted as those two characters as part of the ' 'string,\n' '*not* as a line continuation.\n' '\n' 'When an "\'r\'" or "\'R\'" prefix is used in conjunction with a ' '"\'u\'" or\n' '"\'U\'" prefix, then the "\\uXXXX" and "\\UXXXXXXXX" escape ' 'sequences are\n' 'processed while *all other backslashes are left in the string*. ' 'For\n' 'example, the string literal "ur"\\u0062\\n"" consists of three ' 'Unicode\n' "characters: 'LATIN SMALL LETTER B', 'REVERSE SOLIDUS', and " "'LATIN\n" "SMALL LETTER N'. Backslashes can be escaped with a preceding\n" 'backslash; however, both remain in the string. As a result, ' '"\\uXXXX"\n' 'escape sequences are only recognized when there are an odd number ' 'of\n' 'backslashes.\n', 'subscriptions': '\n' 'Subscriptions\n' '*************\n' '\n' 'A subscription selects an item of a sequence (string, tuple ' 'or list)\n' 'or mapping (dictionary) object:\n' '\n' ' subscription ::= primary "[" expression_list "]"\n' '\n' 'The primary must evaluate to an object of a sequence or ' 'mapping type.\n' '\n' 'If the primary is a mapping, the expression list must ' 'evaluate to an\n' 'object whose value is one of the keys of the mapping, and ' 'the\n' 'subscription selects the value in the mapping that ' 'corresponds to that\n' 'key. (The expression list is a tuple except if it has ' 'exactly one\n' 'item.)\n' '\n' 'If the primary is a sequence, the expression (list) must ' 'evaluate to a\n' 'plain integer. If this value is negative, the length of ' 'the sequence\n' 'is added to it (so that, e.g., "x[-1]" selects the last ' 'item of "x".)\n' 'The resulting value must be a nonnegative integer less than ' 'the number\n' 'of items in the sequence, and the subscription selects the ' 'item whose\n' 'index is that value (counting from zero).\n' '\n' "A string's items are characters. A character is not a " 'separate data\n' 'type but a string of exactly one character.\n', 'truth': '\n' 'Truth Value Testing\n' '*******************\n' '\n' 'Any object can be tested for truth value, for use in an "if" or\n' '"while" condition or as operand of the Boolean operations below. ' 'The\n' 'following values are considered false:\n' '\n' '* "None"\n' '\n' '* "False"\n' '\n' '* zero of any numeric type, for example, "0", "0L", "0.0", "0j".\n' '\n' '* any empty sequence, for example, "\'\'", "()", "[]".\n' '\n' '* any empty mapping, for example, "{}".\n' '\n' '* instances of user-defined classes, if the class defines a\n' ' "__nonzero__()" or "__len__()" method, when that method returns ' 'the\n' ' integer zero or "bool" value "False". [1]\n' '\n' 'All other values are considered true --- so objects of many types ' 'are\n' 'always true.\n' '\n' 'Operations and built-in functions that have a Boolean result ' 'always\n' 'return "0" or "False" for false and "1" or "True" for true, unless\n' 'otherwise stated. (Important exception: the Boolean operations ' '"or"\n' 'and "and" always return one of their operands.)\n', 'try': '\n' 'The "try" statement\n' '*******************\n' '\n' 'The "try" statement specifies exception handlers and/or cleanup code\n' 'for a group of statements:\n' '\n' ' try_stmt ::= try1_stmt | try2_stmt\n' ' try1_stmt ::= "try" ":" suite\n' ' ("except" [expression [("as" | ",") identifier]] ":" ' 'suite)+\n' ' ["else" ":" suite]\n' ' ["finally" ":" suite]\n' ' try2_stmt ::= "try" ":" suite\n' ' "finally" ":" suite\n' '\n' 'Changed in version 2.5: In previous versions of Python,\n' '"try"..."except"..."finally" did not work. "try"..."except" had to ' 'be\n' 'nested in "try"..."finally".\n' '\n' 'The "except" clause(s) specify one or more exception handlers. When ' 'no\n' 'exception occurs in the "try" clause, no exception handler is\n' 'executed. When an exception occurs in the "try" suite, a search for ' 'an\n' 'exception handler is started. This search inspects the except ' 'clauses\n' 'in turn until one is found that matches the exception. An ' 'expression-\n' 'less except clause, if present, must be last; it matches any\n' 'exception. For an except clause with an expression, that expression\n' 'is evaluated, and the clause matches the exception if the resulting\n' 'object is "compatible" with the exception. An object is compatible\n' 'with an exception if it is the class or a base class of the ' 'exception\n' 'object, or a tuple containing an item compatible with the exception.\n' '\n' 'If no except clause matches the exception, the search for an ' 'exception\n' 'handler continues in the surrounding code and on the invocation ' 'stack.\n' '[1]\n' '\n' 'If the evaluation of an expression in the header of an except clause\n' 'raises an exception, the original search for a handler is canceled ' 'and\n' 'a search starts for the new exception in the surrounding code and on\n' 'the call stack (it is treated as if the entire "try" statement ' 'raised\n' 'the exception).\n' '\n' 'When a matching except clause is found, the exception is assigned to\n' 'the target specified in that except clause, if present, and the ' 'except\n' "clause's suite is executed. All except clauses must have an\n" 'executable block. When the end of this block is reached, execution\n' 'continues normally after the entire try statement. (This means that\n' 'if two nested handlers exist for the same exception, and the ' 'exception\n' 'occurs in the try clause of the inner handler, the outer handler ' 'will\n' 'not handle the exception.)\n' '\n' "Before an except clause's suite is executed, details about the\n" 'exception are assigned to three variables in the "sys" module:\n' '"sys.exc_type" receives the object identifying the exception;\n' '"sys.exc_value" receives the exception\'s parameter;\n' '"sys.exc_traceback" receives a traceback object (see section The\n' 'standard type hierarchy) identifying the point in the program where\n' 'the exception occurred. These details are also available through the\n' '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' 'exc_value, exc_traceback)". Use of the corresponding variables is\n' 'deprecated in favor of this function, since their use is unsafe in a\n' 'threaded program. As of Python 1.5, the variables are restored to\n' 'their previous values (before the call) when returning from a ' 'function\n' 'that handled an exception.\n' '\n' 'The optional "else" clause is executed if and when control flows off\n' 'the end of the "try" clause. [2] Exceptions in the "else" clause are\n' 'not handled by the preceding "except" clauses.\n' '\n' 'If "finally" is present, it specifies a \'cleanup\' handler. The ' '"try"\n' 'clause is executed, including any "except" and "else" clauses. If ' 'an\n' 'exception occurs in any of the clauses and is not handled, the\n' 'exception is temporarily saved. The "finally" clause is executed. ' 'If\n' 'there is a saved exception, it is re-raised at the end of the\n' '"finally" clause. If the "finally" clause raises another exception ' 'or\n' 'executes a "return" or "break" statement, the saved exception is\n' 'discarded:\n' '\n' ' >>> def f():\n' ' ... try:\n' ' ... 1/0\n' ' ... finally:\n' ' ... return 42\n' ' ...\n' ' >>> f()\n' ' 42\n' '\n' 'The exception information is not available to the program during\n' 'execution of the "finally" clause.\n' '\n' 'When a "return", "break" or "continue" statement is executed in the\n' '"try" suite of a "try"..."finally" statement, the "finally" clause ' 'is\n' 'also executed \'on the way out.\' A "continue" statement is illegal ' 'in\n' 'the "finally" clause. (The reason is a problem with the current\n' 'implementation --- this restriction may be lifted in the future).\n' '\n' 'The return value of a function is determined by the last "return"\n' 'statement executed. Since the "finally" clause always executes, a\n' '"return" statement executed in the "finally" clause will always be ' 'the\n' 'last one executed:\n' '\n' ' >>> def foo():\n' ' ... try:\n' " ... return 'try'\n" ' ... finally:\n' " ... return 'finally'\n" ' ...\n' ' >>> foo()\n' " 'finally'\n" '\n' 'Additional information on exceptions can be found in section\n' 'Exceptions, and information on using the "raise" statement to ' 'generate\n' 'exceptions may be found in section The raise statement.\n', 'types': '\n' 'The standard type hierarchy\n' '***************************\n' '\n' 'Below is a list of the types that are built into Python. ' 'Extension\n' 'modules (written in C, Java, or other languages, depending on the\n' 'implementation) can define additional types. Future versions of\n' 'Python may add types to the type hierarchy (e.g., rational ' 'numbers,\n' 'efficiently stored arrays of integers, etc.).\n' '\n' 'Some of the type descriptions below contain a paragraph listing\n' "'special attributes.' These are attributes that provide access to " 'the\n' 'implementation and are not intended for general use. Their ' 'definition\n' 'may change in the future.\n' '\n' 'None\n' ' This type has a single value. There is a single object with ' 'this\n' ' value. This object is accessed through the built-in name "None". ' 'It\n' ' is used to signify the absence of a value in many situations, ' 'e.g.,\n' " it is returned from functions that don't explicitly return\n" ' anything. Its truth value is false.\n' '\n' 'NotImplemented\n' ' This type has a single value. There is a single object with ' 'this\n' ' value. This object is accessed through the built-in name\n' ' "NotImplemented". Numeric methods and rich comparison methods ' 'may\n' ' return this value if they do not implement the operation for ' 'the\n' ' operands provided. (The interpreter will then try the ' 'reflected\n' ' operation, or some other fallback, depending on the operator.) ' 'Its\n' ' truth value is true.\n' '\n' 'Ellipsis\n' ' This type has a single value. There is a single object with ' 'this\n' ' value. This object is accessed through the built-in name\n' ' "Ellipsis". It is used to indicate the presence of the "..." ' 'syntax\n' ' in a slice. Its truth value is true.\n' '\n' '"numbers.Number"\n' ' These are created by numeric literals and returned as results ' 'by\n' ' arithmetic operators and arithmetic built-in functions. ' 'Numeric\n' ' objects are immutable; once created their value never changes.\n' ' Python numbers are of course strongly related to mathematical\n' ' numbers, but subject to the limitations of numerical ' 'representation\n' ' in computers.\n' '\n' ' Python distinguishes between integers, floating point numbers, ' 'and\n' ' complex numbers:\n' '\n' ' "numbers.Integral"\n' ' These represent elements from the mathematical set of ' 'integers\n' ' (positive and negative).\n' '\n' ' There are three types of integers:\n' '\n' ' Plain integers\n' ' These represent numbers in the range -2147483648 through\n' ' 2147483647. (The range may be larger on machines with a\n' ' larger natural word size, but not smaller.) When the ' 'result\n' ' of an operation would fall outside this range, the result ' 'is\n' ' normally returned as a long integer (in some cases, the\n' ' exception "OverflowError" is raised instead). For the\n' ' purpose of shift and mask operations, integers are assumed ' 'to\n' " have a binary, 2's complement notation using 32 or more " 'bits,\n' ' and hiding no bits from the user (i.e., all 4294967296\n' ' different bit patterns correspond to different values).\n' '\n' ' Long integers\n' ' These represent numbers in an unlimited range, subject to\n' ' available (virtual) memory only. For the purpose of ' 'shift\n' ' and mask operations, a binary representation is assumed, ' 'and\n' " negative numbers are represented in a variant of 2's\n" ' complement which gives the illusion of an infinite string ' 'of\n' ' sign bits extending to the left.\n' '\n' ' Booleans\n' ' These represent the truth values False and True. The two\n' ' objects representing the values "False" and "True" are ' 'the\n' ' only Boolean objects. The Boolean type is a subtype of ' 'plain\n' ' integers, and Boolean values behave like the values 0 and ' '1,\n' ' respectively, in almost all contexts, the exception being\n' ' that when converted to a string, the strings ""False"" or\n' ' ""True"" are returned, respectively.\n' '\n' ' The rules for integer representation are intended to give ' 'the\n' ' most meaningful interpretation of shift and mask operations\n' ' involving negative integers and the least surprises when\n' ' switching between the plain and long integer domains. Any\n' ' operation, if it yields a result in the plain integer ' 'domain,\n' ' will yield the same result in the long integer domain or ' 'when\n' ' using mixed operands. The switch between domains is ' 'transparent\n' ' to the programmer.\n' '\n' ' "numbers.Real" ("float")\n' ' These represent machine-level double precision floating ' 'point\n' ' numbers. You are at the mercy of the underlying machine\n' ' architecture (and C or Java implementation) for the accepted\n' ' range and handling of overflow. Python does not support ' 'single-\n' ' precision floating point numbers; the savings in processor ' 'and\n' ' memory usage that are usually the reason for using these are\n' ' dwarfed by the overhead of using objects in Python, so there ' 'is\n' ' no reason to complicate the language with two kinds of ' 'floating\n' ' point numbers.\n' '\n' ' "numbers.Complex"\n' ' These represent complex numbers as a pair of machine-level\n' ' double precision floating point numbers. The same caveats ' 'apply\n' ' as for floating point numbers. The real and imaginary parts ' 'of a\n' ' complex number "z" can be retrieved through the read-only\n' ' attributes "z.real" and "z.imag".\n' '\n' 'Sequences\n' ' These represent finite ordered sets indexed by non-negative\n' ' numbers. The built-in function "len()" returns the number of ' 'items\n' ' of a sequence. When the length of a sequence is *n*, the index ' 'set\n' ' contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* ' 'is\n' ' selected by "a[i]".\n' '\n' ' Sequences also support slicing: "a[i:j]" selects all items with\n' ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n' ' expression, a slice is a sequence of the same type. This ' 'implies\n' ' that the index set is renumbered so that it starts at 0.\n' '\n' ' Some sequences also support "extended slicing" with a third ' '"step"\n' ' parameter: "a[i:j:k]" selects all items of *a* with index *x* ' 'where\n' ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' '\n' ' Sequences are distinguished according to their mutability:\n' '\n' ' Immutable sequences\n' ' An object of an immutable sequence type cannot change once it ' 'is\n' ' created. (If the object contains references to other ' 'objects,\n' ' these other objects may be mutable and may be changed; ' 'however,\n' ' the collection of objects directly referenced by an ' 'immutable\n' ' object cannot change.)\n' '\n' ' The following types are immutable sequences:\n' '\n' ' Strings\n' ' The items of a string are characters. There is no ' 'separate\n' ' character type; a character is represented by a string of ' 'one\n' ' item. Characters represent (at least) 8-bit bytes. The\n' ' built-in functions "chr()" and "ord()" convert between\n' ' characters and nonnegative integers representing the byte\n' ' values. Bytes with the values 0--127 usually represent ' 'the\n' ' corresponding ASCII values, but the interpretation of ' 'values\n' ' is up to the program. The string data type is also used ' 'to\n' ' represent arrays of bytes, e.g., to hold data read from a\n' ' file.\n' '\n' ' (On systems whose native character set is not ASCII, ' 'strings\n' ' may use EBCDIC in their internal representation, provided ' 'the\n' ' functions "chr()" and "ord()" implement a mapping between\n' ' ASCII and EBCDIC, and string comparison preserves the ' 'ASCII\n' ' order. Or perhaps someone can propose a better rule?)\n' '\n' ' Unicode\n' ' The items of a Unicode object are Unicode code units. A\n' ' Unicode code unit is represented by a Unicode object of ' 'one\n' ' item and can hold either a 16-bit or 32-bit value\n' ' representing a Unicode ordinal (the maximum value for the\n' ' ordinal is given in "sys.maxunicode", and depends on how\n' ' Python is configured at compile time). Surrogate pairs ' 'may\n' ' be present in the Unicode object, and will be reported as ' 'two\n' ' separate items. The built-in functions "unichr()" and\n' ' "ord()" convert between code units and nonnegative ' 'integers\n' ' representing the Unicode ordinals as defined in the ' 'Unicode\n' ' Standard 3.0. Conversion from and to other encodings are\n' ' possible through the Unicode method "encode()" and the ' 'built-\n' ' in function "unicode()".\n' '\n' ' Tuples\n' ' The items of a tuple are arbitrary Python objects. Tuples ' 'of\n' ' two or more items are formed by comma-separated lists of\n' " expressions. A tuple of one item (a 'singleton') can be\n" ' formed by affixing a comma to an expression (an expression ' 'by\n' ' itself does not create a tuple, since parentheses must be\n' ' usable for grouping of expressions). An empty tuple can ' 'be\n' ' formed by an empty pair of parentheses.\n' '\n' ' Mutable sequences\n' ' Mutable sequences can be changed after they are created. ' 'The\n' ' subscription and slicing notations can be used as the target ' 'of\n' ' assignment and "del" (delete) statements.\n' '\n' ' There are currently two intrinsic mutable sequence types:\n' '\n' ' Lists\n' ' The items of a list are arbitrary Python objects. Lists ' 'are\n' ' formed by placing a comma-separated list of expressions ' 'in\n' ' square brackets. (Note that there are no special cases ' 'needed\n' ' to form lists of length 0 or 1.)\n' '\n' ' Byte Arrays\n' ' A bytearray object is a mutable array. They are created ' 'by\n' ' the built-in "bytearray()" constructor. Aside from being\n' ' mutable (and hence unhashable), byte arrays otherwise ' 'provide\n' ' the same interface and functionality as immutable bytes\n' ' objects.\n' '\n' ' The extension module "array" provides an additional example ' 'of a\n' ' mutable sequence type.\n' '\n' 'Set types\n' ' These represent unordered, finite sets of unique, immutable\n' ' objects. As such, they cannot be indexed by any subscript. ' 'However,\n' ' they can be iterated over, and the built-in function "len()"\n' ' returns the number of items in a set. Common uses for sets are ' 'fast\n' ' membership testing, removing duplicates from a sequence, and\n' ' computing mathematical operations such as intersection, union,\n' ' difference, and symmetric difference.\n' '\n' ' For set elements, the same immutability rules apply as for\n' ' dictionary keys. Note that numeric types obey the normal rules ' 'for\n' ' numeric comparison: if two numbers compare equal (e.g., "1" and\n' ' "1.0"), only one of them can be contained in a set.\n' '\n' ' There are currently two intrinsic set types:\n' '\n' ' Sets\n' ' These represent a mutable set. They are created by the ' 'built-in\n' ' "set()" constructor and can be modified afterwards by ' 'several\n' ' methods, such as "add()".\n' '\n' ' Frozen sets\n' ' These represent an immutable set. They are created by the\n' ' built-in "frozenset()" constructor. As a frozenset is ' 'immutable\n' ' and *hashable*, it can be used again as an element of ' 'another\n' ' set, or as a dictionary key.\n' '\n' 'Mappings\n' ' These represent finite sets of objects indexed by arbitrary ' 'index\n' ' sets. The subscript notation "a[k]" selects the item indexed by ' '"k"\n' ' from the mapping "a"; this can be used in expressions and as ' 'the\n' ' target of assignments or "del" statements. The built-in ' 'function\n' ' "len()" returns the number of items in a mapping.\n' '\n' ' There is currently a single intrinsic mapping type:\n' '\n' ' Dictionaries\n' ' These represent finite sets of objects indexed by nearly\n' ' arbitrary values. The only types of values not acceptable ' 'as\n' ' keys are values containing lists or dictionaries or other\n' ' mutable types that are compared by value rather than by ' 'object\n' ' identity, the reason being that the efficient implementation ' 'of\n' " dictionaries requires a key's hash value to remain constant.\n" ' Numeric types used for keys obey the normal rules for ' 'numeric\n' ' comparison: if two numbers compare equal (e.g., "1" and ' '"1.0")\n' ' then they can be used interchangeably to index the same\n' ' dictionary entry.\n' '\n' ' Dictionaries are mutable; they can be created by the "{...}"\n' ' notation (see section Dictionary displays).\n' '\n' ' The extension modules "dbm", "gdbm", and "bsddb" provide\n' ' additional examples of mapping types.\n' '\n' 'Callable types\n' ' These are the types to which the function call operation (see\n' ' section Calls) can be applied:\n' '\n' ' User-defined functions\n' ' A user-defined function object is created by a function\n' ' definition (see section Function definitions). It should be\n' ' called with an argument list containing the same number of ' 'items\n' " as the function's formal parameter list.\n" '\n' ' Special attributes:\n' '\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | Attribute | Meaning ' '| |\n' ' ' '+=========================+=================================+=============+\n' ' | "__doc__" "func_doc" | The function\'s documentation ' '| Writable |\n' ' | | string, or "None" if ' '| |\n' ' | | unavailable. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__name__" "func_name" | The function\'s name ' '| Writable |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__module__" | The name of the module the | ' 'Writable |\n' ' | | function was defined in, or ' '| |\n' ' | | "None" if unavailable. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__defaults__" | A tuple containing default | ' 'Writable |\n' ' | "func_defaults" | argument values for those ' '| |\n' ' | | arguments that have defaults, ' '| |\n' ' | | or "None" if no arguments have ' '| |\n' ' | | a default value. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__code__" "func_code" | The code object representing | ' 'Writable |\n' ' | | the compiled function body. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__globals__" | A reference to the dictionary | ' 'Read-only |\n' ' | "func_globals" | that holds the function\'s ' '| |\n' ' | | global variables --- the global ' '| |\n' ' | | namespace of the module in ' '| |\n' ' | | which the function was defined. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__dict__" "func_dict" | The namespace supporting | ' 'Writable |\n' ' | | arbitrary function attributes. ' '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' ' | "__closure__" | "None" or a tuple of cells that | ' 'Read-only |\n' ' | "func_closure" | contain bindings for the ' '| |\n' " | | function's free variables. " '| |\n' ' ' '+-------------------------+---------------------------------+-------------+\n' '\n' ' Most of the attributes labelled "Writable" check the type of ' 'the\n' ' assigned value.\n' '\n' ' Changed in version 2.4: "func_name" is now writable.\n' '\n' ' Changed in version 2.6: The double-underscore attributes\n' ' "__closure__", "__code__", "__defaults__", and "__globals__"\n' ' were introduced as aliases for the corresponding "func_*"\n' ' attributes for forwards compatibility with Python 3.\n' '\n' ' Function objects also support getting and setting arbitrary\n' ' attributes, which can be used, for example, to attach ' 'metadata\n' ' to functions. Regular attribute dot-notation is used to get ' 'and\n' ' set such attributes. *Note that the current implementation ' 'only\n' ' supports function attributes on user-defined functions. ' 'Function\n' ' attributes on built-in functions may be supported in the\n' ' future.*\n' '\n' " Additional information about a function's definition can be\n" ' retrieved from its code object; see the description of ' 'internal\n' ' types below.\n' '\n' ' User-defined methods\n' ' A user-defined method object combines a class, a class ' 'instance\n' ' (or "None") and any callable object (normally a user-defined\n' ' function).\n' '\n' ' Special read-only attributes: "im_self" is the class ' 'instance\n' ' object, "im_func" is the function object; "im_class" is the\n' ' class of "im_self" for bound methods or the class that asked ' 'for\n' ' the method for unbound methods; "__doc__" is the method\'s\n' ' documentation (same as "im_func.__doc__"); "__name__" is the\n' ' method name (same as "im_func.__name__"); "__module__" is ' 'the\n' ' name of the module the method was defined in, or "None" if\n' ' unavailable.\n' '\n' ' Changed in version 2.2: "im_self" used to refer to the class\n' ' that defined the method.\n' '\n' ' Changed in version 2.6: For Python 3 forward-compatibility,\n' ' "im_func" is also available as "__func__", and "im_self" as\n' ' "__self__".\n' '\n' ' Methods also support accessing (but not setting) the ' 'arbitrary\n' ' function attributes on the underlying function object.\n' '\n' ' User-defined method objects may be created when getting an\n' ' attribute of a class (perhaps via an instance of that class), ' 'if\n' ' that attribute is a user-defined function object, an unbound\n' ' user-defined method object, or a class method object. When ' 'the\n' ' attribute is a user-defined method object, a new method ' 'object\n' ' is only created if the class from which it is being retrieved ' 'is\n' ' the same as, or a derived class of, the class stored in the\n' ' original method object; otherwise, the original method object ' 'is\n' ' used as it is.\n' '\n' ' When a user-defined method object is created by retrieving a\n' ' user-defined function object from a class, its "im_self"\n' ' attribute is "None" and the method object is said to be ' 'unbound.\n' ' When one is created by retrieving a user-defined function ' 'object\n' ' from a class via one of its instances, its "im_self" ' 'attribute\n' ' is the instance, and the method object is said to be bound. ' 'In\n' ' either case, the new method\'s "im_class" attribute is the ' 'class\n' ' from which the retrieval takes place, and its "im_func"\n' ' attribute is the original function object.\n' '\n' ' When a user-defined method object is created by retrieving\n' ' another method object from a class or instance, the behaviour ' 'is\n' ' the same as for a function object, except that the "im_func"\n' ' attribute of the new instance is not the original method ' 'object\n' ' but its "im_func" attribute.\n' '\n' ' When a user-defined method object is created by retrieving a\n' ' class method object from a class or instance, its "im_self"\n' ' attribute is the class itself, and its "im_func" attribute ' 'is\n' ' the function object underlying the class method.\n' '\n' ' When an unbound user-defined method object is called, the\n' ' underlying function ("im_func") is called, with the ' 'restriction\n' ' that the first argument must be an instance of the proper ' 'class\n' ' ("im_class") or of a derived class thereof.\n' '\n' ' When a bound user-defined method object is called, the\n' ' underlying function ("im_func") is called, inserting the ' 'class\n' ' instance ("im_self") in front of the argument list. For\n' ' instance, when "C" is a class which contains a definition for ' 'a\n' ' function "f()", and "x" is an instance of "C", calling ' '"x.f(1)"\n' ' is equivalent to calling "C.f(x, 1)".\n' '\n' ' When a user-defined method object is derived from a class ' 'method\n' ' object, the "class instance" stored in "im_self" will ' 'actually\n' ' be the class itself, so that calling either "x.f(1)" or ' '"C.f(1)"\n' ' is equivalent to calling "f(C,1)" where "f" is the ' 'underlying\n' ' function.\n' '\n' ' Note that the transformation from function object to (unbound ' 'or\n' ' bound) method object happens each time the attribute is\n' ' retrieved from the class or instance. In some cases, a ' 'fruitful\n' ' optimization is to assign the attribute to a local variable ' 'and\n' ' call that local variable. Also notice that this ' 'transformation\n' ' only happens for user-defined functions; other callable ' 'objects\n' ' (and all non-callable objects) are retrieved without\n' ' transformation. It is also important to note that ' 'user-defined\n' ' functions which are attributes of a class instance are not\n' ' converted to bound methods; this *only* happens when the\n' ' function is an attribute of the class.\n' '\n' ' Generator functions\n' ' A function or method which uses the "yield" statement (see\n' ' section The yield statement) is called a *generator ' 'function*.\n' ' Such a function, when called, always returns an iterator ' 'object\n' ' which can be used to execute the body of the function: ' 'calling\n' ' the iterator\'s "next()" method will cause the function to\n' ' execute until it provides a value using the "yield" ' 'statement.\n' ' When the function executes a "return" statement or falls off ' 'the\n' ' end, a "StopIteration" exception is raised and the iterator ' 'will\n' ' have reached the end of the set of values to be returned.\n' '\n' ' Built-in functions\n' ' A built-in function object is a wrapper around a C function.\n' ' Examples of built-in functions are "len()" and "math.sin()"\n' ' ("math" is a standard built-in module). The number and type ' 'of\n' ' the arguments are determined by the C function. Special ' 'read-\n' ' only attributes: "__doc__" is the function\'s documentation\n' ' string, or "None" if unavailable; "__name__" is the ' "function's\n" ' name; "__self__" is set to "None" (but see the next item);\n' ' "__module__" is the name of the module the function was ' 'defined\n' ' in or "None" if unavailable.\n' '\n' ' Built-in methods\n' ' This is really a different disguise of a built-in function, ' 'this\n' ' time containing an object passed to the C function as an\n' ' implicit extra argument. An example of a built-in method is\n' ' "alist.append()", assuming *alist* is a list object. In this\n' ' case, the special read-only attribute "__self__" is set to ' 'the\n' ' object denoted by *alist*.\n' '\n' ' Class Types\n' ' Class types, or "new-style classes," are callable. These\n' ' objects normally act as factories for new instances of\n' ' themselves, but variations are possible for class types that\n' ' override "__new__()". The arguments of the call are passed ' 'to\n' ' "__new__()" and, in the typical case, to "__init__()" to\n' ' initialize the new instance.\n' '\n' ' Classic Classes\n' ' Class objects are described below. When a class object is\n' ' called, a new class instance (also described below) is ' 'created\n' " and returned. This implies a call to the class's " '"__init__()"\n' ' method if it has one. Any arguments are passed on to the\n' ' "__init__()" method. If there is no "__init__()" method, ' 'the\n' ' class must be called without arguments.\n' '\n' ' Class instances\n' ' Class instances are described below. Class instances are\n' ' callable only when the class has a "__call__()" method;\n' ' "x(arguments)" is a shorthand for "x.__call__(arguments)".\n' '\n' 'Modules\n' ' Modules are imported by the "import" statement (see section The\n' ' import statement). A module object has a namespace implemented ' 'by a\n' ' dictionary object (this is the dictionary referenced by the\n' ' func_globals attribute of functions defined in the module).\n' ' Attribute references are translated to lookups in this ' 'dictionary,\n' ' e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object\n' ' does not contain the code object used to initialize the module\n' " (since it isn't needed once the initialization is done).\n" '\n' " Attribute assignment updates the module's namespace dictionary,\n" ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' '\n' ' Special read-only attribute: "__dict__" is the module\'s ' 'namespace\n' ' as a dictionary object.\n' '\n' ' **CPython implementation detail:** Because of the way CPython\n' ' clears module dictionaries, the module dictionary will be ' 'cleared\n' ' when the module falls out of scope even if the dictionary still ' 'has\n' ' live references. To avoid this, copy the dictionary or keep ' 'the\n' ' module around while using its dictionary directly.\n' '\n' ' Predefined (writable) attributes: "__name__" is the module\'s ' 'name;\n' ' "__doc__" is the module\'s documentation string, or "None" if\n' ' unavailable; "__file__" is the pathname of the file from which ' 'the\n' ' module was loaded, if it was loaded from a file. The "__file__"\n' ' attribute is not present for C modules that are statically ' 'linked\n' ' into the interpreter; for extension modules loaded dynamically ' 'from\n' ' a shared library, it is the pathname of the shared library ' 'file.\n' '\n' 'Classes\n' ' Both class types (new-style classes) and class objects (old-\n' ' style/classic classes) are typically created by class ' 'definitions\n' ' (see section Class definitions). A class has a namespace\n' ' implemented by a dictionary object. Class attribute references ' 'are\n' ' translated to lookups in this dictionary, e.g., "C.x" is ' 'translated\n' ' to "C.__dict__["x"]" (although for new-style classes in ' 'particular\n' ' there are a number of hooks which allow for other means of ' 'locating\n' ' attributes). When the attribute name is not found there, the\n' ' attribute search continues in the base classes. For old-style\n' ' classes, the search is depth-first, left-to-right in the order ' 'of\n' ' occurrence in the base class list. New-style classes use the ' 'more\n' ' complex C3 method resolution order which behaves correctly even ' 'in\n' " the presence of 'diamond' inheritance structures where there " 'are\n' ' multiple inheritance paths leading back to a common ancestor.\n' ' Additional details on the C3 MRO used by new-style classes can ' 'be\n' ' found in the documentation accompanying the 2.3 release at\n' ' https://www.python.org/download/releases/2.3/mro/.\n' '\n' ' When a class attribute reference (for class "C", say) would ' 'yield a\n' ' user-defined function object or an unbound user-defined method\n' ' object whose associated class is either "C" or one of its base\n' ' classes, it is transformed into an unbound user-defined method\n' ' object whose "im_class" attribute is "C". When it would yield a\n' ' class method object, it is transformed into a bound ' 'user-defined\n' ' method object whose "im_self" attribute is "C". When it would\n' ' yield a static method object, it is transformed into the object\n' ' wrapped by the static method object. See section Implementing\n' ' Descriptors for another way in which attributes retrieved from ' 'a\n' ' class may differ from those actually contained in its ' '"__dict__"\n' ' (note that only new-style classes support descriptors).\n' '\n' " Class attribute assignments update the class's dictionary, " 'never\n' ' the dictionary of a base class.\n' '\n' ' A class object can be called (see above) to yield a class ' 'instance\n' ' (see below).\n' '\n' ' Special attributes: "__name__" is the class name; "__module__" ' 'is\n' ' the module name in which the class was defined; "__dict__" is ' 'the\n' ' dictionary containing the class\'s namespace; "__bases__" is a ' 'tuple\n' ' (possibly empty or a singleton) containing the base classes, in ' 'the\n' ' order of their occurrence in the base class list; "__doc__" is ' 'the\n' ' class\'s documentation string, or "None" if undefined.\n' '\n' 'Class instances\n' ' A class instance is created by calling a class object (see ' 'above).\n' ' A class instance has a namespace implemented as a dictionary ' 'which\n' ' is the first place in which attribute references are searched.\n' " When an attribute is not found there, and the instance's class " 'has\n' ' an attribute by that name, the search continues with the class\n' ' attributes. If a class attribute is found that is a ' 'user-defined\n' ' function object or an unbound user-defined method object whose\n' ' associated class is the class (call it "C") of the instance for\n' ' which the attribute reference was initiated or one of its bases, ' 'it\n' ' is transformed into a bound user-defined method object whose\n' ' "im_class" attribute is "C" and whose "im_self" attribute is ' 'the\n' ' instance. Static method and class method objects are also\n' ' transformed, as if they had been retrieved from class "C"; see\n' ' above under "Classes". See section Implementing Descriptors for\n' ' another way in which attributes of a class retrieved via its\n' ' instances may differ from the objects actually stored in the\n' ' class\'s "__dict__". If no class attribute is found, and the\n' ' object\'s class has a "__getattr__()" method, that is called to\n' ' satisfy the lookup.\n' '\n' " Attribute assignments and deletions update the instance's\n" " dictionary, never a class's dictionary. If the class has a\n" ' "__setattr__()" or "__delattr__()" method, this is called ' 'instead\n' ' of updating the instance dictionary directly.\n' '\n' ' Class instances can pretend to be numbers, sequences, or ' 'mappings\n' ' if they have methods with certain special names. See section\n' ' Special method names.\n' '\n' ' Special attributes: "__dict__" is the attribute dictionary;\n' ' "__class__" is the instance\'s class.\n' '\n' 'Files\n' ' A file object represents an open file. File objects are created ' 'by\n' ' the "open()" built-in function, and also by "os.popen()",\n' ' "os.fdopen()", and the "makefile()" method of socket objects ' '(and\n' ' perhaps by other functions or methods provided by extension\n' ' modules). The objects "sys.stdin", "sys.stdout" and ' '"sys.stderr"\n' ' are initialized to file objects corresponding to the ' "interpreter's\n" ' standard input, output and error streams. See File Objects for\n' ' complete documentation of file objects.\n' '\n' 'Internal types\n' ' A few types used internally by the interpreter are exposed to ' 'the\n' ' user. Their definitions may change with future versions of the\n' ' interpreter, but they are mentioned here for completeness.\n' '\n' ' Code objects\n' ' Code objects represent *byte-compiled* executable Python ' 'code,\n' ' or *bytecode*. The difference between a code object and a\n' ' function object is that the function object contains an ' 'explicit\n' " reference to the function's globals (the module in which it " 'was\n' ' defined), while a code object contains no context; also the\n' ' default argument values are stored in the function object, ' 'not\n' ' in the code object (because they represent values calculated ' 'at\n' ' run-time). Unlike function objects, code objects are ' 'immutable\n' ' and contain no references (directly or indirectly) to ' 'mutable\n' ' objects.\n' '\n' ' Special read-only attributes: "co_name" gives the function ' 'name;\n' ' "co_argcount" is the number of positional arguments ' '(including\n' ' arguments with default values); "co_nlocals" is the number ' 'of\n' ' local variables used by the function (including arguments);\n' ' "co_varnames" is a tuple containing the names of the local\n' ' variables (starting with the argument names); "co_cellvars" ' 'is a\n' ' tuple containing the names of local variables that are\n' ' referenced by nested functions; "co_freevars" is a tuple\n' ' containing the names of free variables; "co_code" is a ' 'string\n' ' representing the sequence of bytecode instructions; ' '"co_consts"\n' ' is a tuple containing the literals used by the bytecode;\n' ' "co_names" is a tuple containing the names used by the ' 'bytecode;\n' ' "co_filename" is the filename from which the code was ' 'compiled;\n' ' "co_firstlineno" is the first line number of the function;\n' ' "co_lnotab" is a string encoding the mapping from bytecode\n' ' offsets to line numbers (for details see the source code of ' 'the\n' ' interpreter); "co_stacksize" is the required stack size\n' ' (including local variables); "co_flags" is an integer ' 'encoding a\n' ' number of flags for the interpreter.\n' '\n' ' The following flag bits are defined for "co_flags": bit ' '"0x04"\n' ' is set if the function uses the "*arguments" syntax to accept ' 'an\n' ' arbitrary number of positional arguments; bit "0x08" is set ' 'if\n' ' the function uses the "**keywords" syntax to accept ' 'arbitrary\n' ' keyword arguments; bit "0x20" is set if the function is a\n' ' generator.\n' '\n' ' Future feature declarations ("from __future__ import ' 'division")\n' ' also use bits in "co_flags" to indicate whether a code ' 'object\n' ' was compiled with a particular feature enabled: bit "0x2000" ' 'is\n' ' set if the function was compiled with future division ' 'enabled;\n' ' bits "0x10" and "0x1000" were used in earlier versions of\n' ' Python.\n' '\n' ' Other bits in "co_flags" are reserved for internal use.\n' '\n' ' If a code object represents a function, the first item in\n' ' "co_consts" is the documentation string of the function, or\n' ' "None" if undefined.\n' '\n' ' Frame objects\n' ' Frame objects represent execution frames. They may occur in\n' ' traceback objects (see below).\n' '\n' ' Special read-only attributes: "f_back" is to the previous ' 'stack\n' ' frame (towards the caller), or "None" if this is the bottom\n' ' stack frame; "f_code" is the code object being executed in ' 'this\n' ' frame; "f_locals" is the dictionary used to look up local\n' ' variables; "f_globals" is used for global variables;\n' ' "f_builtins" is used for built-in (intrinsic) names;\n' ' "f_restricted" is a flag indicating whether the function is\n' ' executing in restricted execution mode; "f_lasti" gives the\n' ' precise instruction (this is an index into the bytecode ' 'string\n' ' of the code object).\n' '\n' ' Special writable attributes: "f_trace", if not "None", is a\n' ' function called at the start of each source code line (this ' 'is\n' ' used by the debugger); "f_exc_type", "f_exc_value",\n' ' "f_exc_traceback" represent the last exception raised in the\n' ' parent frame provided another exception was ever raised in ' 'the\n' ' current frame (in all other cases they are "None"); ' '"f_lineno"\n' ' is the current line number of the frame --- writing to this ' 'from\n' ' within a trace function jumps to the given line (only for ' 'the\n' ' bottom-most frame). A debugger can implement a Jump command\n' ' (aka Set Next Statement) by writing to f_lineno.\n' '\n' ' Traceback objects\n' ' Traceback objects represent a stack trace of an exception. ' 'A\n' ' traceback object is created when an exception occurs. When ' 'the\n' ' search for an exception handler unwinds the execution stack, ' 'at\n' ' each unwound level a traceback object is inserted in front ' 'of\n' ' the current traceback. When an exception handler is ' 'entered,\n' ' the stack trace is made available to the program. (See ' 'section\n' ' The try statement.) It is accessible as "sys.exc_traceback", ' 'and\n' ' also as the third item of the tuple returned by\n' ' "sys.exc_info()". The latter is the preferred interface, ' 'since\n' ' it works correctly when the program is using multiple ' 'threads.\n' ' When the program contains no suitable handler, the stack ' 'trace\n' ' is written (nicely formatted) to the standard error stream; ' 'if\n' ' the interpreter is interactive, it is also made available to ' 'the\n' ' user as "sys.last_traceback".\n' '\n' ' Special read-only attributes: "tb_next" is the next level in ' 'the\n' ' stack trace (towards the frame where the exception occurred), ' 'or\n' ' "None" if there is no next level; "tb_frame" points to the\n' ' execution frame of the current level; "tb_lineno" gives the ' 'line\n' ' number where the exception occurred; "tb_lasti" indicates ' 'the\n' ' precise instruction. The line number and last instruction ' 'in\n' ' the traceback may differ from the line number of its frame\n' ' object if the exception occurred in a "try" statement with ' 'no\n' ' matching except clause or with a finally clause.\n' '\n' ' Slice objects\n' ' Slice objects are used to represent slices when *extended ' 'slice\n' ' syntax* is used. This is a slice using two colons, or ' 'multiple\n' ' slices or ellipses separated by commas, e.g., "a[i:j:step]",\n' ' "a[i:j, k:l]", or "a[..., i:j]". They are also created by ' 'the\n' ' built-in "slice()" function.\n' '\n' ' Special read-only attributes: "start" is the lower bound; ' '"stop"\n' ' is the upper bound; "step" is the step value; each is "None" ' 'if\n' ' omitted. These attributes can have any type.\n' '\n' ' Slice objects support one method:\n' '\n' ' slice.indices(self, length)\n' '\n' ' This method takes a single integer argument *length* and\n' ' computes information about the extended slice that the ' 'slice\n' ' object would describe if applied to a sequence of ' '*length*\n' ' items. It returns a tuple of three integers; ' 'respectively\n' ' these are the *start* and *stop* indices and the *step* ' 'or\n' ' stride length of the slice. Missing or out-of-bounds ' 'indices\n' ' are handled in a manner consistent with regular slices.\n' '\n' ' New in version 2.3.\n' '\n' ' Static method objects\n' ' Static method objects provide a way of defeating the\n' ' transformation of function objects to method objects ' 'described\n' ' above. A static method object is a wrapper around any other\n' ' object, usually a user-defined method object. When a static\n' ' method object is retrieved from a class or a class instance, ' 'the\n' ' object actually returned is the wrapped object, which is not\n' ' subject to any further transformation. Static method objects ' 'are\n' ' not themselves callable, although the objects they wrap ' 'usually\n' ' are. Static method objects are created by the built-in\n' ' "staticmethod()" constructor.\n' '\n' ' Class method objects\n' ' A class method object, like a static method object, is a ' 'wrapper\n' ' around another object that alters the way in which that ' 'object\n' ' is retrieved from classes and class instances. The behaviour ' 'of\n' ' class method objects upon such retrieval is described above,\n' ' under "User-defined methods". Class method objects are ' 'created\n' ' by the built-in "classmethod()" constructor.\n', 'typesfunctions': '\n' 'Functions\n' '*********\n' '\n' 'Function objects are created by function definitions. The ' 'only\n' 'operation on a function object is to call it: ' '"func(argument-list)".\n' '\n' 'There are really two flavors of function objects: built-in ' 'functions\n' 'and user-defined functions. Both support the same ' 'operation (to call\n' 'the function), but the implementation is different, hence ' 'the\n' 'different object types.\n' '\n' 'See Function definitions for more information.\n', 'typesmapping': '\n' 'Mapping Types --- "dict"\n' '************************\n' '\n' 'A *mapping* object maps *hashable* values to arbitrary ' 'objects.\n' 'Mappings are mutable objects. There is currently only one ' 'standard\n' 'mapping type, the *dictionary*. (For other containers see ' 'the built\n' 'in "list", "set", and "tuple" classes, and the "collections" ' 'module.)\n' '\n' "A dictionary's keys are *almost* arbitrary values. Values " 'that are\n' 'not *hashable*, that is, values containing lists, ' 'dictionaries or\n' 'other mutable types (that are compared by value rather than ' 'by object\n' 'identity) may not be used as keys. Numeric types used for ' 'keys obey\n' 'the normal rules for numeric comparison: if two numbers ' 'compare equal\n' '(such as "1" and "1.0") then they can be used ' 'interchangeably to index\n' 'the same dictionary entry. (Note however, that since ' 'computers store\n' 'floating-point numbers as approximations it is usually ' 'unwise to use\n' 'them as dictionary keys.)\n' '\n' 'Dictionaries can be created by placing a comma-separated ' 'list of "key:\n' 'value" pairs within braces, for example: "{\'jack\': 4098, ' "'sjoerd':\n" '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the ' '"dict"\n' 'constructor.\n' '\n' 'class dict(**kwarg)\n' 'class dict(mapping, **kwarg)\n' 'class dict(iterable, **kwarg)\n' '\n' ' Return a new dictionary initialized from an optional ' 'positional\n' ' argument and a possibly empty set of keyword arguments.\n' '\n' ' If no positional argument is given, an empty dictionary ' 'is created.\n' ' If a positional argument is given and it is a mapping ' 'object, a\n' ' dictionary is created with the same key-value pairs as ' 'the mapping\n' ' object. Otherwise, the positional argument must be an ' '*iterable*\n' ' object. Each item in the iterable must itself be an ' 'iterable with\n' ' exactly two objects. The first object of each item ' 'becomes a key\n' ' in the new dictionary, and the second object the ' 'corresponding\n' ' value. If a key occurs more than once, the last value ' 'for that key\n' ' becomes the corresponding value in the new dictionary.\n' '\n' ' If keyword arguments are given, the keyword arguments and ' 'their\n' ' values are added to the dictionary created from the ' 'positional\n' ' argument. If a key being added is already present, the ' 'value from\n' ' the keyword argument replaces the value from the ' 'positional\n' ' argument.\n' '\n' ' To illustrate, the following examples all return a ' 'dictionary equal\n' ' to "{"one": 1, "two": 2, "three": 3}":\n' '\n' ' >>> a = dict(one=1, two=2, three=3)\n' " >>> b = {'one': 1, 'two': 2, 'three': 3}\n" " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n" " >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n" " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" ' >>> a == b == c == d == e\n' ' True\n' '\n' ' Providing keyword arguments as in the first example only ' 'works for\n' ' keys that are valid Python identifiers. Otherwise, any ' 'valid keys\n' ' can be used.\n' '\n' ' New in version 2.2.\n' '\n' ' Changed in version 2.3: Support for building a dictionary ' 'from\n' ' keyword arguments added.\n' '\n' ' These are the operations that dictionaries support (and ' 'therefore,\n' ' custom mapping types should support too):\n' '\n' ' len(d)\n' '\n' ' Return the number of items in the dictionary *d*.\n' '\n' ' d[key]\n' '\n' ' Return the item of *d* with key *key*. Raises a ' '"KeyError" if\n' ' *key* is not in the map.\n' '\n' ' If a subclass of dict defines a method "__missing__()" ' 'and *key*\n' ' is not present, the "d[key]" operation calls that ' 'method with\n' ' the key *key* as argument. The "d[key]" operation ' 'then returns\n' ' or raises whatever is returned or raised by the\n' ' "__missing__(key)" call. No other operations or ' 'methods invoke\n' ' "__missing__()". If "__missing__()" is not defined, ' '"KeyError"\n' ' is raised. "__missing__()" must be a method; it cannot ' 'be an\n' ' instance variable:\n' '\n' ' >>> class Counter(dict):\n' ' ... def __missing__(self, key):\n' ' ... return 0\n' ' >>> c = Counter()\n' " >>> c['red']\n" ' 0\n' " >>> c['red'] += 1\n" " >>> c['red']\n" ' 1\n' '\n' ' The example above shows part of the implementation of\n' ' "collections.Counter". A different "__missing__" ' 'method is used\n' ' by "collections.defaultdict".\n' '\n' ' New in version 2.5: Recognition of __missing__ methods ' 'of dict\n' ' subclasses.\n' '\n' ' d[key] = value\n' '\n' ' Set "d[key]" to *value*.\n' '\n' ' del d[key]\n' '\n' ' Remove "d[key]" from *d*. Raises a "KeyError" if ' '*key* is not\n' ' in the map.\n' '\n' ' key in d\n' '\n' ' Return "True" if *d* has a key *key*, else "False".\n' '\n' ' New in version 2.2.\n' '\n' ' key not in d\n' '\n' ' Equivalent to "not key in d".\n' '\n' ' New in version 2.2.\n' '\n' ' iter(d)\n' '\n' ' Return an iterator over the keys of the dictionary. ' 'This is a\n' ' shortcut for "iterkeys()".\n' '\n' ' clear()\n' '\n' ' Remove all items from the dictionary.\n' '\n' ' copy()\n' '\n' ' Return a shallow copy of the dictionary.\n' '\n' ' fromkeys(seq[, value])\n' '\n' ' Create a new dictionary with keys from *seq* and ' 'values set to\n' ' *value*.\n' '\n' ' "fromkeys()" is a class method that returns a new ' 'dictionary.\n' ' *value* defaults to "None".\n' '\n' ' New in version 2.3.\n' '\n' ' get(key[, default])\n' '\n' ' Return the value for *key* if *key* is in the ' 'dictionary, else\n' ' *default*. If *default* is not given, it defaults to ' '"None", so\n' ' that this method never raises a "KeyError".\n' '\n' ' has_key(key)\n' '\n' ' Test for the presence of *key* in the dictionary. ' '"has_key()"\n' ' is deprecated in favor of "key in d".\n' '\n' ' items()\n' '\n' ' Return a copy of the dictionary\'s list of "(key, ' 'value)" pairs.\n' '\n' ' **CPython implementation detail:** Keys and values are ' 'listed in\n' ' an arbitrary order which is non-random, varies across ' 'Python\n' " implementations, and depends on the dictionary's " 'history of\n' ' insertions and deletions.\n' '\n' ' If "items()", "keys()", "values()", "iteritems()", ' '"iterkeys()",\n' ' and "itervalues()" are called with no intervening ' 'modifications\n' ' to the dictionary, the lists will directly ' 'correspond. This\n' ' allows the creation of "(value, key)" pairs using ' '"zip()":\n' ' "pairs = zip(d.values(), d.keys())". The same ' 'relationship\n' ' holds for the "iterkeys()" and "itervalues()" methods: ' '"pairs =\n' ' zip(d.itervalues(), d.iterkeys())" provides the same ' 'value for\n' ' "pairs". Another way to create the same list is "pairs ' '= [(v, k)\n' ' for (k, v) in d.iteritems()]".\n' '\n' ' iteritems()\n' '\n' ' Return an iterator over the dictionary\'s "(key, ' 'value)" pairs.\n' ' See the note for "dict.items()".\n' '\n' ' Using "iteritems()" while adding or deleting entries ' 'in the\n' ' dictionary may raise a "RuntimeError" or fail to ' 'iterate over\n' ' all entries.\n' '\n' ' New in version 2.2.\n' '\n' ' iterkeys()\n' '\n' " Return an iterator over the dictionary's keys. See " 'the note for\n' ' "dict.items()".\n' '\n' ' Using "iterkeys()" while adding or deleting entries in ' 'the\n' ' dictionary may raise a "RuntimeError" or fail to ' 'iterate over\n' ' all entries.\n' '\n' ' New in version 2.2.\n' '\n' ' itervalues()\n' '\n' " Return an iterator over the dictionary's values. See " 'the note\n' ' for "dict.items()".\n' '\n' ' Using "itervalues()" while adding or deleting entries ' 'in the\n' ' dictionary may raise a "RuntimeError" or fail to ' 'iterate over\n' ' all entries.\n' '\n' ' New in version 2.2.\n' '\n' ' keys()\n' '\n' " Return a copy of the dictionary's list of keys. See " 'the note\n' ' for "dict.items()".\n' '\n' ' pop(key[, default])\n' '\n' ' If *key* is in the dictionary, remove it and return ' 'its value,\n' ' else return *default*. If *default* is not given and ' '*key* is\n' ' not in the dictionary, a "KeyError" is raised.\n' '\n' ' New in version 2.3.\n' '\n' ' popitem()\n' '\n' ' Remove and return an arbitrary "(key, value)" pair ' 'from the\n' ' dictionary.\n' '\n' ' "popitem()" is useful to destructively iterate over a\n' ' dictionary, as often used in set algorithms. If the ' 'dictionary\n' ' is empty, calling "popitem()" raises a "KeyError".\n' '\n' ' setdefault(key[, default])\n' '\n' ' If *key* is in the dictionary, return its value. If ' 'not, insert\n' ' *key* with a value of *default* and return *default*. ' '*default*\n' ' defaults to "None".\n' '\n' ' update([other])\n' '\n' ' Update the dictionary with the key/value pairs from ' '*other*,\n' ' overwriting existing keys. Return "None".\n' '\n' ' "update()" accepts either another dictionary object or ' 'an\n' ' iterable of key/value pairs (as tuples or other ' 'iterables of\n' ' length two). If keyword arguments are specified, the ' 'dictionary\n' ' is then updated with those key/value pairs: ' '"d.update(red=1,\n' ' blue=2)".\n' '\n' ' Changed in version 2.4: Allowed the argument to be an ' 'iterable\n' ' of key/value pairs and allowed keyword arguments.\n' '\n' ' values()\n' '\n' " Return a copy of the dictionary's list of values. See " 'the note\n' ' for "dict.items()".\n' '\n' ' viewitems()\n' '\n' ' Return a new view of the dictionary\'s items ("(key, ' 'value)"\n' ' pairs). See below for documentation of view objects.\n' '\n' ' New in version 2.7.\n' '\n' ' viewkeys()\n' '\n' " Return a new view of the dictionary's keys. See below " 'for\n' ' documentation of view objects.\n' '\n' ' New in version 2.7.\n' '\n' ' viewvalues()\n' '\n' " Return a new view of the dictionary's values. See " 'below for\n' ' documentation of view objects.\n' '\n' ' New in version 2.7.\n' '\n' ' Dictionaries compare equal if and only if they have the ' 'same "(key,\n' ' value)" pairs.\n' '\n' '\n' 'Dictionary view objects\n' '=======================\n' '\n' 'The objects returned by "dict.viewkeys()", ' '"dict.viewvalues()" and\n' '"dict.viewitems()" are *view objects*. They provide a ' 'dynamic view on\n' "the dictionary's entries, which means that when the " 'dictionary\n' 'changes, the view reflects these changes.\n' '\n' 'Dictionary views can be iterated over to yield their ' 'respective data,\n' 'and support membership tests:\n' '\n' 'len(dictview)\n' '\n' ' Return the number of entries in the dictionary.\n' '\n' 'iter(dictview)\n' '\n' ' Return an iterator over the keys, values or items ' '(represented as\n' ' tuples of "(key, value)") in the dictionary.\n' '\n' ' Keys and values are iterated over in an arbitrary order ' 'which is\n' ' non-random, varies across Python implementations, and ' 'depends on\n' " the dictionary's history of insertions and deletions. If " 'keys,\n' ' values and items views are iterated over with no ' 'intervening\n' ' modifications to the dictionary, the order of items will ' 'directly\n' ' correspond. This allows the creation of "(value, key)" ' 'pairs using\n' ' "zip()": "pairs = zip(d.values(), d.keys())". Another ' 'way to\n' ' create the same list is "pairs = [(v, k) for (k, v) in ' 'd.items()]".\n' '\n' ' Iterating views while adding or deleting entries in the ' 'dictionary\n' ' may raise a "RuntimeError" or fail to iterate over all ' 'entries.\n' '\n' 'x in dictview\n' '\n' ' Return "True" if *x* is in the underlying dictionary\'s ' 'keys, values\n' ' or items (in the latter case, *x* should be a "(key, ' 'value)"\n' ' tuple).\n' '\n' 'Keys views are set-like since their entries are unique and ' 'hashable.\n' 'If all values are hashable, so that (key, value) pairs are ' 'unique and\n' 'hashable, then the items view is also set-like. (Values ' 'views are not\n' 'treated as set-like since the entries are generally not ' 'unique.) Then\n' 'these set operations are available ("other" refers either to ' 'another\n' 'view or a set):\n' '\n' 'dictview & other\n' '\n' ' Return the intersection of the dictview and the other ' 'object as a\n' ' new set.\n' '\n' 'dictview | other\n' '\n' ' Return the union of the dictview and the other object as ' 'a new set.\n' '\n' 'dictview - other\n' '\n' ' Return the difference between the dictview and the other ' 'object\n' " (all elements in *dictview* that aren't in *other*) as a " 'new set.\n' '\n' 'dictview ^ other\n' '\n' ' Return the symmetric difference (all elements either in ' '*dictview*\n' ' or *other*, but not in both) of the dictview and the ' 'other object\n' ' as a new set.\n' '\n' 'An example of dictionary view usage:\n' '\n' " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " "'spam': 500}\n" ' >>> keys = dishes.viewkeys()\n' ' >>> values = dishes.viewvalues()\n' '\n' ' >>> # iteration\n' ' >>> n = 0\n' ' >>> for val in values:\n' ' ... n += val\n' ' >>> print(n)\n' ' 504\n' '\n' ' >>> # keys and values are iterated over in the same ' 'order\n' ' >>> list(keys)\n' " ['eggs', 'bacon', 'sausage', 'spam']\n" ' >>> list(values)\n' ' [2, 1, 1, 500]\n' '\n' ' >>> # view objects are dynamic and reflect dict changes\n' " >>> del dishes['eggs']\n" " >>> del dishes['sausage']\n" ' >>> list(keys)\n' " ['spam', 'bacon']\n" '\n' ' >>> # set operations\n' " >>> keys & {'eggs', 'bacon', 'salad'}\n" " {'bacon'}\n", 'typesmethods': '\n' 'Methods\n' '*******\n' '\n' 'Methods are functions that are called using the attribute ' 'notation.\n' 'There are two flavors: built-in methods (such as "append()" ' 'on lists)\n' 'and class instance methods. Built-in methods are described ' 'with the\n' 'types that support them.\n' '\n' 'The implementation adds two special read-only attributes to ' 'class\n' 'instance methods: "m.im_self" is the object on which the ' 'method\n' 'operates, and "m.im_func" is the function implementing the ' 'method.\n' 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' 'equivalent to\n' 'calling "m.im_func(m.im_self, arg-1, arg-2, ..., arg-n)".\n' '\n' 'Class instance methods are either *bound* or *unbound*, ' 'referring to\n' 'whether the method was accessed through an instance or a ' 'class,\n' 'respectively. When a method is unbound, its "im_self" ' 'attribute will\n' 'be "None" and if called, an explicit "self" object must be ' 'passed as\n' 'the first argument. In this case, "self" must be an ' 'instance of the\n' "unbound method's class (or a subclass of that class), " 'otherwise a\n' '"TypeError" is raised.\n' '\n' 'Like function objects, methods objects support getting ' 'arbitrary\n' 'attributes. However, since method attributes are actually ' 'stored on\n' 'the underlying function object ("meth.im_func"), setting ' 'method\n' 'attributes on either bound or unbound methods is ' 'disallowed.\n' 'Attempting to set an attribute on a method results in an\n' '"AttributeError" being raised. In order to set a method ' 'attribute,\n' 'you need to explicitly set it on the underlying function ' 'object:\n' '\n' ' >>> class C:\n' ' ... def method(self):\n' ' ... pass\n' ' ...\n' ' >>> c = C()\n' " >>> c.method.whoami = 'my name is method' # can't set on " 'the method\n' ' Traceback (most recent call last):\n' ' File "", line 1, in \n' " AttributeError: 'instancemethod' object has no attribute " "'whoami'\n" " >>> c.method.im_func.whoami = 'my name is method'\n" ' >>> c.method.whoami\n' " 'my name is method'\n" '\n' 'See The standard type hierarchy for more information.\n', 'typesmodules': '\n' 'Modules\n' '*******\n' '\n' 'The only special operation on a module is attribute access: ' '"m.name",\n' 'where *m* is a module and *name* accesses a name defined in ' "*m*'s\n" 'symbol table. Module attributes can be assigned to. (Note ' 'that the\n' '"import" statement is not, strictly speaking, an operation ' 'on a module\n' 'object; "import foo" does not require a module object named ' '*foo* to\n' 'exist, rather it requires an (external) *definition* for a ' 'module\n' 'named *foo* somewhere.)\n' '\n' 'A special attribute of every module is "__dict__". This is ' 'the\n' "dictionary containing the module's symbol table. Modifying " 'this\n' "dictionary will actually change the module's symbol table, " 'but direct\n' 'assignment to the "__dict__" attribute is not possible (you ' 'can write\n' '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but ' "you can't\n" 'write "m.__dict__ = {}"). Modifying "__dict__" directly is ' 'not\n' 'recommended.\n' '\n' 'Modules built into the interpreter are written like this: ' '"". If loaded from a file, they are ' 'written as\n' '"".\n', 'typesseq': '\n' 'Sequence Types --- "str", "unicode", "list", "tuple", ' '"bytearray", "buffer", "xrange"\n' '*************************************************************************************\n' '\n' 'There are seven sequence types: strings, Unicode strings, ' 'lists,\n' 'tuples, bytearrays, buffers, and xrange objects.\n' '\n' 'For other containers see the built in "dict" and "set" classes, ' 'and\n' 'the "collections" module.\n' '\n' 'String literals are written in single or double quotes: ' '"\'xyzzy\'",\n' '""frobozz"". See String literals for more about string ' 'literals.\n' 'Unicode strings are much like strings, but are specified in the ' 'syntax\n' 'using a preceding "\'u\'" character: "u\'abc\'", "u"def"". In ' 'addition to\n' 'the functionality described here, there are also ' 'string-specific\n' 'methods described in the String Methods section. Lists are ' 'constructed\n' 'with square brackets, separating items with commas: "[a, b, ' 'c]".\n' 'Tuples are constructed by the comma operator (not within square\n' 'brackets), with or without enclosing parentheses, but an empty ' 'tuple\n' 'must have the enclosing parentheses, such as "a, b, c" or "()". ' 'A\n' 'single item tuple must have a trailing comma, such as "(d,)".\n' '\n' 'Bytearray objects are created with the built-in function\n' '"bytearray()".\n' '\n' 'Buffer objects are not directly supported by Python syntax, but ' 'can be\n' 'created by calling the built-in function "buffer()". They ' "don't\n" 'support concatenation or repetition.\n' '\n' 'Objects of type xrange are similar to buffers in that there is ' 'no\n' 'specific syntax to create them, but they are created using the\n' '"xrange()" function. They don\'t support slicing, concatenation ' 'or\n' 'repetition, and using "in", "not in", "min()" or "max()" on them ' 'is\n' 'inefficient.\n' '\n' 'Most sequence types support the following operations. The "in" ' 'and\n' '"not in" operations have the same priorities as the comparison\n' 'operations. The "+" and "*" operations have the same priority ' 'as the\n' 'corresponding numeric operations. [3] Additional methods are ' 'provided\n' 'for Mutable Sequence Types.\n' '\n' 'This table lists the sequence operations sorted in ascending ' 'priority.\n' 'In the table, *s* and *t* are sequences of the same type; *n*, ' '*i* and\n' '*j* are integers:\n' '\n' '+--------------------+----------------------------------+------------+\n' '| Operation | Result | ' 'Notes |\n' '+====================+==================================+============+\n' '| "x in s" | "True" if an item of *s* is | ' '(1) |\n' '| | equal to *x*, else "False" ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "x not in s" | "False" if an item of *s* is | ' '(1) |\n' '| | equal to *x*, else "True" ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "s + t" | the concatenation of *s* and *t* | ' '(6) |\n' '+--------------------+----------------------------------+------------+\n' '| "s * n, n * s" | equivalent to adding *s* to | ' '(2) |\n' '| | itself *n* times ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "s[i]" | *i*th item of *s*, origin 0 | ' '(3) |\n' '+--------------------+----------------------------------+------------+\n' '| "s[i:j]" | slice of *s* from *i* to *j* | ' '(3)(4) |\n' '+--------------------+----------------------------------+------------+\n' '| "s[i:j:k]" | slice of *s* from *i* to *j* | ' '(3)(5) |\n' '| | with step *k* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "len(s)" | length of *s* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "min(s)" | smallest item of *s* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "max(s)" | largest item of *s* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "s.index(x)" | index of the first occurrence of ' '| |\n' '| | *x* in *s* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '| "s.count(x)" | total number of occurrences of ' '| |\n' '| | *x* in *s* ' '| |\n' '+--------------------+----------------------------------+------------+\n' '\n' 'Sequence types also support comparisons. In particular, tuples ' 'and\n' 'lists are compared lexicographically by comparing corresponding\n' 'elements. This means that to compare equal, every element must ' 'compare\n' 'equal and the two sequences must be of the same type and have ' 'the same\n' 'length. (For full details see Comparisons in the language ' 'reference.)\n' '\n' 'Notes:\n' '\n' '1. When *s* is a string or Unicode string object the "in" and ' '"not\n' ' in" operations act like a substring test. In Python ' 'versions\n' ' before 2.3, *x* had to be a string of length 1. In Python 2.3 ' 'and\n' ' beyond, *x* may be a string of any length.\n' '\n' '2. Values of *n* less than "0" are treated as "0" (which yields ' 'an\n' ' empty sequence of the same type as *s*). Note that items in ' 'the\n' ' sequence *s* are not copied; they are referenced multiple ' 'times.\n' ' This often haunts new Python programmers; consider:\n' '\n' ' >>> lists = [[]] * 3\n' ' >>> lists\n' ' [[], [], []]\n' ' >>> lists[0].append(3)\n' ' >>> lists\n' ' [[3], [3], [3]]\n' '\n' ' What has happened is that "[[]]" is a one-element list ' 'containing\n' ' an empty list, so all three elements of "[[]] * 3" are ' 'references\n' ' to this single empty list. Modifying any of the elements of\n' ' "lists" modifies this single list. You can create a list of\n' ' different lists this way:\n' '\n' ' >>> lists = [[] for i in range(3)]\n' ' >>> lists[0].append(3)\n' ' >>> lists[1].append(5)\n' ' >>> lists[2].append(7)\n' ' >>> lists\n' ' [[3], [5], [7]]\n' '\n' ' Further explanation is available in the FAQ entry How do I ' 'create a\n' ' multidimensional list?.\n' '\n' '3. If *i* or *j* is negative, the index is relative to the end ' 'of\n' ' sequence *s*: "len(s) + i" or "len(s) + j" is substituted. ' 'But\n' ' note that "-0" is still "0".\n' '\n' '4. The slice of *s* from *i* to *j* is defined as the sequence ' 'of\n' ' items with index *k* such that "i <= k < j". If *i* or *j* ' 'is\n' ' greater than "len(s)", use "len(s)". If *i* is omitted or ' '"None",\n' ' use "0". If *j* is omitted or "None", use "len(s)". If *i* ' 'is\n' ' greater than or equal to *j*, the slice is empty.\n' '\n' '5. The slice of *s* from *i* to *j* with step *k* is defined as ' 'the\n' ' sequence of items with index "x = i + n*k" such that "0 <= n ' '<\n' ' (j-i)/k". In other words, the indices are "i", "i+k", ' '"i+2*k",\n' ' "i+3*k" and so on, stopping when *j* is reached (but never\n' ' including *j*). When *k* is positive, *i* and *j* are ' 'reduced to\n' ' "len(s)" if they are greater. When *k* is negative, *i* and ' '*j* are\n' ' reduced to "len(s) - 1" if they are greater. If *i* or *j* ' 'are\n' ' omitted or "None", they become "end" values (which end ' 'depends on\n' ' the sign of *k*). Note, *k* cannot be zero. If *k* is ' '"None", it\n' ' is treated like "1".\n' '\n' '6. **CPython implementation detail:** If *s* and *t* are both\n' ' strings, some Python implementations such as CPython can ' 'usually\n' ' perform an in-place optimization for assignments of the form ' '"s = s\n' ' + t" or "s += t". When applicable, this optimization makes\n' ' quadratic run-time much less likely. This optimization is ' 'both\n' ' version and implementation dependent. For performance ' 'sensitive\n' ' code, it is preferable to use the "str.join()" method which ' 'assures\n' ' consistent linear concatenation performance across versions ' 'and\n' ' implementations.\n' '\n' ' Changed in version 2.4: Formerly, string concatenation never\n' ' occurred in-place.\n' '\n' '\n' 'String Methods\n' '==============\n' '\n' 'Below are listed the string methods which both 8-bit strings ' 'and\n' 'Unicode objects support. Some of them are also available on\n' '"bytearray" objects.\n' '\n' "In addition, Python's strings support the sequence type methods\n" 'described in the Sequence Types --- str, unicode, list, tuple,\n' 'bytearray, buffer, xrange section. To output formatted strings ' 'use\n' 'template strings or the "%" operator described in the String\n' 'Formatting Operations section. Also, see the "re" module for ' 'string\n' 'functions based on regular expressions.\n' '\n' 'str.capitalize()\n' '\n' ' Return a copy of the string with its first character ' 'capitalized\n' ' and the rest lowercased.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.center(width[, fillchar])\n' '\n' ' Return centered in a string of length *width*. Padding is ' 'done\n' ' using the specified *fillchar* (default is a space).\n' '\n' ' Changed in version 2.4: Support for the *fillchar* argument.\n' '\n' 'str.count(sub[, start[, end]])\n' '\n' ' Return the number of non-overlapping occurrences of substring ' '*sub*\n' ' in the range [*start*, *end*]. Optional arguments *start* ' 'and\n' ' *end* are interpreted as in slice notation.\n' '\n' 'str.decode([encoding[, errors]])\n' '\n' ' Decodes the string using the codec registered for ' '*encoding*.\n' ' *encoding* defaults to the default string encoding. *errors* ' 'may\n' ' be given to set a different error handling scheme. The ' 'default is\n' ' "\'strict\'", meaning that encoding errors raise ' '"UnicodeError".\n' ' Other possible values are "\'ignore\'", "\'replace\'" and any ' 'other\n' ' name registered via "codecs.register_error()", see section ' 'Codec\n' ' Base Classes.\n' '\n' ' New in version 2.2.\n' '\n' ' Changed in version 2.3: Support for other error handling ' 'schemes\n' ' added.\n' '\n' ' Changed in version 2.7: Support for keyword arguments added.\n' '\n' 'str.encode([encoding[, errors]])\n' '\n' ' Return an encoded version of the string. Default encoding is ' 'the\n' ' current default string encoding. *errors* may be given to ' 'set a\n' ' different error handling scheme. The default for *errors* ' 'is\n' ' "\'strict\'", meaning that encoding errors raise a ' '"UnicodeError".\n' ' Other possible values are "\'ignore\'", "\'replace\'",\n' ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and any other ' 'name\n' ' registered via "codecs.register_error()", see section Codec ' 'Base\n' ' Classes. For a list of possible encodings, see section ' 'Standard\n' ' Encodings.\n' '\n' ' New in version 2.0.\n' '\n' ' Changed in version 2.3: Support for "\'xmlcharrefreplace\'" ' 'and\n' ' "\'backslashreplace\'" and other error handling schemes ' 'added.\n' '\n' ' Changed in version 2.7: Support for keyword arguments added.\n' '\n' 'str.endswith(suffix[, start[, end]])\n' '\n' ' Return "True" if the string ends with the specified ' '*suffix*,\n' ' otherwise return "False". *suffix* can also be a tuple of ' 'suffixes\n' ' to look for. With optional *start*, test beginning at that\n' ' position. With optional *end*, stop comparing at that ' 'position.\n' '\n' ' Changed in version 2.5: Accept tuples as *suffix*.\n' '\n' 'str.expandtabs([tabsize])\n' '\n' ' Return a copy of the string where all tab characters are ' 'replaced\n' ' by one or more spaces, depending on the current column and ' 'the\n' ' given tab size. Tab positions occur every *tabsize* ' 'characters\n' ' (default is 8, giving tab positions at columns 0, 8, 16 and ' 'so on).\n' ' To expand the string, the current column is set to zero and ' 'the\n' ' string is examined character by character. If the character ' 'is a\n' ' tab ("\\t"), one or more space characters are inserted in the ' 'result\n' ' until the current column is equal to the next tab position. ' '(The\n' ' tab character itself is not copied.) If the character is a ' 'newline\n' ' ("\\n") or return ("\\r"), it is copied and the current ' 'column is\n' ' reset to zero. Any other character is copied unchanged and ' 'the\n' ' current column is incremented by one regardless of how the\n' ' character is represented when printed.\n' '\n' " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" " '01 012 0123 01234'\n" " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" " '01 012 0123 01234'\n" '\n' 'str.find(sub[, start[, end]])\n' '\n' ' Return the lowest index in the string where substring *sub* ' 'is\n' ' found within the slice "s[start:end]". Optional arguments ' '*start*\n' ' and *end* are interpreted as in slice notation. Return "-1" ' 'if\n' ' *sub* is not found.\n' '\n' ' Note: The "find()" method should be used only if you need to ' 'know\n' ' the position of *sub*. To check if *sub* is a substring or ' 'not,\n' ' use the "in" operator:\n' '\n' " >>> 'Py' in 'Python'\n" ' True\n' '\n' 'str.format(*args, **kwargs)\n' '\n' ' Perform a string formatting operation. The string on which ' 'this\n' ' method is called can contain literal text or replacement ' 'fields\n' ' delimited by braces "{}". Each replacement field contains ' 'either\n' ' the numeric index of a positional argument, or the name of a\n' ' keyword argument. Returns a copy of the string where each\n' ' replacement field is replaced with the string value of the\n' ' corresponding argument.\n' '\n' ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' " 'The sum of 1 + 2 is 3'\n" '\n' ' See Format String Syntax for a description of the various\n' ' formatting options that can be specified in format strings.\n' '\n' ' This method of string formatting is the new standard in ' 'Python 3,\n' ' and should be preferred to the "%" formatting described in ' 'String\n' ' Formatting Operations in new code.\n' '\n' ' New in version 2.6.\n' '\n' 'str.index(sub[, start[, end]])\n' '\n' ' Like "find()", but raise "ValueError" when the substring is ' 'not\n' ' found.\n' '\n' 'str.isalnum()\n' '\n' ' Return true if all characters in the string are alphanumeric ' 'and\n' ' there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isalpha()\n' '\n' ' Return true if all characters in the string are alphabetic ' 'and\n' ' there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isdigit()\n' '\n' ' Return true if all characters in the string are digits and ' 'there is\n' ' at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.islower()\n' '\n' ' Return true if all cased characters [4] in the string are ' 'lowercase\n' ' and there is at least one cased character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isspace()\n' '\n' ' Return true if there are only whitespace characters in the ' 'string\n' ' and there is at least one character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.istitle()\n' '\n' ' Return true if the string is a titlecased string and there is ' 'at\n' ' least one character, for example uppercase characters may ' 'only\n' ' follow uncased characters and lowercase characters only cased ' 'ones.\n' ' Return false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.isupper()\n' '\n' ' Return true if all cased characters [4] in the string are ' 'uppercase\n' ' and there is at least one cased character, false otherwise.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.join(iterable)\n' '\n' ' Return a string which is the concatenation of the strings in\n' ' *iterable*. A "TypeError" will be raised if there are any ' 'non-\n' ' string values in *iterable*, including "bytes" objects. The\n' ' separator between elements is the string providing this ' 'method.\n' '\n' 'str.ljust(width[, fillchar])\n' '\n' ' Return the string left justified in a string of length ' '*width*.\n' ' Padding is done using the specified *fillchar* (default is a\n' ' space). The original string is returned if *width* is less ' 'than or\n' ' equal to "len(s)".\n' '\n' ' Changed in version 2.4: Support for the *fillchar* argument.\n' '\n' 'str.lower()\n' '\n' ' Return a copy of the string with all the cased characters ' '[4]\n' ' converted to lowercase.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.lstrip([chars])\n' '\n' ' Return a copy of the string with leading characters removed. ' 'The\n' ' *chars* argument is a string specifying the set of characters ' 'to be\n' ' removed. If omitted or "None", the *chars* argument defaults ' 'to\n' ' removing whitespace. The *chars* argument is not a prefix; ' 'rather,\n' ' all combinations of its values are stripped:\n' '\n' " >>> ' spacious '.lstrip()\n" " 'spacious '\n" " >>> 'www.example.com'.lstrip('cmowz.')\n" " 'example.com'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* argument.\n' '\n' 'str.partition(sep)\n' '\n' ' Split the string at the first occurrence of *sep*, and return ' 'a\n' ' 3-tuple containing the part before the separator, the ' 'separator\n' ' itself, and the part after the separator. If the separator ' 'is not\n' ' found, return a 3-tuple containing the string itself, ' 'followed by\n' ' two empty strings.\n' '\n' ' New in version 2.5.\n' '\n' 'str.replace(old, new[, count])\n' '\n' ' Return a copy of the string with all occurrences of substring ' '*old*\n' ' replaced by *new*. If the optional argument *count* is ' 'given, only\n' ' the first *count* occurrences are replaced.\n' '\n' 'str.rfind(sub[, start[, end]])\n' '\n' ' Return the highest index in the string where substring *sub* ' 'is\n' ' found, such that *sub* is contained within "s[start:end]".\n' ' Optional arguments *start* and *end* are interpreted as in ' 'slice\n' ' notation. Return "-1" on failure.\n' '\n' 'str.rindex(sub[, start[, end]])\n' '\n' ' Like "rfind()" but raises "ValueError" when the substring ' '*sub* is\n' ' not found.\n' '\n' 'str.rjust(width[, fillchar])\n' '\n' ' Return the string right justified in a string of length ' '*width*.\n' ' Padding is done using the specified *fillchar* (default is a\n' ' space). The original string is returned if *width* is less ' 'than or\n' ' equal to "len(s)".\n' '\n' ' Changed in version 2.4: Support for the *fillchar* argument.\n' '\n' 'str.rpartition(sep)\n' '\n' ' Split the string at the last occurrence of *sep*, and return ' 'a\n' ' 3-tuple containing the part before the separator, the ' 'separator\n' ' itself, and the part after the separator. If the separator ' 'is not\n' ' found, return a 3-tuple containing two empty strings, ' 'followed by\n' ' the string itself.\n' '\n' ' New in version 2.5.\n' '\n' 'str.rsplit([sep[, maxsplit]])\n' '\n' ' Return a list of the words in the string, using *sep* as the\n' ' delimiter string. If *maxsplit* is given, at most *maxsplit* ' 'splits\n' ' are done, the *rightmost* ones. If *sep* is not specified ' 'or\n' ' "None", any whitespace string is a separator. Except for ' 'splitting\n' ' from the right, "rsplit()" behaves like "split()" which is\n' ' described in detail below.\n' '\n' ' New in version 2.4.\n' '\n' 'str.rstrip([chars])\n' '\n' ' Return a copy of the string with trailing characters ' 'removed. The\n' ' *chars* argument is a string specifying the set of characters ' 'to be\n' ' removed. If omitted or "None", the *chars* argument defaults ' 'to\n' ' removing whitespace. The *chars* argument is not a suffix; ' 'rather,\n' ' all combinations of its values are stripped:\n' '\n' " >>> ' spacious '.rstrip()\n" " ' spacious'\n" " >>> 'mississippi'.rstrip('ipz')\n" " 'mississ'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* argument.\n' '\n' 'str.split([sep[, maxsplit]])\n' '\n' ' Return a list of the words in the string, using *sep* as the\n' ' delimiter string. If *maxsplit* is given, at most ' '*maxsplit*\n' ' splits are done (thus, the list will have at most ' '"maxsplit+1"\n' ' elements). If *maxsplit* is not specified or "-1", then ' 'there is\n' ' no limit on the number of splits (all possible splits are ' 'made).\n' '\n' ' If *sep* is given, consecutive delimiters are not grouped ' 'together\n' ' and are deemed to delimit empty strings (for example,\n' ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', \'2\']"). The ' '*sep* argument\n' ' may consist of multiple characters (for example,\n' ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', \'3\']"). ' 'Splitting an\n' ' empty string with a specified separator returns "[\'\']".\n' '\n' ' If *sep* is not specified or is "None", a different ' 'splitting\n' ' algorithm is applied: runs of consecutive whitespace are ' 'regarded\n' ' as a single separator, and the result will contain no empty ' 'strings\n' ' at the start or end if the string has leading or trailing\n' ' whitespace. Consequently, splitting an empty string or a ' 'string\n' ' consisting of just whitespace with a "None" separator returns ' '"[]".\n' '\n' ' For example, "\' 1 2 3 \'.split()" returns "[\'1\', ' '\'2\', \'3\']", and\n' ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', \'2 3 ' '\']".\n' '\n' 'str.splitlines([keepends])\n' '\n' ' Return a list of the lines in the string, breaking at line\n' ' boundaries. This method uses the *universal newlines* ' 'approach to\n' ' splitting lines. Line breaks are not included in the ' 'resulting list\n' ' unless *keepends* is given and true.\n' '\n' ' Python recognizes ""\\r"", ""\\n"", and ""\\r\\n"" as line ' 'boundaries\n' ' for 8-bit strings.\n' '\n' ' For example:\n' '\n' " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" " ['ab c', '', 'de fg', 'kl']\n" " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines(True)\n" " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" '\n' ' Unlike "split()" when a delimiter string *sep* is given, ' 'this\n' ' method returns an empty list for the empty string, and a ' 'terminal\n' ' line break does not result in an extra line:\n' '\n' ' >>> "".splitlines()\n' ' []\n' ' >>> "One line\\n".splitlines()\n' " ['One line']\n" '\n' ' For comparison, "split(\'\\n\')" gives:\n' '\n' " >>> ''.split('\\n')\n" " ['']\n" " >>> 'Two lines\\n'.split('\\n')\n" " ['Two lines', '']\n" '\n' 'unicode.splitlines([keepends])\n' '\n' ' Return a list of the lines in the string, like ' '"str.splitlines()".\n' ' However, the Unicode method splits on the following line\n' ' boundaries, which are a superset of the *universal newlines*\n' ' recognized for 8-bit strings.\n' '\n' ' +-------------------------+-------------------------------+\n' ' | Representation | Description |\n' ' +=========================+===============================+\n' ' | "\\n" | Line Feed |\n' ' +-------------------------+-------------------------------+\n' ' | "\\r" | Carriage Return |\n' ' +-------------------------+-------------------------------+\n' ' | "\\r\\n" | Carriage Return + Line Feed ' '|\n' ' +-------------------------+-------------------------------+\n' ' | "\\v" or "\\x0b" | Line Tabulation ' '|\n' ' +-------------------------+-------------------------------+\n' ' | "\\f" or "\\x0c" | Form Feed ' '|\n' ' +-------------------------+-------------------------------+\n' ' | "\\x1c" | File Separator |\n' ' +-------------------------+-------------------------------+\n' ' | "\\x1d" | Group Separator |\n' ' +-------------------------+-------------------------------+\n' ' | "\\x1e" | Record Separator |\n' ' +-------------------------+-------------------------------+\n' ' | "\\x85" | Next Line (C1 Control Code) |\n' ' +-------------------------+-------------------------------+\n' ' | "\\u2028" | Line Separator |\n' ' +-------------------------+-------------------------------+\n' ' | "\\u2029" | Paragraph Separator |\n' ' +-------------------------+-------------------------------+\n' '\n' ' Changed in version 2.7: "\\v" and "\\f" added to list of ' 'line\n' ' boundaries.\n' '\n' 'str.startswith(prefix[, start[, end]])\n' '\n' ' Return "True" if string starts with the *prefix*, otherwise ' 'return\n' ' "False". *prefix* can also be a tuple of prefixes to look ' 'for.\n' ' With optional *start*, test string beginning at that ' 'position.\n' ' With optional *end*, stop comparing string at that position.\n' '\n' ' Changed in version 2.5: Accept tuples as *prefix*.\n' '\n' 'str.strip([chars])\n' '\n' ' Return a copy of the string with the leading and trailing\n' ' characters removed. The *chars* argument is a string ' 'specifying the\n' ' set of characters to be removed. If omitted or "None", the ' '*chars*\n' ' argument defaults to removing whitespace. The *chars* ' 'argument is\n' ' not a prefix or suffix; rather, all combinations of its ' 'values are\n' ' stripped:\n' '\n' " >>> ' spacious '.strip()\n" " 'spacious'\n" " >>> 'www.example.com'.strip('cmowz.')\n" " 'example'\n" '\n' ' Changed in version 2.2.2: Support for the *chars* argument.\n' '\n' 'str.swapcase()\n' '\n' ' Return a copy of the string with uppercase characters ' 'converted to\n' ' lowercase and vice versa.\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.title()\n' '\n' ' Return a titlecased version of the string where words start ' 'with an\n' ' uppercase character and the remaining characters are ' 'lowercase.\n' '\n' ' The algorithm uses a simple language-independent definition ' 'of a\n' ' word as groups of consecutive letters. The definition works ' 'in\n' ' many contexts but it means that apostrophes in contractions ' 'and\n' ' possessives form word boundaries, which may not be the ' 'desired\n' ' result:\n' '\n' ' >>> "they\'re bill\'s friends from the UK".title()\n' ' "They\'Re Bill\'S Friends From The Uk"\n' '\n' ' A workaround for apostrophes can be constructed using ' 'regular\n' ' expressions:\n' '\n' ' >>> import re\n' ' >>> def titlecase(s):\n' ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' ' ... lambda mo: mo.group(0)[0].upper() +\n' ' ... mo.group(0)[1:].lower(),\n' ' ... s)\n' ' ...\n' ' >>> titlecase("they\'re bill\'s friends.")\n' ' "They\'re Bill\'s Friends."\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.translate(table[, deletechars])\n' '\n' ' Return a copy of the string where all characters occurring in ' 'the\n' ' optional argument *deletechars* are removed, and the ' 'remaining\n' ' characters have been mapped through the given translation ' 'table,\n' ' which must be a string of length 256.\n' '\n' ' You can use the "maketrans()" helper function in the ' '"string"\n' ' module to create a translation table. For string objects, set ' 'the\n' ' *table* argument to "None" for translations that only delete\n' ' characters:\n' '\n' " >>> 'read this short text'.translate(None, 'aeiou')\n" " 'rd ths shrt txt'\n" '\n' ' New in version 2.6: Support for a "None" *table* argument.\n' '\n' ' For Unicode objects, the "translate()" method does not accept ' 'the\n' ' optional *deletechars* argument. Instead, it returns a copy ' 'of the\n' ' *s* where all characters have been mapped through the given\n' ' translation table which must be a mapping of Unicode ordinals ' 'to\n' ' Unicode ordinals, Unicode strings or "None". Unmapped ' 'characters\n' ' are left untouched. Characters mapped to "None" are deleted. ' 'Note,\n' ' a more flexible approach is to create a custom character ' 'mapping\n' ' codec using the "codecs" module (see "encodings.cp1251" for ' 'an\n' ' example).\n' '\n' 'str.upper()\n' '\n' ' Return a copy of the string with all the cased characters ' '[4]\n' ' converted to uppercase. Note that "str.upper().isupper()" ' 'might be\n' ' "False" if "s" contains uncased characters or if the Unicode\n' ' category of the resulting character(s) is not "Lu" (Letter,\n' ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' '\n' ' For 8-bit strings, this method is locale-dependent.\n' '\n' 'str.zfill(width)\n' '\n' ' Return the numeric string left filled with zeros in a string ' 'of\n' ' length *width*. A sign prefix is handled correctly. The ' 'original\n' ' string is returned if *width* is less than or equal to ' '"len(s)".\n' '\n' ' New in version 2.2.2.\n' '\n' 'The following methods are present only on unicode objects:\n' '\n' 'unicode.isnumeric()\n' '\n' ' Return "True" if there are only numeric characters in S, ' '"False"\n' ' otherwise. Numeric characters include digit characters, and ' 'all\n' ' characters that have the Unicode numeric value property, ' 'e.g.\n' ' U+2155, VULGAR FRACTION ONE FIFTH.\n' '\n' 'unicode.isdecimal()\n' '\n' ' Return "True" if there are only decimal characters in S, ' '"False"\n' ' otherwise. Decimal characters include digit characters, and ' 'all\n' ' characters that can be used to form decimal-radix numbers, ' 'e.g.\n' ' U+0660, ARABIC-INDIC DIGIT ZERO.\n' '\n' '\n' 'String Formatting Operations\n' '============================\n' '\n' 'String and Unicode objects have one unique built-in operation: ' 'the "%"\n' 'operator (modulo). This is also known as the string ' '*formatting* or\n' '*interpolation* operator. Given "format % values" (where ' '*format* is\n' 'a string or Unicode object), "%" conversion specifications in ' '*format*\n' 'are replaced with zero or more elements of *values*. The effect ' 'is\n' 'similar to the using "sprintf()" in the C language. If *format* ' 'is a\n' 'Unicode object, or if any of the objects being converted using ' 'the\n' '"%s" conversion are Unicode objects, the result will also be a ' 'Unicode\n' 'object.\n' '\n' 'If *format* requires a single argument, *values* may be a single ' 'non-\n' 'tuple object. [5] Otherwise, *values* must be a tuple with ' 'exactly\n' 'the number of items specified by the format string, or a single\n' 'mapping object (for example, a dictionary).\n' '\n' 'A conversion specifier contains two or more characters and has ' 'the\n' 'following components, which must occur in this order:\n' '\n' '1. The "\'%\'" character, which marks the start of the ' 'specifier.\n' '\n' '2. Mapping key (optional), consisting of a parenthesised ' 'sequence\n' ' of characters (for example, "(somename)").\n' '\n' '3. Conversion flags (optional), which affect the result of some\n' ' conversion types.\n' '\n' '4. Minimum field width (optional). If specified as an "\'*\'"\n' ' (asterisk), the actual width is read from the next element of ' 'the\n' ' tuple in *values*, and the object to convert comes after the\n' ' minimum field width and optional precision.\n' '\n' '5. Precision (optional), given as a "\'.\'" (dot) followed by ' 'the\n' ' precision. If specified as "\'*\'" (an asterisk), the actual ' 'width\n' ' is read from the next element of the tuple in *values*, and ' 'the\n' ' value to convert comes after the precision.\n' '\n' '6. Length modifier (optional).\n' '\n' '7. Conversion type.\n' '\n' 'When the right argument is a dictionary (or other mapping type), ' 'then\n' 'the formats in the string *must* include a parenthesised mapping ' 'key\n' 'into that dictionary inserted immediately after the "\'%\'" ' 'character.\n' 'The mapping key selects the value to be formatted from the ' 'mapping.\n' 'For example:\n' '\n' ">>> print '%(language)s has %(number)03d quote types.' % \\\n" '... {"language": "Python", "number": 2}\n' 'Python has 002 quote types.\n' '\n' 'In this case no "*" specifiers may occur in a format (since ' 'they\n' 'require a sequential parameter list).\n' '\n' 'The conversion flag characters are:\n' '\n' '+-----------+-----------------------------------------------------------------------+\n' '| Flag | ' 'Meaning ' '|\n' '+===========+=======================================================================+\n' '| "\'#\'" | The value conversion will use the "alternate ' 'form" (where defined |\n' '| | ' 'below). ' '|\n' '+-----------+-----------------------------------------------------------------------+\n' '| "\'0\'" | The conversion will be zero padded for numeric ' 'values. |\n' '+-----------+-----------------------------------------------------------------------+\n' '| "\'-\'" | The converted value is left adjusted (overrides ' 'the "\'0\'" conversion |\n' '| | if both are ' 'given). |\n' '+-----------+-----------------------------------------------------------------------+\n' '| "\' \'" | (a space) A blank should be left before a ' 'positive number (or empty |\n' '| | string) produced by a signed ' 'conversion. |\n' '+-----------+-----------------------------------------------------------------------+\n' '| "\'+\'" | A sign character ("\'+\'" or "\'-\'") will ' 'precede the conversion |\n' '| | (overrides a "space" ' 'flag). |\n' '+-----------+-----------------------------------------------------------------------+\n' '\n' 'A length modifier ("h", "l", or "L") may be present, but is ' 'ignored as\n' 'it is not necessary for Python -- so e.g. "%ld" is identical to ' '"%d".\n' '\n' 'The conversion types are:\n' '\n' '+--------------+-------------------------------------------------------+---------+\n' '| Conversion | ' 'Meaning | Notes ' '|\n' '+==============+=======================================================+=========+\n' '| "\'d\'" | Signed integer ' 'decimal. | |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'i\'" | Signed integer ' 'decimal. | |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'o\'" | Signed octal ' 'value. | (1) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'u\'" | Obsolete type -- it is identical to ' '"\'d\'". | (7) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'x\'" | Signed hexadecimal ' '(lowercase). | (2) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'X\'" | Signed hexadecimal ' '(uppercase). | (2) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'e\'" | Floating point exponential format ' '(lowercase). | (3) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'E\'" | Floating point exponential format ' '(uppercase). | (3) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'f\'" | Floating point decimal ' 'format. | (3) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'F\'" | Floating point decimal ' 'format. | (3) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'g\'" | Floating point format. Uses lowercase ' 'exponential | (4) |\n' '| | format if exponent is less than -4 or not less ' 'than | |\n' '| | precision, decimal format ' 'otherwise. | |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'G\'" | Floating point format. Uses uppercase ' 'exponential | (4) |\n' '| | format if exponent is less than -4 or not less ' 'than | |\n' '| | precision, decimal format ' 'otherwise. | |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'c\'" | Single character (accepts integer or single ' 'character | |\n' '| | ' 'string). | ' '|\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'r\'" | String (converts any Python object using ' 'repr()). | (5) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'s\'" | String (converts any Python object using ' '"str()"). | (6) |\n' '+--------------+-------------------------------------------------------+---------+\n' '| "\'%\'" | No argument is converted, results in a ' '"\'%\'" | |\n' '| | character in the ' 'result. | |\n' '+--------------+-------------------------------------------------------+---------+\n' '\n' 'Notes:\n' '\n' '1. The alternate form causes a leading zero ("\'0\'") to be ' 'inserted\n' ' between left-hand padding and the formatting of the number if ' 'the\n' ' leading character of the result is not already a zero.\n' '\n' '2. The alternate form causes a leading "\'0x\'" or "\'0X\'" ' '(depending\n' ' on whether the "\'x\'" or "\'X\'" format was used) to be ' 'inserted\n' ' before the first digit.\n' '\n' '3. The alternate form causes the result to always contain a ' 'decimal\n' ' point, even if no digits follow it.\n' '\n' ' The precision determines the number of digits after the ' 'decimal\n' ' point and defaults to 6.\n' '\n' '4. The alternate form causes the result to always contain a ' 'decimal\n' ' point, and trailing zeroes are not removed as they would ' 'otherwise\n' ' be.\n' '\n' ' The precision determines the number of significant digits ' 'before\n' ' and after the decimal point and defaults to 6.\n' '\n' '5. The "%r" conversion was added in Python 2.0.\n' '\n' ' The precision determines the maximal number of characters ' 'used.\n' '\n' '6. If the object or format provided is a "unicode" string, the\n' ' resulting string will also be "unicode".\n' '\n' ' The precision determines the maximal number of characters ' 'used.\n' '\n' '7. See **PEP 237**.\n' '\n' 'Since Python strings have an explicit length, "%s" conversions ' 'do not\n' 'assume that "\'\\0\'" is the end of the string.\n' '\n' 'Changed in version 2.7: "%f" conversions for numbers whose ' 'absolute\n' 'value is over 1e50 are no longer replaced by "%g" conversions.\n' '\n' 'Additional string operations are defined in standard modules ' '"string"\n' 'and "re".\n' '\n' '\n' 'XRange Type\n' '===========\n' '\n' 'The "xrange" type is an immutable sequence which is commonly ' 'used for\n' 'looping. The advantage of the "xrange" type is that an ' '"xrange"\n' 'object will always take the same amount of memory, no matter the ' 'size\n' 'of the range it represents. There are no consistent ' 'performance\n' 'advantages.\n' '\n' 'XRange objects have very little behavior: they only support ' 'indexing,\n' 'iteration, and the "len()" function.\n' '\n' '\n' 'Mutable Sequence Types\n' '======================\n' '\n' 'List and "bytearray" objects support additional operations that ' 'allow\n' 'in-place modification of the object. Other mutable sequence ' 'types\n' '(when added to the language) should also support these ' 'operations.\n' 'Strings and tuples are immutable sequence types: such objects ' 'cannot\n' 'be modified once created. The following operations are defined ' 'on\n' 'mutable sequence types (where *x* is an arbitrary object):\n' '\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| Operation | ' 'Result | Notes |\n' '+================================+==================================+=======================+\n' '| "s[i] = x" | item *i* of *s* is replaced ' 'by | |\n' '| | ' '*x* | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s[i:j] = t" | slice of *s* from *i* to *j* ' 'is | |\n' '| | replaced by the contents of ' 'the | |\n' '| | iterable ' '*t* | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "del s[i:j]" | same as "s[i:j] = ' '[]" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' 'are | (1) |\n' '| | replaced by those of ' '*t* | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "del s[i:j:k]" | removes the elements ' 'of | |\n' '| | "s[i:j:k]" from the ' 'list | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.append(x)" | same as "s[len(s):len(s)] = ' '[x]" | (2) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.extend(t)" or "s += t" | for the most part the same ' 'as | (3) |\n' '| | "s[len(s):len(s)] = ' 't" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s *= n" | updates *s* with its ' 'contents | (11) |\n' '| | repeated *n* ' 'times | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.count(x)" | return number of *i*\'s for ' 'which | |\n' '| | "s[i] == ' 'x" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.index(x[, i[, j]])" | return smallest *k* such ' 'that | (4) |\n' '| | "s[k] == x" and "i <= k < ' 'j" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.insert(i, x)" | same as "s[i:i] = ' '[x]" | (5) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.pop([i])" | same as "x = s[i]; del ' 's[i]; | (6) |\n' '| | return ' 'x" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.remove(x)" | same as "del ' 's[s.index(x)]" | (4) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.reverse()" | reverses the items of *s* ' 'in | (7) |\n' '| | ' 'place | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.sort([cmp[, key[, | sort the items of *s* in ' 'place | (7)(8)(9)(10) |\n' '| reverse]]])" ' '| | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '\n' 'Notes:\n' '\n' '1. *t* must have the same length as the slice it is replacing.\n' '\n' '2. The C implementation of Python has historically accepted\n' ' multiple parameters and implicitly joined them into a tuple; ' 'this\n' ' no longer works in Python 2.0. Use of this misfeature has ' 'been\n' ' deprecated since Python 1.4.\n' '\n' '3. *t* can be any iterable object.\n' '\n' '4. Raises "ValueError" when *x* is not found in *s*. When a\n' ' negative index is passed as the second or third parameter to ' 'the\n' ' "index()" method, the list length is added, as for slice ' 'indices.\n' ' If it is still negative, it is truncated to zero, as for ' 'slice\n' ' indices.\n' '\n' ' Changed in version 2.3: Previously, "index()" didn\'t have ' 'arguments\n' ' for specifying start and stop positions.\n' '\n' '5. When a negative index is passed as the first parameter to ' 'the\n' ' "insert()" method, the list length is added, as for slice ' 'indices.\n' ' If it is still negative, it is truncated to zero, as for ' 'slice\n' ' indices.\n' '\n' ' Changed in version 2.3: Previously, all negative indices ' 'were\n' ' truncated to zero.\n' '\n' '6. The "pop()" method\'s optional argument *i* defaults to "-1", ' 'so\n' ' that by default the last item is removed and returned.\n' '\n' '7. The "sort()" and "reverse()" methods modify the list in ' 'place\n' ' for economy of space when sorting or reversing a large list. ' 'To\n' " remind you that they operate by side effect, they don't " 'return the\n' ' sorted or reversed list.\n' '\n' '8. The "sort()" method takes optional arguments for controlling ' 'the\n' ' comparisons.\n' '\n' ' *cmp* specifies a custom comparison function of two arguments ' '(list\n' ' items) which should return a negative, zero or positive ' 'number\n' ' depending on whether the first argument is considered smaller ' 'than,\n' ' equal to, or larger than the second argument: "cmp=lambda ' 'x,y:\n' ' cmp(x.lower(), y.lower())". The default value is "None".\n' '\n' ' *key* specifies a function of one argument that is used to ' 'extract\n' ' a comparison key from each list element: "key=str.lower". ' 'The\n' ' default value is "None".\n' '\n' ' *reverse* is a boolean value. If set to "True", then the ' 'list\n' ' elements are sorted as if each comparison were reversed.\n' '\n' ' In general, the *key* and *reverse* conversion processes are ' 'much\n' ' faster than specifying an equivalent *cmp* function. This ' 'is\n' ' because *cmp* is called multiple times for each list element ' 'while\n' ' *key* and *reverse* touch each element only once. Use\n' ' "functools.cmp_to_key()" to convert an old-style *cmp* ' 'function to\n' ' a *key* function.\n' '\n' ' Changed in version 2.3: Support for "None" as an equivalent ' 'to\n' ' omitting *cmp* was added.\n' '\n' ' Changed in version 2.4: Support for *key* and *reverse* was ' 'added.\n' '\n' '9. Starting with Python 2.3, the "sort()" method is guaranteed ' 'to\n' ' be stable. A sort is stable if it guarantees not to change ' 'the\n' ' relative order of elements that compare equal --- this is ' 'helpful\n' ' for sorting in multiple passes (for example, sort by ' 'department,\n' ' then by salary grade).\n' '\n' '10. **CPython implementation detail:** While a list is being\n' ' sorted, the effect of attempting to mutate, or even inspect, ' 'the\n' ' list is undefined. The C implementation of Python 2.3 and ' 'newer\n' ' makes the list appear empty for the duration, and raises\n' ' "ValueError" if it can detect that the list has been ' 'mutated\n' ' during a sort.\n' '\n' '11. The value *n* is an integer, or an object implementing\n' ' "__index__()". Zero and negative values of *n* clear the\n' ' sequence. Items in the sequence are not copied; they are\n' ' referenced multiple times, as explained for "s * n" under ' 'Sequence\n' ' Types --- str, unicode, list, tuple, bytearray, buffer, ' 'xrange.\n', 'typesseq-mutable': '\n' 'Mutable Sequence Types\n' '**********************\n' '\n' 'List and "bytearray" objects support additional ' 'operations that allow\n' 'in-place modification of the object. Other mutable ' 'sequence types\n' '(when added to the language) should also support these ' 'operations.\n' 'Strings and tuples are immutable sequence types: such ' 'objects cannot\n' 'be modified once created. The following operations are ' 'defined on\n' 'mutable sequence types (where *x* is an arbitrary ' 'object):\n' '\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| Operation | ' 'Result | Notes ' '|\n' '+================================+==================================+=======================+\n' '| "s[i] = x" | item *i* of *s* is ' 'replaced by | |\n' '| | ' '*x* | ' '|\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s[i:j] = t" | slice of *s* from *i* ' 'to *j* is | |\n' '| | replaced by the ' 'contents of the | |\n' '| | iterable ' '*t* | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "del s[i:j]" | same as "s[i:j] = ' '[]" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s[i:j:k] = t" | the elements of ' '"s[i:j:k]" are | (1) |\n' '| | replaced by those of ' '*t* | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "del s[i:j:k]" | removes the elements ' 'of | |\n' '| | "s[i:j:k]" from the ' 'list | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.append(x)" | same as ' '"s[len(s):len(s)] = [x]" | (2) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.extend(t)" or "s += t" | for the most part the ' 'same as | (3) |\n' '| | "s[len(s):len(s)] = ' 't" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s *= n" | updates *s* with its ' 'contents | (11) |\n' '| | repeated *n* ' 'times | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.count(x)" | return number of ' "*i*'s for which | |\n" '| | "s[i] == ' 'x" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.index(x[, i[, j]])" | return smallest *k* ' 'such that | (4) |\n' '| | "s[k] == x" and "i <= ' 'k < j" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.insert(i, x)" | same as "s[i:i] = ' '[x]" | (5) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.pop([i])" | same as "x = s[i]; ' 'del s[i]; | (6) |\n' '| | return ' 'x" | |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.remove(x)" | same as "del ' 's[s.index(x)]" | (4) |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.reverse()" | reverses the items of ' '*s* in | (7) |\n' '| | ' 'place | ' '|\n' '+--------------------------------+----------------------------------+-----------------------+\n' '| "s.sort([cmp[, key[, | sort the items of *s* ' 'in place | (7)(8)(9)(10) |\n' '| reverse]]])" ' '| ' '| |\n' '+--------------------------------+----------------------------------+-----------------------+\n' '\n' 'Notes:\n' '\n' '1. *t* must have the same length as the slice it is ' 'replacing.\n' '\n' '2. The C implementation of Python has historically ' 'accepted\n' ' multiple parameters and implicitly joined them into a ' 'tuple; this\n' ' no longer works in Python 2.0. Use of this ' 'misfeature has been\n' ' deprecated since Python 1.4.\n' '\n' '3. *t* can be any iterable object.\n' '\n' '4. Raises "ValueError" when *x* is not found in *s*. ' 'When a\n' ' negative index is passed as the second or third ' 'parameter to the\n' ' "index()" method, the list length is added, as for ' 'slice indices.\n' ' If it is still negative, it is truncated to zero, as ' 'for slice\n' ' indices.\n' '\n' ' Changed in version 2.3: Previously, "index()" didn\'t ' 'have arguments\n' ' for specifying start and stop positions.\n' '\n' '5. When a negative index is passed as the first ' 'parameter to the\n' ' "insert()" method, the list length is added, as for ' 'slice indices.\n' ' If it is still negative, it is truncated to zero, as ' 'for slice\n' ' indices.\n' '\n' ' Changed in version 2.3: Previously, all negative ' 'indices were\n' ' truncated to zero.\n' '\n' '6. The "pop()" method\'s optional argument *i* defaults ' 'to "-1", so\n' ' that by default the last item is removed and ' 'returned.\n' '\n' '7. The "sort()" and "reverse()" methods modify the list ' 'in place\n' ' for economy of space when sorting or reversing a ' 'large list. To\n' ' remind you that they operate by side effect, they ' "don't return the\n" ' sorted or reversed list.\n' '\n' '8. The "sort()" method takes optional arguments for ' 'controlling the\n' ' comparisons.\n' '\n' ' *cmp* specifies a custom comparison function of two ' 'arguments (list\n' ' items) which should return a negative, zero or ' 'positive number\n' ' depending on whether the first argument is considered ' 'smaller than,\n' ' equal to, or larger than the second argument: ' '"cmp=lambda x,y:\n' ' cmp(x.lower(), y.lower())". The default value is ' '"None".\n' '\n' ' *key* specifies a function of one argument that is ' 'used to extract\n' ' a comparison key from each list element: ' '"key=str.lower". The\n' ' default value is "None".\n' '\n' ' *reverse* is a boolean value. If set to "True", then ' 'the list\n' ' elements are sorted as if each comparison were ' 'reversed.\n' '\n' ' In general, the *key* and *reverse* conversion ' 'processes are much\n' ' faster than specifying an equivalent *cmp* function. ' 'This is\n' ' because *cmp* is called multiple times for each list ' 'element while\n' ' *key* and *reverse* touch each element only once. ' 'Use\n' ' "functools.cmp_to_key()" to convert an old-style ' '*cmp* function to\n' ' a *key* function.\n' '\n' ' Changed in version 2.3: Support for "None" as an ' 'equivalent to\n' ' omitting *cmp* was added.\n' '\n' ' Changed in version 2.4: Support for *key* and ' '*reverse* was added.\n' '\n' '9. Starting with Python 2.3, the "sort()" method is ' 'guaranteed to\n' ' be stable. A sort is stable if it guarantees not to ' 'change the\n' ' relative order of elements that compare equal --- ' 'this is helpful\n' ' for sorting in multiple passes (for example, sort by ' 'department,\n' ' then by salary grade).\n' '\n' '10. **CPython implementation detail:** While a list is ' 'being\n' ' sorted, the effect of attempting to mutate, or even ' 'inspect, the\n' ' list is undefined. The C implementation of Python ' '2.3 and newer\n' ' makes the list appear empty for the duration, and ' 'raises\n' ' "ValueError" if it can detect that the list has been ' 'mutated\n' ' during a sort.\n' '\n' '11. The value *n* is an integer, or an object ' 'implementing\n' ' "__index__()". Zero and negative values of *n* ' 'clear the\n' ' sequence. Items in the sequence are not copied; ' 'they are\n' ' referenced multiple times, as explained for "s * n" ' 'under Sequence\n' ' Types --- str, unicode, list, tuple, bytearray, ' 'buffer, xrange.\n', 'unary': '\n' 'Unary arithmetic and bitwise operations\n' '***************************************\n' '\n' 'All unary arithmetic and bitwise operations have the same ' 'priority:\n' '\n' ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' '\n' 'The unary "-" (minus) operator yields the negation of its numeric\n' 'argument.\n' '\n' 'The unary "+" (plus) operator yields its numeric argument ' 'unchanged.\n' '\n' 'The unary "~" (invert) operator yields the bitwise inversion of ' 'its\n' 'plain or long integer argument. The bitwise inversion of "x" is\n' 'defined as "-(x+1)". It only applies to integral numbers.\n' '\n' 'In all three cases, if the argument does not have the proper type, ' 'a\n' '"TypeError" exception is raised.\n', 'while': '\n' 'The "while" statement\n' '*********************\n' '\n' 'The "while" statement is used for repeated execution as long as an\n' 'expression is true:\n' '\n' ' while_stmt ::= "while" expression ":" suite\n' ' ["else" ":" suite]\n' '\n' 'This repeatedly tests the expression and, if it is true, executes ' 'the\n' 'first suite; if the expression is false (which may be the first ' 'time\n' 'it is tested) the suite of the "else" clause, if present, is ' 'executed\n' 'and the loop terminates.\n' '\n' 'A "break" statement executed in the first suite terminates the ' 'loop\n' 'without executing the "else" clause\'s suite. A "continue" ' 'statement\n' 'executed in the first suite skips the rest of the suite and goes ' 'back\n' 'to testing the expression.\n', 'with': '\n' 'The "with" statement\n' '********************\n' '\n' 'New in version 2.5.\n' '\n' 'The "with" statement is used to wrap the execution of a block with\n' 'methods defined by a context manager (see section With Statement\n' 'Context Managers). This allows common "try"..."except"..."finally"\n' 'usage patterns to be encapsulated for convenient reuse.\n' '\n' ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' ' with_item ::= expression ["as" target]\n' '\n' 'The execution of the "with" statement with one "item" proceeds as\n' 'follows:\n' '\n' '1. The context expression (the expression given in the "with_item")\n' ' is evaluated to obtain a context manager.\n' '\n' '2. The context manager\'s "__exit__()" is loaded for later use.\n' '\n' '3. The context manager\'s "__enter__()" method is invoked.\n' '\n' '4. If a target was included in the "with" statement, the return\n' ' value from "__enter__()" is assigned to it.\n' '\n' ' Note: The "with" statement guarantees that if the "__enter__()"\n' ' method returns without an error, then "__exit__()" will always ' 'be\n' ' called. Thus, if an error occurs during the assignment to the\n' ' target list, it will be treated the same as an error occurring\n' ' within the suite would be. See step 6 below.\n' '\n' '5. The suite is executed.\n' '\n' '6. The context manager\'s "__exit__()" method is invoked. If an\n' ' exception caused the suite to be exited, its type, value, and\n' ' traceback are passed as arguments to "__exit__()". Otherwise, ' 'three\n' ' "None" arguments are supplied.\n' '\n' ' If the suite was exited due to an exception, and the return ' 'value\n' ' from the "__exit__()" method was false, the exception is ' 'reraised.\n' ' If the return value was true, the exception is suppressed, and\n' ' execution continues with the statement following the "with"\n' ' statement.\n' '\n' ' If the suite was exited for any reason other than an exception, ' 'the\n' ' return value from "__exit__()" is ignored, and execution ' 'proceeds\n' ' at the normal location for the kind of exit that was taken.\n' '\n' 'With more than one item, the context managers are processed as if\n' 'multiple "with" statements were nested:\n' '\n' ' with A() as a, B() as b:\n' ' suite\n' '\n' 'is equivalent to\n' '\n' ' with A() as a:\n' ' with B() as b:\n' ' suite\n' '\n' 'Note: In Python 2.5, the "with" statement is only allowed when the\n' ' "with_statement" feature has been enabled. It is always enabled ' 'in\n' ' Python 2.6.\n' '\n' 'Changed in version 2.7: Support for multiple context expressions.\n' '\n' 'See also:\n' '\n' ' **PEP 343** - The "with" statement\n' ' The specification, background, and examples for the Python ' '"with"\n' ' statement.\n', 'yield': '\n' 'The "yield" statement\n' '*********************\n' '\n' ' yield_stmt ::= yield_expression\n' '\n' 'The "yield" statement is only used when defining a generator ' 'function,\n' 'and is only used in the body of the generator function. Using a\n' '"yield" statement in a function definition is sufficient to cause ' 'that\n' 'definition to create a generator function instead of a normal\n' 'function.\n' '\n' 'When a generator function is called, it returns an iterator known ' 'as a\n' 'generator iterator, or more commonly, a generator. The body of ' 'the\n' "generator function is executed by calling the generator's " '"next()"\n' 'method repeatedly until it raises an exception.\n' '\n' 'When a "yield" statement is executed, the state of the generator ' 'is\n' 'frozen and the value of "expression_list" is returned to ' '"next()"\'s\n' 'caller. By "frozen" we mean that all local state is retained,\n' 'including the current bindings of local variables, the instruction\n' 'pointer, and the internal evaluation stack: enough information is\n' 'saved so that the next time "next()" is invoked, the function can\n' 'proceed exactly as if the "yield" statement were just another ' 'external\n' 'call.\n' '\n' 'As of Python version 2.5, the "yield" statement is now allowed in ' 'the\n' '"try" clause of a "try" ... "finally" construct. If the generator ' 'is\n' 'not resumed before it is finalized (by reaching a zero reference ' 'count\n' "or by being garbage collected), the generator-iterator's " '"close()"\n' 'method will be called, allowing any pending "finally" clauses to\n' 'execute.\n' '\n' 'For full details of "yield" semantics, refer to the Yield ' 'expressions\n' 'section.\n' '\n' 'Note: In Python 2.2, the "yield" statement was only allowed when ' 'the\n' ' "generators" feature has been enabled. This "__future__" import\n' ' statement was used to enable the feature:\n' '\n' ' from __future__ import generators\n' '\n' 'See also:\n' '\n' ' **PEP 255** - Simple Generators\n' ' The proposal for adding generators and the "yield" statement ' 'to\n' ' Python.\n' '\n' ' **PEP 342** - Coroutines via Enhanced Generators\n' ' The proposal that, among other generator enhancements, ' 'proposed\n' ' allowing "yield" to appear inside a "try" ... "finally" ' 'block.\n'} PKe&1]tkk topics.pycnu[ |fc@s3iOdd6dd6dd6dd6dd 6d d 6d d 6dd6dd6dd6dd6dd6dd6dd6dd6dd6d d!6d"d#6d$d%6d&d'6d(d)6d*d+6d,d-6d.d/6d0d16d2d36d4d56d6d76d8d96d:d;6d<d=6d>d?6d@dA6dBdC6dDdE6dFdG6dHdI6dJdK6dLdM6dNdO6dPdQ6d:dR6dSdT6dUdV6dWdX6dYdZ6d[d\6d]d^6d_d`6dadb6dcdd6dedf6dgdh6didj6dkdl6dmdn6dodp6dqdr6dsdt6dudv6dwdx6dydz6d{d|6d}d~6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6ZdS(st The "assert" statement ********************** Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= "assert" expression ["," expression] The simple form, "assert expression", is equivalent to if __debug__: if not expression: raise AssertionError The extended form, "assert expression1, expression2", is equivalent to if __debug__: if not expression1: raise AssertionError(expression2) These equivalences assume that "__debug__" and "AssertionError" refer to the built-in variables with those names. In the current implementation, the built-in variable "__debug__" is "True" under normal circumstances, "False" when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace. Assignments to "__debug__" are illegal. The value for the built-in variable is determined when the interpreter starts. tasserts Assignment statements ********************* Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" target_list ")" | "[" [target_list] "]" | attributeref | subscription | slicing (See section Primaries for the syntax definitions for the last three symbols.) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy). Assignment of an object to a target list is recursively defined as follows. * If the target list is a single target: The object is assigned to that target. * If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): * If the name does not occur in a "global" statement in the current code block: the name is bound to the object in the current local namespace. * Otherwise: the name is bound to the object in the current global namespace. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is a target list enclosed in parentheses or in square brackets: The object must be an iterable with the same number of items as there are targets in the target list, and its items are assigned, from left to right, to the corresponding targets. * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, "TypeError" is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily "AttributeError"). Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression, "a.x" can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS target "a.x" is always set as an instance attribute, creating it if necessary. Thus, the two occurrences of "a.x" do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment: class Cls: x = 3 # class variable inst = Cls() inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3 This description does not necessarily apply to descriptor attributes, such as properties created with "property()". * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list), the subscript must yield a plain integer. If it is negative, the sequence's length is added to it. The resulting value must be a nonnegative integer less than the sequence's length, and the sequence is asked to assign the assigned object to its item with that index. If the index is out of range, "IndexError" is raised (assignment to a subscripted sequence cannot add new items to a list). If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping's key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed). * If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence's length. The bounds should evaluate to (small) integers. If either bound is negative, the sequence's length is added to it. The resulting bounds are clipped to lie between zero and the sequence's length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the object allows it. **CPython implementation detail:** In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages. WARNING: Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are 'safe' (for example "a, b = b, a" swaps two variables), overlaps *within* the collection of assigned-to variables are not safe! For instance, the following program prints "[0, 2]": x = [0, 1] i = 0 i, x[i] = 1, 2 print x Augmented assignment statements =============================== Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions for the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. t assignments Identifiers (Names) ******************* An identifier occurring as an atom is a name. See section 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. **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. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier "__spam" occurring in a class named "Ham" will be transformed to "_Ham__spam". This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done. satom-identifierss Literals ******** Python supports string literals and various numeric literals: literal ::= stringliteral | integer | longinteger | floatnumber | imagnumber Evaluation of a literal yields an object of the given type (string, integer, long integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section Literals for details. 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. s atom-literalssU* Customizing attribute access **************************** The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for "self"). "name" is the attribute name. This method should return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control in new-style classes. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should not simply execute "self.name = value" --- this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., "self.__dict__[name] = value". For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. More attribute access for new-style classes =========================================== The following methods only apply to new-style classes. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup for new-style classes. Implementing Descriptors ======================== The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner's class dictionary or in the class dictionary for one of its parents). In the examples below, "the attribute" refers to the attribute whose name is the key of the property in the owner class' "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. Invoking Descriptors ==================== In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass "object()" or "type()"). The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to a new-style object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a new-style class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object's instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ ========= By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances. The default can be overridden by defining *__slots__* in a new-style class definition. The *__slots__* declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because *__dict__* is not created for each instance. __slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. If defined in a new-style class, *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. New in version 2.2. Notes on using *__slots__* * When inheriting from a class without *__slots__*, the *__dict__* attribute of that class will always be accessible, so a *__slots__* definition in the subclass is meaningless. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__dict__'" to the *__slots__* declaration would not enable the assignment of new attributes not specifically listed in the sequence of instance variable names. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__weakref__'" to the *__slots__* declaration would not enable support for weak references. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is limited to the class where it is defined. As a result, subclasses will have a *__dict__* unless they also define *__slots__* (which must only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from "variable-length" built-in types such as "long", "str" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. Changed in version 2.6: Previously, *__class__* assignment raised an error if either new or old class had *__slots__*. sattribute-accesss_ Attribute references ******************** An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, e.g., a module, list, or an instance. This object is then asked to produce the attribute whose name is the identifier. If this attribute is not available, the exception "AttributeError" is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. sattribute-referencess Augmented assignment statements ******************************* Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions for the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. t augassignsn Binary arithmetic operations **************************** The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non- numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr The "*" (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer (plain or long) and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. The "/" (division) and "//" (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the 'floor' function applied to the result. Division by zero raises the "ZeroDivisionError" exception. The "%" (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the "ZeroDivisionError" exception. The arguments may be floating point numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 + 0.34".) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2]. The integer division and modulo operators are connected by the following identity: "x == (x/y)*y + (x%y)". Integer division and modulo are also connected with the built-in function "divmod()": "divmod(x, y) == (x/y, x%y)". These identities don't hold for floating point numbers; there similar identities hold approximately where "x/y" is replaced by "floor(x/y)" or "floor(x/y) - 1" [3]. In addition to performing the modulo operation on numbers, the "%" operator is also overloaded by string and unicode objects to perform string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section String Formatting Operations. Deprecated since version 2.3: The floor division operator, the modulo operator, and the "divmod()" function are no longer defined for complex numbers. Instead, convert to a floating point number using the "abs()" function if appropriate. The "+" (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The "-" (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. tbinarys Binary bitwise operations ************************* Each of the three bitwise operations has a different priority level: and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr The "&" operator yields the bitwise AND of its arguments, which must be plain or long integers. The arguments are converted to a common type. The "^" operator yields the bitwise XOR (exclusive OR) of its arguments, which must be plain or long integers. The arguments are converted to a common type. The "|" operator yields the bitwise (inclusive) OR of its arguments, which must be plain or long integers. The arguments are converted to a common type. tbitwises~ Code Objects ************ Code objects are used by the implementation to represent "pseudo- compiled" executable Python code such as a function body. They differ from function objects because they don't contain a reference to their global execution environment. Code objects are returned by the built- in "compile()" function and can be extracted from function objects through their "func_code" attribute. See also the "code" module. A code object can be executed or evaluated by passing it (instead of a source string) to the "exec" statement or the built-in "eval()" function. See The standard type hierarchy for more information. sbltin-code-objectssE The Ellipsis Object ******************* This object is used by extended slice notation (see Slicings). It supports no special operations. There is exactly one ellipsis object, named "Ellipsis" (a built-in name). It is written as "Ellipsis". When in a subscript, it can also be written as "...", for example "seq[...]". sbltin-ellipsis-objects+ File Objects ************ File objects are implemented using C's "stdio" package and can be created with the built-in "open()" function. File objects are also returned by some other built-in functions and methods, such as "os.popen()" and "os.fdopen()" and the "makefile()" method of socket objects. Temporary files can be created using the "tempfile" module, and high-level file operations such as copying, moving, and deleting files and directories can be achieved with the "shutil" module. When a file operation fails for an I/O-related reason, the exception "IOError" is raised. This includes situations where the operation is not defined for some reason, like "seek()" on a tty device or writing a file opened for reading. Files have the following methods: file.close() Close the file. A closed file cannot be read or written any more. Any operation which requires that the file be open will raise a "ValueError" after the file has been closed. Calling "close()" more than once is allowed. As of Python 2.5, you can avoid having to call this method explicitly if you use the "with" statement. For example, the following code will automatically close *f* when the "with" block is exited: from __future__ import with_statement # This isn't required in Python 2.6 with open("hello.txt") as f: for line in f: print line, In older versions of Python, you would have needed to do this to get the same effect: f = open("hello.txt") try: for line in f: print line, finally: f.close() Note: Not all "file-like" types in Python support use as a context manager for the "with" statement. If your code is intended to work with any file-like object, you can use the function "contextlib.closing()" instead of using the object directly. file.flush() Flush the internal buffer, like "stdio"'s "fflush()". This may be a no-op on some file-like objects. Note: "flush()" does not necessarily write the file's data to disk. Use "flush()" followed by "os.fsync()" to ensure this behavior. file.fileno() Return the integer "file descriptor" that is used by the underlying implementation to request I/O operations from the operating system. This can be useful for other, lower level interfaces that use file descriptors, such as the "fcntl" module or "os.read()" and friends. Note: File-like objects which do not have a real file descriptor should *not* provide this method! file.isatty() Return "True" if the file is connected to a tty(-like) device, else "False". Note: If a file-like object is not associated with a real file, this method should *not* be implemented. file.next() A file object is its own iterator, for example "iter(f)" returns *f* (unless *f* is closed). When a file is used as an iterator, typically in a "for" loop (for example, "for line in f: print line.strip()"), the "next()" method is called repeatedly. This method returns the next input line, or raises "StopIteration" when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing). In order to make a "for" loop the most efficient way of looping over the lines of a file (a very common operation), the "next()" method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining "next()" with other file methods (like "readline()") does not work right. However, using "seek()" to reposition the file to an absolute position will flush the read-ahead buffer. New in version 2.3. file.read([size]) Read at most *size* bytes from the file (less if the read hits EOF before obtaining *size* bytes). If the *size* argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function "fread()" more than once in an effort to acquire as close to *size* bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no *size* parameter was given. Note: This function is simply a wrapper for the underlying "fread()" C function, and will behave the same in corner cases, such as whether the EOF value is cached. file.readline([size]) Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). [6] If the *size* argument is present and non- negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When *size* is not 0, an empty string is returned *only* when EOF is encountered immediately. Note: Unlike "stdio"'s "fgets()", the returned string contains null characters ("'\0'") if they occurred in the input. file.readlines([sizehint]) Read until EOF using "readline()" and return a list containing the lines thus read. If the optional *sizehint* argument is present, instead of reading up to EOF, whole lines totalling approximately *sizehint* bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore *sizehint* if it cannot be implemented, or cannot be implemented efficiently. file.xreadlines() This method returns the same thing as "iter(f)". New in version 2.1. Deprecated since version 2.3: Use "for line in file" instead. file.seek(offset[, whence]) Set the file's current position, like "stdio"'s "fseek()". The *whence* argument is optional and defaults to "os.SEEK_SET" or "0" (absolute file positioning); other values are "os.SEEK_CUR" or "1" (seek relative to the current position) and "os.SEEK_END" or "2" (seek relative to the file's end). There is no return value. For example, "f.seek(2, os.SEEK_CUR)" advances the position by two and "f.seek(-3, os.SEEK_END)" sets the position to the third to last. Note that if the file is opened for appending (mode "'a'" or "'a+'"), any "seek()" operations will be undone at the next write. If the file is only opened for writing in append mode (mode "'a'"), this method is essentially a no-op, but it remains useful for files opened in append mode with reading enabled (mode "'a+'"). If the file is opened in text mode (without "'b'"), only offsets returned by "tell()" are legal. Use of other offsets causes undefined behavior. Note that not all file objects are seekable. Changed in version 2.6: Passing float values as offset has been deprecated. file.tell() Return the file's current position, like "stdio"'s "ftell()". Note: On Windows, "tell()" can return illegal values (after an "fgets()") when reading files with Unix-style line-endings. Use binary mode ("'rb'") to circumvent this problem. file.truncate([size]) Truncate the file's size. If the optional *size* argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file's current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants. file.write(str) Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the "flush()" or "close()" method is called. file.writelines(sequence) Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. (The name is intended to match "readlines()"; "writelines()" does not add line separators.) Files support the iterator protocol. Each iteration returns the same result as "readline()", and iteration ends when the "readline()" method returns an empty string. File objects also offer a number of other interesting attributes. These are not required for file-like objects, but should be implemented if they make sense for the particular object. file.closed bool indicating the current state of the file object. This is a read-only attribute; the "close()" method changes the value. It may not be available on all file-like objects. file.encoding The encoding that this file uses. When Unicode strings are written to a file, they will be converted to byte strings using this encoding. In addition, when the file is connected to a terminal, the attribute gives the encoding that the terminal is likely to use (that information might be incorrect if the user has misconfigured the terminal). The attribute is read-only and may not be present on all file-like objects. It may also be "None", in which case the file uses the system default encoding for converting Unicode strings. New in version 2.3. file.errors The Unicode error handler used along with the encoding. New in version 2.6. file.mode The I/O mode for the file. If the file was created using the "open()" built-in function, this will be the value of the *mode* parameter. This is a read-only attribute and may not be present on all file-like objects. file.name If the file object was created using "open()", the name of the file. Otherwise, some string that indicates the source of the file object, of the form "<...>". This is a read-only attribute and may not be present on all file-like objects. file.newlines If Python was built with *universal newlines* enabled (the default) this read-only attribute exists, and for files opened in universal newline read mode it keeps track of the types of newlines encountered while reading the file. The values it can take are "'\r'", "'\n'", "'\r\n'", "None" (unknown, no newlines read yet) or a tuple containing all the newline types seen, to indicate that multiple newline conventions were encountered. For files not opened in universal newlines read mode the value of this attribute will be "None". file.softspace Boolean that indicates whether a space character needs to be printed before another value when using the "print" statement. Classes that are trying to simulate a file object should also have a writable "softspace" attribute, which should be initialized to zero. This will be automatic for most classes implemented in Python (care may be needed for objects that override attribute access); types implemented in C will have to provide a writable "softspace" attribute. Note: This attribute is not used to control the "print" statement, but to allow the implementation of "print" to keep track of its internal state. sbltin-file-objectss The Null Object *************** This object is returned by functions that don't explicitly return a value. It supports no special operations. There is exactly one null object, named "None" (a built-in name). It is written as "None". sbltin-null-objects3 Type Objects ************ Type objects represent the various object types. An object's type is accessed by the built-in function "type()". There are no special operations on types. The standard module "types" defines names for all standard built-in types. Types are written like this: "". sbltin-type-objectss Boolean operations ****************** or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: "False", "None", numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the "__nonzero__()" special method for a way to change this.) The operator "not" yields "True" if its argument is false, "False" otherwise. The expression "x and y" first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The expression "x or y" first evaluates *x*; if *x* is true, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. (Note that neither "and" nor "or" restrict the value and type they return to "False" and "True", but rather return the last evaluated argument. This is sometimes useful, e.g., if "s" is a string that should be replaced by a default value if it is empty, the expression "s or 'foo'" yields the desired value. Because "not" has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., "not 'foo'" yields "False", not "''".) tbooleanss% The "break" statement ********************* break_stmt ::= "break" "break" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional "else" clause if the loop has one. If a "for" loop is terminated by "break", the loop control target keeps its current value. When "break" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the loop. tbreaks Emulating callable objects ************************** object.__call__(self[, args...]) Called when the instance is "called" as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". scallable-typess Calls ***** A call calls a callable object (e.g., a *function*) with a possibly empty series of *arguments*: call ::= primary "(" [argument_list [","] | expression genexpr_for] ")" argument_list ::= positional_arguments ["," keyword_arguments] ["," "*" expression] ["," keyword_arguments] ["," "**" expression] | keyword_arguments ["," "*" expression] ["," "**" expression] | "*" expression ["," keyword_arguments] ["," "**" expression] | "**" expression positional_arguments ::= expression ("," expression)* keyword_arguments ::= keyword_item ("," keyword_item)* keyword_item ::= identifier "=" expression A trailing comma may be present after the positional and keyword arguments but does not affect the semantics. The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and certain class instances themselves are callable; extensions may define additional callable object types). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal *parameter* lists. If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a "TypeError" exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is "None", it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don't specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a "TypeError" exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. **CPython implementation detail:** An implementation may provide built-in functions whose positional parameters do not have names, even if they are 'named' for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use "PyArg_ParseTuple()" to parse their arguments. If there are more positional arguments than there are formal parameter slots, a "TypeError" exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments). If any keyword argument does not correspond to a formal parameter name, a "TypeError" exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments. If the syntax "*expression" appears in the function call, "expression" must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments *x1*, ..., *xN*, and "expression" evaluates to a sequence *y1*, ..., *yM*, this is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*. A consequence of this is that although the "*expression" syntax may appear *after* some keyword arguments, it is processed *before* the keyword arguments (and the "**expression" argument, if any -- see below). So: >>> def f(a, b): ... print a, b ... >>> f(b=1, *(2,)) 2 1 >>> f(a=1, *(2,)) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'a' >>> f(1, *(2,)) 1 2 It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise. If the syntax "**expression" appears in the function call, "expression" must evaluate to a mapping, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both "expression" and as an explicit keyword argument, a "TypeError" exception is raised. Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Formal parameters using the syntax "(sublist)" cannot be used as keyword argument names; the outermost sublist corresponds to a single unnamed argument slot, and the argument value is assigned to the sublist using the usual tuple assignment rules after all other parameter processing is done. A call always returns some value, possibly "None", unless it raises an exception. How this value is computed depends on the type of the callable object. If it is--- a user-defined function: The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a "return" statement, this specifies the return value of the function call. a built-in function or method: The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods. a class object: A new instance of that class is returned. a class instance method: The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument. a class instance: The class must define a "__call__()" method; the effect is then the same as if that method was called. tcallssJ Class definitions ***************** A class definition defines a class object (see section The standard type hierarchy): classdef ::= "class" classname [inheritance] ":" suite inheritance ::= "(" [expression_list] ")" classname ::= identifier A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class's suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class's suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. **Programmer's note:** Variables defined in the class definition are class variables; they are shared by all instances. To create instance variables, they can be set in a method with "self.name = value". Both class and instance variables are accessible through the notation ""self.name"", and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For *new-style class*es, descriptors can be used to create instance variables with different implementation details. Class definitions, like function definitions, may be wrapped by one or more *decorator* expressions. The evaluation rules for the decorator expressions are the same as for functions. The result must be a class object, which is then bound to the class name. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] Currently, control "flows off the end" except in the case of an exception or the execution of a "return", "continue", or "break" statement. [3] A string literal appearing as the first statement in the function body is transformed into the function's "__doc__" attribute and therefore the function's *docstring*. [4] A string literal appearing as the first statement in the class body is transformed into the namespace's "__doc__" item and therefore the class's *docstring*. tclasss$ Comparisons *********** Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like "a < b < c" have the interpretation that is conventional in mathematics: comparison ::= or_expr ( comp_operator or_expr )* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" | "is" ["not"] | ["not"] "in" Comparisons yield boolean values: "True" or "False". Comparisons can be chained arbitrarily, e.g., "x < y <= z" is equivalent to "x < y and y <= z", except that "y" is evaluated only once (but in both cases "z" is not evaluated at all when "x < y" is found to be false). Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ..., *opN* are comparison operators, then "a op1 b op2 c ... y opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except that each expression is evaluated at most once. Note that "a op1 b op2 c" doesn't imply any kind of comparison between *a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though perhaps not pretty). The forms "<>" and "!=" are equivalent; for consistency with C, "!=" is preferred; where "!=" is mentioned below "<>" is also accepted. The "<>" spelling is considered obsolescent. Value comparisons ================= The operators "<", ">", "==", ">=", "<=", and "!=" compare the values of two objects. The objects do not need to have the same type. Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object's value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation. Types can customize their comparison behavior by implementing a "__cmp__()" method or *rich comparison methods* like "__lt__()", described in Basic customization. The default behavior for equality comparison ("==" and "!=") is based on the identity of the objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of instances with different identities results in inequality. A motivation for this default behavior is the desire that all objects should be reflexive (i.e. "x is y" implies "x == y"). The default order comparison ("<", ">", "<=", and ">=") gives a consistent but arbitrary order. (This unusual definition of comparison was used to simplify the definition of operations like sorting and the "in" and "not in" operators. In the future, the comparison rules for objects of different types are likely to change.) The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that. The following list describes the comparison behavior of the most important built-in types. * Numbers of built-in numeric types (Numeric Types --- int, float, long, complex) and of the standard library types "fractions.Fraction" and "decimal.Decimal" can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision. * Strings (instances of "str" or "unicode") compare lexicographically using the numeric equivalents (the result of the built-in function "ord()") of their characters. [4] When comparing an 8-bit string and a Unicode string, the 8-bit string is converted to Unicode. If the conversion fails, the strings are considered unequal. * Instances of "tuple" or "list" can be compared only within each of their types. Equality comparison across these types results in unequality, and ordering comparison across these types gives an arbitrary order. These sequences compare lexicographically using comparison of corresponding elements, whereby reflexivity of the elements is enforced. In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element "x", "x == x" is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison. Lexicographical comparison between built-in collections works as follows: * For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, "[1,2] == (1,2)" is false because the type is not the same). * Collections are ordered the same as their first unequal elements (for example, "cmp([1,2,x], [1,2,y])" returns the same as "cmp(x,y)"). If a corresponding element does not exist, the shorter collection is ordered first (for example, "[1,2] < [1,2,3]" is true). * Mappings (instances of "dict") compare equal if and only if they have equal *(key, value)* pairs. Equality comparison of the keys and values enforces reflexivity. Outcomes other than equality are resolved consistently, but are not otherwise defined. [5] * Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: * Equality comparison should be reflexive. In other words, identical objects should compare equal: "x is y" implies "x == y" * Comparison should be symmetric. In other words, the following expressions should have the same result: "x == y" and "y == x" "x != y" and "y != x" "x < y" and "y > x" "x <= y" and "y >= x" * Comparison should be transitive. The following (non-exhaustive) examples illustrate that: "x > y and y > z" implies "x > z" "x < y and y <= z" implies "x < z" * Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: "x == y" and "not x != y" "x < y" and "not x >= y" (for total ordering) "x > y" and "not x <= y" (for total ordering) The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the "total_ordering()" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. Python does not enforce these consistency rules. Membership test operations ========================== The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. "x is not y" yields the inverse truth value. [6] t comparisonsspP Compound statements ******************* Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The "if", "while" and "for" statements implement traditional control flow constructs. "try" specifies exception handlers and/or cleanup code for a group of statements. Function and class definitions are also syntactically compound statements. Compound statements consist of one or more 'clauses.' A clause consists of a header and a 'suite.' The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which "if" clause a following "else" clause would belong: if test1: if test2: print x Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the "print" statements are executed: if x < y < z: print x; print y; print z Summarizing: compound_stmt ::= if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT statement ::= stmt_list NEWLINE | compound_stmt stmt_list ::= simple_stmt (";" simple_stmt)* [";"] Note that statements always end in a "NEWLINE" possibly followed by a "DEDENT". Also note that optional continuation clauses always begin with a keyword that cannot start a statement, thus there are no ambiguities (the 'dangling "else"' problem is solved in Python by requiring nested "if" statements to be indented). The formatting of the grammar rules in the following sections places each clause on a separate line for clarity. The "if" statement ================== The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. The "while" statement ===================== The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. The "for" statement =================== The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there was no next item. The suite may assign to the variable(s) in the target list; this does not affect the next item assigned to it. The target list is not deleted when the loop is finished, but if the sequence is empty, it will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns a sequence of integers suitable to emulate the effect of Pascal's "for i := a to b do"; e.g., "range(3)" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) The "try" statement =================== The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression [("as" | ",") identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite Changed in version 2.5: In previous versions of Python, "try"..."except"..."finally" did not work. "try"..."except" had to be nested in "try"..."finally". The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) Before an except clause's suite is executed, details about the exception are assigned to three variables in the "sys" module: "sys.exc_type" receives the object identifying the exception; "sys.exc_value" receives the exception's parameter; "sys.exc_traceback" receives a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. These details are also available through the "sys.exc_info()" function, which returns a tuple "(exc_type, exc_value, exc_traceback)". Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if and when control flows off the end of the "try" clause. [2] Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a 'cleanup' handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception, it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception or executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"..."finally" statement, the "finally" clause is also executed 'on the way out.' A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation --- this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. The "with" statement ==================== New in version 2.5. The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"..."except"..."finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one "item" proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager's "__exit__()" is loaded for later use. 3. The context manager's "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager's "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Note: In Python 2.5, the "with" statement is only allowed when the "with_statement" feature has been enabled. It is always enabled in Python 2.6. Changed in version 2.7: Support for multiple context expressions. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. Function definitions ==================== A function definition defines a user-defined function object (see section The standard type hierarchy): decorated ::= decorators (classdef | funcdef) decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE funcdef ::= "def" funcname "(" [parameter_list] ")" ":" suite dotted_name ::= identifier ("." identifier)* parameter_list ::= (defparameter ",")* ( "*" identifier ["," "**" identifier] | "**" identifier | defparameter [","] ) defparameter ::= parameter ["=" expression] sublist ::= parameter ("," parameter)* [","] parameter ::= identifier | "(" sublist ")" funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [3] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code: @f1(arg) @f2 def func(): pass is equivalent to: def func(): pass func = f1(arg)(f2(func)) When one or more top-level *parameters* have the form *parameter* "=" *expression*, the function is said to have "default parameter values." For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form ""*identifier"" is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ""**identifier"" is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a ""def"" statement can be passed around or assigned to another name just like a function defined by a lambda expression. The ""def"" form is actually more powerful since it allows the execution of multiple statements. **Programmer's note:** Functions are first-class objects. A ""def"" form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. Class definitions ================= A class definition defines a class object (see section The standard type hierarchy): classdef ::= "class" classname [inheritance] ":" suite inheritance ::= "(" [expression_list] ")" classname ::= identifier A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class's suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class's suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. **Programmer's note:** Variables defined in the class definition are class variables; they are shared by all instances. To create instance variables, they can be set in a method with "self.name = value". Both class and instance variables are accessible through the notation ""self.name"", and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For *new-style class*es, descriptors can be used to create instance variables with different implementation details. Class definitions, like function definitions, may be wrapped by one or more *decorator* expressions. The evaluation rules for the decorator expressions are the same as for functions. The result must be a class object, which is then bound to the class name. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] Currently, control "flows off the end" except in the case of an exception or the execution of a "return", "continue", or "break" statement. [3] A string literal appearing as the first statement in the function body is transformed into the function's "__doc__" attribute and therefore the function's *docstring*. [4] A string literal appearing as the first statement in the class body is transformed into the namespace's "__doc__" item and therefore the class's *docstring*. tcompounds With Statement Context Managers ******************************* New in version 2.5. A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method's return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller's responsibility. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. scontext-managerss The "continue" statement ************************ continue_stmt ::= "continue" "continue" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition or "finally" clause within that loop. It continues with the next cycle of the nearest enclosing loop. When "continue" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really starting the next loop cycle. tcontinuesB Arithmetic conversions ********************** When a description of an arithmetic operator below uses the phrase "the numeric arguments are converted to a common type," the arguments are coerced using the coercion rules listed at Coercion rules. If both arguments are standard numeric types, the following coercions are applied: * If either argument is a complex number, the other is converted to complex; * otherwise, if either argument is a floating point number, the other is converted to floating point; * otherwise, if either argument is a long integer, the other is converted to long integer; * otherwise, both must be plain integers and no conversion is necessary. Some additional rules apply for certain operators (e.g., a string left argument to the '%' operator). Extensions can define their own coercions. t conversionss/ Basic customization ******************* object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass's "__new__()" method using "super(currentclass, cls).__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance's "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance's "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class's "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "BaseClass.__init__(self, [args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customise it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a "__del__()" method, the derived class's "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn't directly call "x.__del__()" --- the former decrements the reference count for "x" by one, and the latter is only called when "x"'s reference count reaches zero. Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in "sys.exc_traceback" keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in "sys.last_traceback" keeps the stack frame alive). The first situation can only be remedied by explicitly breaking the cycles; the latter two situations can be resolved by storing "None" in "sys.exc_traceback" or "sys.last_traceback". Circular references which are garbage are detected when the option cycle detector is enabled (it's on by default), but can only be cleaned up if there are no Python-level "__del__()" methods involved. Refer to the documentation for the "gc" module for more information about how "__del__()" methods are handled by the cycle detector, particularly the description of the "garbage" value. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. Also, when "__del__()" is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the "__del__()" method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, "__del__()" methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. See also the "-R" command-line option. object.__repr__(self) Called by the "repr()" built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an "informal" string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by the "str()" built-in function and by the "print" statement to compute the "informal" string representation of an object. This differs from "__repr__()" in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) New in version 2.1. These are the so-called "rich comparison" methods, and are called for comparison operators in preference to "__cmp__()" below. The correspondence between operator symbols and method names is as follows: "xy" call "x.__ne__(y)", "x>y" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. There are no implied relationships among the comparison operators. The truth of "x==y" does not imply that "x!=y" is false. Accordingly, when defining "__eq__()", one should also define "__ne__()" so that the operators will behave as expected. See the paragraph on "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, "__lt__()" and "__gt__()" are each other's reflection, "__le__()" and "__ge__()" are each other's reflection, and "__eq__()" and "__ne__()" are their own reflection. Arguments to rich comparison methods are never coerced. To automatically generate ordering operations from a single root operation, see "functools.total_ordering()". object.__cmp__(self, other) Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if "self < other", zero if "self == other", a positive integer if "self > other". If no "__cmp__()", "__eq__()" or "__ne__()" operation is defined, class instances are compared by object identity ("address"). See also the description of "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by "__cmp__()" has been removed since Python 1.5.) object.__rcmp__(self, other) Changed in version 2.1: No longer supported. object.__hash__(self) Called by built-in function "hash()" and for operations on members of hashed collections including "set", "frozenset", and "dict". "__hash__()" should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example: def __hash__(self): return hash((self.name, self.nick, self.color)) If a class does not define a "__cmp__()" or "__eq__()" method it should not define a "__hash__()" operation either; if it defines "__cmp__()" or "__eq__()" but not "__hash__()", its instances will not be usable in hashed collections. If a class defines mutable objects and implements a "__cmp__()" or "__eq__()" method, it should not implement "__hash__()", since hashable collection implementations require that an object's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket). User-defined classes have "__cmp__()" and "__hash__()" methods by default; with them, all objects compare unequal (except with themselves) and "x.__hash__()" returns a result derived from "id(x)". Classes which inherit a "__hash__()" method from a parent class but change the meaning of "__cmp__()" or "__eq__()" such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting "__hash__ = None" in the class definition. Doing so means that not only will instances of the class raise an appropriate "TypeError" when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking "isinstance(obj, collections.Hashable)" (unlike classes which define their own "__hash__()" to explicitly raise "TypeError"). Changed in version 2.5: "__hash__()" may now also return a long integer object; the 32-bit integer is then derived from the hash of that object. Changed in version 2.6: "__hash__" may now be set to "None" to explicitly flag instances of a class as unhashable. object.__nonzero__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True", or their integer equivalents "0" or "1". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__nonzero__()", all its instances are considered true. object.__unicode__(self) Called to implement "unicode()" built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding. t customizations "pdb" --- The Python Debugger ***************************** **Source code:** Lib/pdb.py ====================================================================== The module "pdb" defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible --- it is actually defined as the class "Pdb". This is currently undocumented but easily understood by reading the source. The extension interface uses the modules "bdb" and "cmd". The debugger's prompt is "(Pdb)". Typical usage to run a program under control of the debugger is: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') > (0)?() (Pdb) continue > (1)?() (Pdb) continue NameError: 'spam' > (1)?() (Pdb) "pdb.py" can also be invoked as a script to debug other scripts. For example: python -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post- mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb's state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program's exit. New in version 2.4: Restarting post-mortem behavior added. The typical usage to break into the debugger from a running program is to insert import pdb; pdb.set_trace() at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the "c" command. The typical usage to inspect a crashed program is: >>> import pdb >>> import mymodule >>> mymodule.test() Traceback (most recent call last): File "", line 1, in File "./mymodule.py", line 4, in test test2() File "./mymodule.py", line 3, in test2 print spam NameError: spam >>> pdb.pm() > ./mymodule.py(3)test2() -> print spam (Pdb) The module defines the following functions; each enters the debugger in a slightly different way: pdb.run(statement[, globals[, locals]]) Execute the *statement* (given as a string) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type "continue", or you can step through the statement using "step" or "next" (all these commands are explained below). The optional *globals* and *locals* arguments specify the environment in which the code is executed; by default the dictionary of the module "__main__" is used. (See the explanation of the "exec" statement or the "eval()" built-in function.) pdb.runeval(expression[, globals[, locals]]) Evaluate the *expression* (given as a string) under debugger control. When "runeval()" returns, it returns the value of the expression. Otherwise this function is similar to "run()". pdb.runcall(function[, argument, ...]) Call the *function* (a function or method object, not a string) with the given arguments. When "runcall()" returns, it returns whatever the function call returned. The debugger prompt appears as soon as the function is entered. pdb.set_trace() Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). pdb.post_mortem([traceback]) Enter post-mortem debugging of the given *traceback* object. If no *traceback* is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used). pdb.pm() Enter post-mortem debugging of the traceback found in "sys.last_traceback". The "run*" functions and "set_trace()" are aliases for instantiating the "Pdb" class and calling the method of the same name. If you want to access further features, you have to do this yourself: class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None) "Pdb" is the debugger class. The *completekey*, *stdin* and *stdout* arguments are passed to the underlying "cmd.Cmd" class; see the description there. The *skip* argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] Example call to enable tracing with *skip*: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 2.7: The *skip* argument. run(statement[, globals[, locals]]) runeval(expression[, globals[, locals]]) runcall(function[, argument, ...]) set_trace() See the documentation for the functions explained above. tdebuggers The "del" statement ******************* del_stmt ::= "del" target_list Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints. Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a "global" statement in the same code block. If the name is unbound, a "NameError" exception will be raised. It is illegal to delete a name from the local namespace if it occurs as a free variable in a nested block. Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object). tdels Dictionary displays ******************* A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression dict_comprehension ::= expression ":" expression comp_for A dictionary display yields a new dictionary object. If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary's value for that key will be the last one given. A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual "for" and "if" clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced. Restrictions on the types of the key values are listed earlier in section The standard type hierarchy. (To summarize, the key type should be *hashable*, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails. tdicts+ Interaction with dynamic features ********************************* There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. sdynamic-featuressE The "if" statement ****************** The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. telsesh Exceptions ********** Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" ... "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the "termination" model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Exceptions can also be identified by strings, in which case the "except" clause is selected by object identity. An arbitrary value can be raised along with the identifying string which can be passed to the handler. Note: Messages to exceptions are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. t exceptionss The "exec" statement ******************** exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] This statement supports dynamic execution of Python code. The first expression should evaluate to either a Unicode string, a *Latin-1* encoded string, an open file object, a code object, or a tuple. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1] If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed. For the interpretation of a tuple, see below. In all cases, the code that's executed is expected to be valid as file input (see section File input). Be aware that the "return" and "yield" statements may not be used outside of function definitions even within the context of code passed to the "exec" statement. In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after "in" is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, they are used for the global and local variables, respectively. If provided, *locals* can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are given as *globals* and *locals*, the code will be executed as if it were embedded in a class definition. The first expression may also be a tuple of length 2 or 3. In this case, the optional parts must be omitted. The form "exec(expr, globals)" is equivalent to "exec expr in globals", while the form "exec(expr, globals, locals)" is equivalent to "exec expr in globals, locals". The tuple form of "exec" provides compatibility with Python 3, where "exec" is a function rather than a statement. Changed in version 2.4: Formerly, *locals* was required to be a dictionary. As a side effect, an implementation may insert additional keys into the dictionaries given besides those corresponding to variable names set by the executed code. For example, the current implementation may add a reference to the dictionary of the built-in module "__builtin__" under the key "__builtins__" (!). **Programmer's hints:** dynamic evaluation of expressions is supported by the built-in function "eval()". The built-in functions "globals()" and "locals()" return the current global and local dictionary, respectively, which may be useful to pass around for use by "exec". -[ Footnotes ]- [1] Note that the parser only accepts the Unix-style end of line convention. If you are reading the code from a file, make sure to use *universal newlines* mode to convert Windows or Mac-style newlines. texecs& Execution model *************** Naming and binding ================== *Names* refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the *binding* of that name established in the innermost function block containing the use. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the '**-c**' option) is a code block. The file read by the built-in function "execfile()" is a code block. The string argument passed to the built-in function "eval()" and to the "exec" statement is a code block. The expression read and evaluated by the built-in function "input()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block's execution has completed. A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods -- this includes generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block's *environment*. If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. When a name is not found at all, a "NameError" exception is raised. If the name refers to a local variable that has not been bound, a "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, in the second position of an "except" clause header or after "as" in a "with" statement. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). It is illegal to unbind a name that is referenced by an enclosing scope; the compiler will report a "SyntaxError". Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "__builtin__". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module's dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "__builtin__" (note: no 's'); when in any other module, "__builtins__" is an alias for the dictionary of the "__builtin__" module itself. "__builtins__" can be set to a user-created dictionary to create a weak form of restricted execution. **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "__builtin__" (no 's') module and modify its attributes appropriately. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class. Names defined at the class scope are not visible in methods. Interaction with dynamic features --------------------------------- There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. Exceptions ========== Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" ... "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the "termination" model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Exceptions can also be identified by strings, in which case the "except" clause is selected by object identity. An arbitrary value can be raised along with the identifying string which can be passed to the handler. Note: Messages to exceptions are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. t execmodelsK Expression lists **************** expression_list ::= expression ( "," expression )* [","] An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. The trailing comma is required only to create a single tuple (a.k.a. a *singleton*); it is optional in all other cases. A single expression without a trailing comma doesn't create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: "()".) t exprlistss Floating point literals *********************** Floating point literals are described by the following lexical definitions: floatnumber ::= pointfloat | exponentfloat pointfloat ::= [intpart] fraction | intpart "." exponentfloat ::= (intpart | pointfloat) exponent intpart ::= digit+ fraction ::= "." digit+ exponent ::= ("e" | "E") ["+" | "-"] digit+ Note that the integer and exponent parts of floating point numbers can look like octal integers, but are interpreted using radix 10. For example, "077e010" is legal, and denotes the same number as "77e10". The allowed range of floating point literals is implementation- dependent. Some examples of floating point literals: 3.14 10. .001 1e100 3.14e-10 0e0 Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator "-" and the literal "1". tfloatingsZ The "for" statement ******************* The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there was no next item. The suite may assign to the variable(s) in the target list; this does not affect the next item assigned to it. The target list is not deleted when the loop is finished, but if the sequence is empty, it will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns a sequence of integers suitable to emulate the effect of Pascal's "for i := a to b do"; e.g., "range(3)" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) tforsQ Format String Syntax ******************** The "str.format()" method and the "Formatter" class share the same syntax for format strings (although in the case of "Formatter", subclasses can define their own format string syntax). Format strings contain "replacement fields" surrounded by curly braces "{}". Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: "{{" and "}}". The grammar for a replacement field is as follows: replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | integer] attribute_name ::= identifier element_index ::= integer | index_string index_string ::= + conversion ::= "r" | "s" format_spec ::= In less formal terms, the replacement field can start with a *field_name* that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is preceded by an exclamation point "'!'", and a *format_spec*, which is preceded by a colon "':'". These specify a non-default format for the replacement value. See also the Format Specification Mini-Language section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it's a number, it refers to a positional argument, and if it's a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings "'10'" or "':-]'") within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form "'.name'" selects the named attribute using "getattr()", while an expression of the form "'[index]'" does an index lookup using "__getitem__()". Changed in version 2.7: The positional argument specifiers can be omitted, so "'{} {}'" is equivalent to "'{0} {1}'". Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the "__format__()" method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling "__format__()", the normal formatting logic is bypassed. Two conversion flags are currently supported: "'!s'" which calls "str()" on the value, and "'!r'" which calls "repr()". Some examples: "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own "formatting mini-language" or interpretation of the *format_spec*. Most built-in types support a common formatting mini-language, which is described in the next section. A *format_spec* field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the *format_spec* string is interpreted. This allows the formatting of a value to be dynamically specified. See the Format examples section for some examples. Format Specification Mini-Language ================================== "Format specifications" are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax). They can also be passed directly to the built-in "format()" function. Each formattable type may define how the format specification is to be interpreted. Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types. A general convention is that an empty format string ("""") produces the same result as if you had called "str()" on the value. A non-empty format string typically modifies the result. The general form of a *standard format specifier* is: format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::= align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" If a valid *align* value is specified, it can be preceded by a *fill* character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (""{"" or ""}"") as the *fill* character when using the "str.format()" method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn't affect the "format()" function. The meaning of the various alignment options is as follows: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'<'" | Forces the field to be left-aligned within the available | | | space (this is the default for most objects). | +-----------+------------------------------------------------------------+ | "'>'" | Forces the field to be right-aligned within the available | | | space (this is the default for numbers). | +-----------+------------------------------------------------------------+ | "'='" | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form '+000000120'. This alignment option is only | | | valid for numeric types. It becomes the default when '0' | | | immediately precedes the field width. | +-----------+------------------------------------------------------------+ | "'^'" | Forces the field to be centered within the available | | | space. | +-----------+------------------------------------------------------------+ Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The *sign* option is only valid for number types, and can be one of the following: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'+'" | indicates that a sign should be used for both positive as | | | well as negative numbers. | +-----------+------------------------------------------------------------+ | "'-'" | indicates that a sign should be used only for negative | | | numbers (this is the default behavior). | +-----------+------------------------------------------------------------+ | space | indicates that a leading space should be used on positive | | | numbers, and a minus sign on negative numbers. | +-----------+------------------------------------------------------------+ The "'#'" option is only valid for integers, and only for binary, octal, or hexadecimal output. If present, it specifies that the output will be prefixed by "'0b'", "'0o'", or "'0x'", respectively. The "','" option signals the use of a comma for a thousands separator. For a locale aware separator, use the "'n'" integer presentation type instead. Changed in version 2.7: Added the "','" option (see also **PEP 378**). *width* is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the *width* field by a zero ("'0'") character enables sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of "'0'" with an *alignment* type of "'='". The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with "'f'" and "'F'", or before and after the decimal point for a floating point value formatted with "'g'" or "'G'". For non- number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The *precision* is not allowed for integer values. Finally, the *type* determines how the data should be presented. The available string presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'s'" | String format. This is the default type for strings and | | | may be omitted. | +-----------+------------------------------------------------------------+ | None | The same as "'s'". | +-----------+------------------------------------------------------------+ The available integer presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'b'" | Binary format. Outputs the number in base 2. | +-----------+------------------------------------------------------------+ | "'c'" | Character. Converts the integer to the corresponding | | | unicode character before printing. | +-----------+------------------------------------------------------------+ | "'d'" | Decimal Integer. Outputs the number in base 10. | +-----------+------------------------------------------------------------+ | "'o'" | Octal format. Outputs the number in base 8. | +-----------+------------------------------------------------------------+ | "'x'" | Hex format. Outputs the number in base 16, using lower- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'X'" | Hex format. Outputs the number in base 16, using upper- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'d'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | None | The same as "'d'". | +-----------+------------------------------------------------------------+ In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except "'n'" and "None"). When doing so, "float()" is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'e'" | Exponent notation. Prints the number in scientific | | | notation using the letter 'e' to indicate the exponent. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'E'" | Exponent notation. Same as "'e'" except it uses an upper | | | case 'E' as the separator character. | +-----------+------------------------------------------------------------+ | "'f'" | Fixed point. Displays the number as a fixed-point number. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'F'" | Fixed point. Same as "'f'". | +-----------+------------------------------------------------------------+ | "'g'" | General format. For a given precision "p >= 1", this | | | rounds the number to "p" significant digits and then | | | formats the result in either fixed-point format or in | | | scientific notation, depending on its magnitude. The | | | precise rules are as follows: suppose that the result | | | formatted with presentation type "'e'" and precision "p-1" | | | would have exponent "exp". Then if "-4 <= exp < p", the | | | number is formatted with presentation type "'f'" and | | | precision "p-1-exp". Otherwise, the number is formatted | | | with presentation type "'e'" and precision "p-1". In both | | | cases insignificant trailing zeros are removed from the | | | significand, and the decimal point is also removed if | | | there are no remaining digits following it. Positive and | | | negative infinity, positive and negative zero, and nans, | | | are formatted as "inf", "-inf", "0", "-0" and "nan" | | | respectively, regardless of the precision. A precision of | | | "0" is treated as equivalent to a precision of "1". The | | | default precision is "6". | +-----------+------------------------------------------------------------+ | "'G'" | General format. Same as "'g'" except switches to "'E'" if | | | the number gets too large. The representations of infinity | | | and NaN are uppercased, too. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'g'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | "'%'" | Percentage. Multiplies the number by 100 and displays in | | | fixed ("'f'") format, followed by a percent sign. | +-----------+------------------------------------------------------------+ | None | The same as "'g'". | +-----------+------------------------------------------------------------+ Format examples =============== This section contains examples of the "str.format()" syntax and comparison with the old "%"-formatting. In most of the cases the syntax is similar to the old "%"-formatting, with the addition of the "{}" and with ":" used instead of "%". For example, "'%03.2f'" can be translated to "'{:03.2f}'". The new format syntax also supports new and different options, shown in the follow examples. Accessing arguments by position: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 'abracadabra' Accessing arguments by name: >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 'Coordinates: 37.24N, -115.81W' >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 'Coordinates: 37.24N, -115.81W' Accessing arguments' attributes: >>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point(object): ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)' Accessing arguments' items: >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' Replacing "%s" and "%r": >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') "repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign: >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases: >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma as a thousands separator: >>> '{:,}'.format(1234567890) '1,234,567,890' Expressing a percentage: >>> points = 19.5 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 88.64%' Using type-specific formatting: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' Nesting arguments and more complex examples: >>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ... 'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right' >>> >>> octets = [192, 168, 0, 1] >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 'C0A80001' >>> int(_, 16) 3232235521 >>> >>> width = 5 >>> for num in range(5,12): ... for base in 'dXob': ... print '{0:{width}{base}}'.format(num, base=base, width=width), ... print ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 t formatstringssz Function definitions ******************** A function definition defines a user-defined function object (see section The standard type hierarchy): decorated ::= decorators (classdef | funcdef) decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE funcdef ::= "def" funcname "(" [parameter_list] ")" ":" suite dotted_name ::= identifier ("." identifier)* parameter_list ::= (defparameter ",")* ( "*" identifier ["," "**" identifier] | "**" identifier | defparameter [","] ) defparameter ::= parameter ["=" expression] sublist ::= parameter ("," parameter)* [","] parameter ::= identifier | "(" sublist ")" funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [3] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code: @f1(arg) @f2 def func(): pass is equivalent to: def func(): pass func = f1(arg)(f2(func)) When one or more top-level *parameters* have the form *parameter* "=" *expression*, the function is said to have "default parameter values." For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form ""*identifier"" is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ""**identifier"" is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a ""def"" statement can be passed around or assigned to another name just like a function defined by a lambda expression. The ""def"" form is actually more powerful since it allows the execution of multiple statements. **Programmer's note:** Functions are first-class objects. A ""def"" form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. tfunctions The "global" statement ********************** global_stmt ::= "global" identifier ("," identifier)* The "global" statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without "global", although free variables may refer to globals without being declared global. Names listed in a "global" statement must not be used in the same code block textually preceding that "global" statement. Names listed in a "global" statement must not be defined as formal parameters or in a "for" loop control target, "class" definition, function definition, or "import" statement. **CPython implementation detail:** The current implementation does not enforce the latter two restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program. **Programmer's note:** "global" is a directive to the parser. It applies only to code parsed at the same time as the "global" statement. In particular, a "global" statement contained in an "exec" statement does not affect the code block *containing* the "exec" statement, and code contained in an "exec" statement is unaffected by "global" statements in the code containing the "exec" statement. The same applies to the "eval()", "execfile()" and "compile()" functions. tglobals Reserved classes of identifiers ******************************* Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "__builtin__" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between "private" attributes of base and derived classes. See section Identifiers (Names). s id-classess Identifiers and keywords ************************ Identifiers (also referred to as *names*) are described by the following lexical definitions: identifier ::= (letter|"_") (letter | digit | "_")* letter ::= lowercase | uppercase lowercase ::= "a"..."z" uppercase ::= "A"..."Z" digit ::= "0"..."9" Identifiers are unlimited in length. Case is significant. Keywords ======== The following identifiers are used as reserved words, or *keywords* of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here: and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try Changed in version 2.4: "None" became a constant and is now recognized by the compiler as a name for the built-in object "None". Although it is not a keyword, you cannot assign a different object to it. Changed in version 2.5: Using "as" and "with" as identifiers triggers a warning. To use them as keywords, enable the "with_statement" future feature . Changed in version 2.6: "as" and "with" are full keywords. Reserved classes of identifiers =============================== Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "__builtin__" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between "private" attributes of base and derived classes. See section Identifiers (Names). t identifierstifs% Imaginary literals ****************** Imaginary literals are described by the following lexical definitions: imagnumber ::= (floatnumber | intpart) ("j" | "J") An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., "(3+4j)". Some examples of imaginary literals: 3.14j 10.j 10j .001j 1e100j 3.14e-10j t imaginarysK. The "import" statement ********************** import_stmt ::= "import" module ["as" name] ( "," module ["as" name] )* | "from" relative_module "import" identifier ["as" name] ( "," identifier ["as" name] )* | "from" relative_module "import" "(" identifier ["as" name] ( "," identifier ["as" name] )* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ name ::= identifier Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the "import" statement occurs). The statement comes in two forms differing on whether it uses the "from" keyword. The first form (without "from") repeats these steps for each identifier in the list. The form with "from" performs step (1) once, and then performs step (2) repeatedly. To understand how step (1) occurs, one must first understand how Python handles hierarchical naming of modules. To help organize modules and provide a hierarchy in naming, Python has a concept of packages. A package can contain other packages and modules while modules cannot contain other modules or packages. From a file system perspective, packages are directories and modules are files. Once the name of the module is known (unless otherwise specified, the term "module" will refer to both packages and modules), searching for the module or package can begin. The first place checked is "sys.modules", the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import. If the module is not found in the cache, then "sys.meta_path" is searched (the specification for "sys.meta_path" can be found in **PEP 302**). The object is a list of *finder* objects which are queried in order as to whether they know how to load the module by calling their "find_module()" method with the name of the module. If the module happens to be contained within a package (as denoted by the existence of a dot in the name), then a second argument to "find_module()" is given as the value of the "__path__" attribute from the parent package (everything up to the last dot in the name of the module being imported). If a finder can find the module it returns a *loader* (discussed later) or returns "None". If none of the finders on "sys.meta_path" are able to find the module then some implicitly defined finders are queried. Implementations of Python vary in what implicit meta path finders are defined. The one they all do define, though, is one that handles "sys.path_hooks", "sys.path_importer_cache", and "sys.path". The implicit finder searches for the requested module in the "paths" specified in one of two places ("paths" do not have to be file system paths). If the module being imported is supposed to be contained within a package then the second argument passed to "find_module()", "__path__" on the parent package, is used as the source of paths. If the module is not contained in a package then "sys.path" is used as the source of paths. Once the source of paths is chosen it is iterated over to find a finder that can handle that path. The dict at "sys.path_importer_cache" caches finders for paths and is checked for a finder. If the path does not have a finder cached then "sys.path_hooks" is searched by calling each object in the list with a single argument of the path, returning a finder or raises "ImportError". If a finder is returned then it is cached in "sys.path_importer_cache" and then used for that path entry. If no finder can be found but the path exists then a value of "None" is stored in "sys.path_importer_cache" to signify that an implicit, file- based finder that handles modules stored as individual files should be used for that path. If the path does not exist then a finder which always returns "None" is placed in the cache for the path. If no finder can find the module then "ImportError" is raised. Otherwise some finder returned a loader whose "load_module()" method is called with the name of the module to load (see **PEP 302** for the original definition of loaders). A loader has several responsibilities to perform on a module it loads. First, if the module already exists in "sys.modules" (a possibility if the loader is called outside of the import machinery) then it is to use that module for initialization and not a new module. But if the module does not exist in "sys.modules" then it is to be added to that dict before initialization begins. If an error occurs during loading of the module and it was added to "sys.modules" it is to be removed from the dict. If an error occurs but the module was already in "sys.modules" it is left in the dict. The loader must set several attributes on the module. "__name__" is to be set to the name of the module. "__file__" is to be the "path" to the file unless the module is built-in (and thus listed in "sys.builtin_module_names") in which case the attribute is not set. If what is being imported is a package then "__path__" is to be set to a list of paths to be searched when looking for modules and packages contained within the package being imported. "__package__" is optional but should be set to the name of package that contains the module or package (the empty string is used for module not contained in a package). "__loader__" is also optional but should be set to the loader object that is loading the module. If an error occurs during loading then the loader raises "ImportError" if some other exception is not already being propagated. Otherwise the loader returns the module that was loaded and initialized. When step (1) finishes without raising an exception, step (2) can begin. The first form of "import" statement binds the module name in the local namespace to the module object, and then goes on to import the next identifier, if any. If the module name is followed by "as", the name following "as" is used as the local name for the module. The "from" form does not bind the module name: it goes through the list of identifiers, looks each one of them up in the module found in step (1), and binds the name in the local namespace to the object thus found. As with the first form of "import", an alternate local name can be supplied by specifying ""as" localname". If a name is not found, "ImportError" is raised. If the list of identifiers is replaced by a star ("'*'"), all public names defined in the module are bound in the local namespace of the "import" statement.. The *public names* defined by a module are determined by checking the module's namespace for a variable named "__all__"; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in "__all__" are all considered public and are required to exist. If "__all__" is not defined, the set of public names includes all names found in the module's namespace which do not begin with an underscore character ("'_'"). "__all__" should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The "from" form with "*" may only occur in a module scope. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after "from" you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute "from . import mod" from a module in the "pkg" package then you will end up importing "pkg.mod". If you execute "from ..subpkg2 import mod" from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The specification for relative imports is contained within **PEP 328**. "importlib.import_module()" is provided to support applications that determine which modules need to be loaded dynamically. Future statements ================= A *future statement* is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard. future_statement ::= "from" "__future__" "import" feature ["as" name] ("," feature ["as" name])* | "from" "__future__" "import" "(" feature ["as" name] ("," feature ["as" name])* [","] ")" feature ::= identifier name ::= identifier A future statement must appear near the top of the module. The only lines that can appear before a future statement are: * the module docstring (if any), * comments, * blank lines, and * other future statements. The features recognized by Python 2.6 are "unicode_literals", "print_function", "absolute_import", "division", "generators", "nested_scopes" and "with_statement". "generators", "with_statement", "nested_scopes" are redundant in Python version 2.6 and above because they are always enabled. A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future statement contains a feature not known to it. The direct runtime semantics are the same as for any import statement: there is a standard module "__future__", described later, and it will be imported in the usual way at the time the future statement is executed. The interesting runtime semantics depend on the specific feature enabled by the future statement. Note that there is nothing special about the statement: import __future__ [as name] That is not a future statement; it's an ordinary import statement with no special semantics or syntax restrictions. Code compiled by an "exec" statement or calls to the built-in functions "compile()" and "execfile()" that occur in a module "M" containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can, starting with Python 2.2 be controlled by optional arguments to "compile()" --- see the documentation of that function for details. A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session. If an interpreter is started with the "-i" option, is passed a script name to execute, and the script includes a future statement, it will be in effect in the interactive session started after the script is executed. See also: **PEP 236** - Back to the __future__ The original proposal for the __future__ mechanism. timportsO Membership test operations ************************** The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". tinso Integer and long integer literals ********************************* Integer and long integer literals are described by the following lexical definitions: longinteger ::= integer ("l" | "L") integer ::= decimalinteger | octinteger | hexinteger | bininteger decimalinteger ::= nonzerodigit digit* | "0" octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+ hexinteger ::= "0" ("x" | "X") hexdigit+ bininteger ::= "0" ("b" | "B") bindigit+ nonzerodigit ::= "1"..."9" octdigit ::= "0"..."7" bindigit ::= "0" | "1" hexdigit ::= digit | "a"..."f" | "A"..."F" Although both lower case "'l'" and upper case "'L'" are allowed as suffix for long integers, it is strongly recommended to always use "'L'", since the letter "'l'" looks too much like the digit "'1'". Plain integer literals that are above the largest representable plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted as if they were long integers instead. [1] There is no limit for long integer literals apart from what can be stored in available memory. Some examples of plain integer literals (first row) and long integer literals (second and third rows): 7 2147483647 0177 3L 79228162514264337593543950336L 0377L 0x100000000L 79228162514264337593543950336 0xdeadbeef tintegerssx Lambdas ******* lambda_expr ::= "lambda" [parameter_list]: expression old_lambda_expr ::= "lambda" [parameter_list]: old_expression Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression "lambda arguments: expression" yields a function object. The unnamed object behaves like a function object defined with def name(arguments): return expression See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements. tlambdas List displays ************* A list display is a possibly empty series of expressions enclosed in square brackets: list_display ::= "[" [expression_list | list_comprehension] "]" list_comprehension ::= expression list_for list_for ::= "for" target_list "in" old_expression_list [list_iter] old_expression_list ::= old_expression [("," old_expression)+ [","]] old_expression ::= or_test | old_lambda_expr list_iter ::= list_for | list_if list_if ::= "if" old_expression [list_iter] A list display yields a new list object. Its contents are specified by providing either a list of expressions or a list comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a list comprehension is supplied, it consists of a single expression followed by at least one "for" clause and zero or more "for" or "if" clauses. In this case, the elements of the new list are those that would be produced by considering each of the "for" or "if" clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached [1]. tlistss Naming and binding ****************** *Names* refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the *binding* of that name established in the innermost function block containing the use. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the '**-c**' option) is a code block. The file read by the built-in function "execfile()" is a code block. The string argument passed to the built-in function "eval()" and to the "exec" statement is a code block. The expression read and evaluated by the built-in function "input()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block's execution has completed. A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods -- this includes generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block's *environment*. If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. When a name is not found at all, a "NameError" exception is raised. If the name refers to a local variable that has not been bound, a "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, in the second position of an "except" clause header or after "as" in a "with" statement. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). It is illegal to unbind a name that is referenced by an enclosing scope; the compiler will report a "SyntaxError". Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "__builtin__". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module's dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "__builtin__" (note: no 's'); when in any other module, "__builtins__" is an alias for the dictionary of the "__builtin__" module itself. "__builtins__" can be set to a user-created dictionary to create a weak form of restricted execution. **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "__builtin__" (no 's') module and modify its attributes appropriately. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class. Names defined at the class scope are not visible in methods. Interaction with dynamic features ================================= There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. tnamings Numeric literals **************** There are four types of numeric literals: plain integers, long integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator '"-"' and the literal "1". tnumberssy Emulating numeric types *********************** The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()" (described below). Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__div__(self, other) object.__truediv__(self, other) The division operator ("/") is implemented by these methods. The "__truediv__()" method is used when "__future__.division" is in effect, otherwise "__div__()" is used. If only one of these two methods is defined, the object will not support division in the alternate context; "TypeError" will be raised instead. object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rdiv__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "/", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types. [2] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand's type is a subclass of the left operand's type and that subclass provides the reflected method for the operation, this method will be called before the left operand's non-reflected method. This behavior allows subclasses to override their ancestors' operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__idiv__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, to execute the statement "x += y", where *x* is an instance of a class that has an "__iadd__()" method, "x.__iadd__(y)" is called. If *x* is an instance of a class that does not define a "__iadd__()" method, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__long__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()", "long()", and "float()". Should return a value of the appropriate type. object.__oct__(self) object.__hex__(self) Called to implement the built-in functions "oct()" and "hex()". Should return a string value. object.__index__(self) Called to implement "operator.index()". Also called whenever Python needs an integer object (such as in slicing). Must return an integer (int or long). New in version 2.5. object.__coerce__(self, other) Called to implement "mixed-mode" numeric arithmetic. Should either return a 2-tuple containing *self* and *other* converted to a common numeric type, or "None" if conversion is impossible. When the common type would be the type of "other", it is sufficient to return "None", since the interpreter will also ask the other object to attempt a coercion (but sometimes, if the implementation of the other type cannot be changed, it is useful to do the conversion to the other type here). A return value of "NotImplemented" is equivalent to returning "None". s numeric-typessZ Objects, values and types ************************* *Objects* are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann's model of a "stored program computer," code is also represented by objects.) Every object has an identity, a type and a value. An object's *identity* never changes once it has been created; you may think of it as the object's address in memory. The '"is"' operator compares the identity of two objects; the "id()" function returns an integer representing its identity (currently implemented as its address). An object's *type* is also unchangeable. [1] An object's type determines the operations that the object supports (e.g., "does it have a length?") and also defines the possible values for objects of that type. The "type()" function returns an object's type (which is an object itself). The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter's value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object's mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable. Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether --- it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable. **CPython implementation detail:** CPython currently uses a reference- counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the "gc" module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (ex: always close files). Note that the use of the implementation's tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a '"try"..."except"' statement may keep objects alive. Some objects contain references to "external" resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a "close()" method. Programs are strongly recommended to explicitly close such objects. The '"try"..."finally"' statement provides a convenient way to do this. Some objects contain references to other objects; these are called *containers*. Examples of containers are tuples, lists and dictionaries. The references are part of a container's value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after "a = 1; b = 1", "a" and "b" may or may not refer to the same object with the value one, depending on the implementation, but after "c = []; d = []", "c" and "d" are guaranteed to refer to two different, unique, newly created empty lists. (Note that "c = d = []" assigns the same object to both "c" and "d".) tobjectss Operator precedence ******************* The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right --- see section Comparisons --- and exponentiation, which groups from right to left). +-------------------------------------------------+---------------------------------------+ | Operator | Description | +=================================================+=======================================+ | "lambda" | Lambda expression | +-------------------------------------------------+---------------------------------------+ | "if" -- "else" | Conditional expression | +-------------------------------------------------+---------------------------------------+ | "or" | Boolean OR | +-------------------------------------------------+---------------------------------------+ | "and" | Boolean AND | +-------------------------------------------------+---------------------------------------+ | "not" "x" | Boolean NOT | +-------------------------------------------------+---------------------------------------+ | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership | | ">=", "<>", "!=", "==" | tests and identity tests | +-------------------------------------------------+---------------------------------------+ | "|" | Bitwise OR | +-------------------------------------------------+---------------------------------------+ | "^" | Bitwise XOR | +-------------------------------------------------+---------------------------------------+ | "&" | Bitwise AND | +-------------------------------------------------+---------------------------------------+ | "<<", ">>" | Shifts | +-------------------------------------------------+---------------------------------------+ | "+", "-" | Addition and subtraction | +-------------------------------------------------+---------------------------------------+ | "*", "/", "//", "%" | Multiplication, division, remainder | | | [7] | +-------------------------------------------------+---------------------------------------+ | "+x", "-x", "~x" | Positive, negative, bitwise NOT | +-------------------------------------------------+---------------------------------------+ | "**" | Exponentiation [8] | +-------------------------------------------------+---------------------------------------+ | "x[index]", "x[index:index]", | Subscription, slicing, call, | | "x(arguments...)", "x.attribute" | attribute reference | +-------------------------------------------------+---------------------------------------+ | "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list | | value...}", "`expressions...`" | display, dictionary display, string | | | conversion | +-------------------------------------------------+---------------------------------------+ -[ Footnotes ]- [1] In Python 2.3 and later releases, a list comprehension "leaks" the control variables of each "for" it contains into the containing scope. However, this behavior is deprecated, and relying on it will not work in Python 3. [2] While "abs(x%y) < abs(y)" is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double- precision number, in order that "-1e-100 % 1e100" have the same sign as "1e100", the computed result is "-1e-100 + 1e100", which is numerically exactly equal to "1e100". The function "math.fmod()" returns a result whose sign matches the sign of the first argument instead, and so returns "-1e-100" in this case. Which approach is more appropriate depends on the application. [3] If x is very close to an exact integer multiple of y, it's possible for "floor(x/y)" to be one larger than "(x-x%y)/y" due to rounding. In such cases, Python returns the latter result, in order to preserve that "divmod(x,y)[0] * y + x % y" be very close to "x". [4] The Unicode standard distinguishes between *code points* (e.g. U+0041) and *abstract characters* (e.g. "LATIN CAPITAL LETTER A"). While most abstract characters in Unicode are only represented using one code point, there is a number of abstract characters that can in addition be represented using a sequence of more than one code point. For example, the abstract character "LATIN CAPITAL LETTER C WITH CEDILLA" can be represented as a single *precomposed character* at code position U+00C7, or as a sequence of a *base character* at code position U+0043 (LATIN CAPITAL LETTER C), followed by a *combining character* at code position U+0327 (COMBINING CEDILLA). The comparison operators on unicode strings compare at the level of Unicode code points. This may be counter-intuitive to humans. For example, "u"\u00C7" == u"\u0043\u0327"" is "False", even though both strings represent the same abstract character "LATIN CAPITAL LETTER C WITH CEDILLA". To compare strings at the level of abstract characters (that is, in a way intuitive to humans), use "unicodedata.normalize()". [5] Earlier versions of Python used lexicographic comparison of the sorted (key, value) lists, but this was very expensive for the common case of comparing for equality. An even earlier version of Python compared dictionaries by identity only, but this caused surprises because people expected to be able to test a dictionary for emptiness by comparing it to "{}". [6] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the "is" operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [7] The "%" operator is also used for string formatting; the same precedence applies. [8] The power operator "**" binds less tightly than an arithmetic or bitwise unary operator on its right, that is, "2**-1" is "0.5". soperator-summarysx The "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation --- when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) tpasss The power operator ****************** The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is: power ::= primary ["**" u_expr] Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): "-1**2" results in "-1". The power operator has the same semantics as the built-in "pow()" function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, "10**2" returns "100", but "10**-2" returns "0.01". (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised). Raising "0.0" to a negative power results in a "ZeroDivisionError". Raising a negative number to a fractional power results in a "ValueError". tpowers The "print" statement ********************* print_stmt ::= "print" ([expression ("," expression)* [","]] | ">>" expression [("," expression)+ [","]]) "print" evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except "' '", or (3) when the last write operation on standard output was not a "print" statement. (In some cases it may be functional to write an empty string to standard output for this reason.) Note: Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object's behavior, so it is best not to rely on this. A "'\n'" character is written at the end, unless the "print" statement ends with a comma. This is the only action if the statement contains just the keyword "print". Standard output is defined as the file object named "stdout" in the built-in module "sys". If no such object exists, or if it does not have a "write()" method, a "RuntimeError" exception is raised. "print" also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as ""print" chevron." In this form, the first expression after the ">>" must evaluate to a "file-like" object, specifically an object that has a "write()" method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to "None", then "sys.stdout" is used as the file for output. tprints The "raise" statement ********************* raise_stmt ::= "raise" [expression ["," expression ["," expression]]] If no expressions are present, "raise" re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a "TypeError" exception is raised indicating that this is an error (if running under IDLE, a "Queue.Empty" exception is raised instead). Otherwise, "raise" evaluates the expressions to get three objects, using "None" as the value of omitted expressions. The first two objects are used to determine the *type* and *value* of the exception. If the first object is an instance, the type of the exception is the class of the instance, the instance itself is the value, and the second object must be "None". If the first object is a class, it becomes the type of the exception. The second object is used to determine the exception value: If it is an instance of the class, the instance becomes the exception value. If the second object is a tuple, it is used as the argument list for the class constructor; if it is "None", an empty argument list is used, and any other object is treated as a single argument to the constructor. The instance so created by calling the constructor is used as the exception value. If a third object is present and not "None", it must be a traceback object (see section The standard type hierarchy), and it is substituted instead of the current location as the place where the exception occurred. If the third object is present and not a traceback object or "None", a "TypeError" exception is raised. The three-expression form of "raise" is useful to re-raise an exception transparently in an except clause, but "raise" with no expressions should be preferred if the exception to be re-raised was the most recently active exception in the current scope. Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement. traises The "return" statement ********************** return_stmt ::= "return" [expression_list] "return" may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else "None" is substituted. "return" leaves the current function call with the expression list (or "None") as return value. When "return" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the function. In a generator function, the "return" statement is not allowed to include an "expression_list". In that context, a bare "return" indicates that the generator is done and will cause "StopIteration" to be raised. treturns Emulating container types ************************* The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. (For backwards compatibility, the method "__getslice__()" (see below) can also be defined to handle simple, but not extended slices.) It is also recommended that mappings provide the methods "keys()", "values()", "items()", "has_key()", "get()", "clear()", "setdefault()", "iterkeys()", "itervalues()", "iteritems()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python's standard dictionary objects. The "UserDict" module provides a "DictMixin" class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define "__coerce__()" or other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should be equivalent of "has_key()"; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "iterkeys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn't define a "__nonzero__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__nonzero__()" method. object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method "iterkeys()". Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". New in version 2.6. The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don't define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. ssequence-typess Shifting operations ******************* The shifting operations have lower priority than the arithmetic operations: shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument. A right shift by *n* bits is defined as division by "pow(2, n)". A left shift by *n* bits is defined as multiplication with "pow(2, n)". Negative shift counts raise a "ValueError" exception. Note: In the current implementation, the right-hand operand is required to be at most "sys.maxsize". If the right-hand operand is larger than "sys.maxsize" an "OverflowError" exception is raised. tshiftings Slicings ******** A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or "del" statements. The syntax for a slicing: slicing ::= simple_slicing | extended_slicing simple_slicing ::= primary "[" short_slice "]" extended_slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice | ellipsis proper_slice ::= short_slice | long_slice short_slice ::= [lower_bound] ":" [upper_bound] long_slice ::= short_slice ":" [stride] lower_bound ::= expression upper_bound ::= expression stride ::= expression ellipsis ::= "..." There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice nor ellipses). Similarly, when the slice list has exactly one short slice and no trailing comma, the interpretation as a simple slicing takes priority over that as an extended slicing. The semantics for a simple slicing are as follows. The primary must evaluate to a sequence object. The lower and upper bound expressions, if present, must evaluate to plain integers; defaults are zero and the "sys.maxint", respectively. If either bound is negative, the sequence's length is added to it. The slicing now selects all items with index *k* such that "i <= k < j" where *i* and *j* are the specified lower and upper bounds. This may be an empty sequence. It is not an error if *i* or *j* lie outside the range of valid indexes (such items don't exist so they aren't selected). The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, and it is indexed with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in "Ellipsis" object. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose "start", "stop" and "step" attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting "None" for missing expressions. tslicingss Special Attributes ****************** The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the "dir()" built-in function. object.__dict__ A dictionary or other mapping object used to store an object's (writable) attributes. object.__methods__ Deprecated since version 2.2: Use the built-in function "dir()" to get a list of an object's attributes. This attribute is no longer available. object.__members__ Deprecated since version 2.2: Use the built-in function "dir()" to get a list of an object's attributes. This attribute is no longer available. instance.__class__ The class to which a class instance belongs. class.__bases__ The tuple of base classes of a class object. definition.__name__ The name of the class, type, function, method, descriptor, or generator instance. The following attributes are only supported by *new-style class*es. class.__mro__ This attribute is a tuple of classes that are considered when looking for base classes during method resolution. class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in "__mro__". class.__subclasses__() Each new-style class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example: >>> int.__subclasses__() [] -[ Footnotes ]- [1] Additional information on these special methods may be found in the Python Reference Manual (Basic customization). [2] As a consequence, the list "[1, 2]" is considered equal to "[1.0, 2.0]", and similarly for tuples. [3] They must have since the parser can't tell the type of the operands. [4] Cased characters are those with general category property being one of "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase). [5] To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted. [6] The advantage of leaving the newline on is that returning an empty string is then an unambiguous EOF indication. It is also possible (in cases where it might matter, for example, if you want to make an exact copy of a file while scanning its lines) to tell whether the last line of a file ended in a newline or not (yes this happens!). t specialattrssa Special method names ******************** A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python's approach to *operator overloading*, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named "__getitem__()", and "x" is an instance of this class, then "x[i]" is roughly equivalent to "x.__getitem__(i)" for old-style classes and "type(x).__getitem__(x, i)" for new-style classes. Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically "AttributeError" or "TypeError"). When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the "NodeList" interface in the W3C's Document Object Model.) Basic customization =================== object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass's "__new__()" method using "super(currentclass, cls).__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance's "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance's "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class's "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "BaseClass.__init__(self, [args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customise it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a "__del__()" method, the derived class's "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn't directly call "x.__del__()" --- the former decrements the reference count for "x" by one, and the latter is only called when "x"'s reference count reaches zero. Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in "sys.exc_traceback" keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in "sys.last_traceback" keeps the stack frame alive). The first situation can only be remedied by explicitly breaking the cycles; the latter two situations can be resolved by storing "None" in "sys.exc_traceback" or "sys.last_traceback". Circular references which are garbage are detected when the option cycle detector is enabled (it's on by default), but can only be cleaned up if there are no Python-level "__del__()" methods involved. Refer to the documentation for the "gc" module for more information about how "__del__()" methods are handled by the cycle detector, particularly the description of the "garbage" value. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. Also, when "__del__()" is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the "__del__()" method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, "__del__()" methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. See also the "-R" command-line option. object.__repr__(self) Called by the "repr()" built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an "informal" string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by the "str()" built-in function and by the "print" statement to compute the "informal" string representation of an object. This differs from "__repr__()" in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) New in version 2.1. These are the so-called "rich comparison" methods, and are called for comparison operators in preference to "__cmp__()" below. The correspondence between operator symbols and method names is as follows: "xy" call "x.__ne__(y)", "x>y" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. There are no implied relationships among the comparison operators. The truth of "x==y" does not imply that "x!=y" is false. Accordingly, when defining "__eq__()", one should also define "__ne__()" so that the operators will behave as expected. See the paragraph on "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, "__lt__()" and "__gt__()" are each other's reflection, "__le__()" and "__ge__()" are each other's reflection, and "__eq__()" and "__ne__()" are their own reflection. Arguments to rich comparison methods are never coerced. To automatically generate ordering operations from a single root operation, see "functools.total_ordering()". object.__cmp__(self, other) Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if "self < other", zero if "self == other", a positive integer if "self > other". If no "__cmp__()", "__eq__()" or "__ne__()" operation is defined, class instances are compared by object identity ("address"). See also the description of "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by "__cmp__()" has been removed since Python 1.5.) object.__rcmp__(self, other) Changed in version 2.1: No longer supported. object.__hash__(self) Called by built-in function "hash()" and for operations on members of hashed collections including "set", "frozenset", and "dict". "__hash__()" should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example: def __hash__(self): return hash((self.name, self.nick, self.color)) If a class does not define a "__cmp__()" or "__eq__()" method it should not define a "__hash__()" operation either; if it defines "__cmp__()" or "__eq__()" but not "__hash__()", its instances will not be usable in hashed collections. If a class defines mutable objects and implements a "__cmp__()" or "__eq__()" method, it should not implement "__hash__()", since hashable collection implementations require that an object's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket). User-defined classes have "__cmp__()" and "__hash__()" methods by default; with them, all objects compare unequal (except with themselves) and "x.__hash__()" returns a result derived from "id(x)". Classes which inherit a "__hash__()" method from a parent class but change the meaning of "__cmp__()" or "__eq__()" such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting "__hash__ = None" in the class definition. Doing so means that not only will instances of the class raise an appropriate "TypeError" when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking "isinstance(obj, collections.Hashable)" (unlike classes which define their own "__hash__()" to explicitly raise "TypeError"). Changed in version 2.5: "__hash__()" may now also return a long integer object; the 32-bit integer is then derived from the hash of that object. Changed in version 2.6: "__hash__" may now be set to "None" to explicitly flag instances of a class as unhashable. object.__nonzero__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True", or their integer equivalents "0" or "1". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__nonzero__()", all its instances are considered true. object.__unicode__(self) Called to implement "unicode()" built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding. Customizing attribute access ============================ The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for "self"). "name" is the attribute name. This method should return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control in new-style classes. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should not simply execute "self.name = value" --- this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., "self.__dict__[name] = value". For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. More attribute access for new-style classes ------------------------------------------- The following methods only apply to new-style classes. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup for new-style classes. Implementing Descriptors ------------------------ The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner's class dictionary or in the class dictionary for one of its parents). In the examples below, "the attribute" refers to the attribute whose name is the key of the property in the owner class' "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. Invoking Descriptors -------------------- In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass "object()" or "type()"). The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to a new-style object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a new-style class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object's instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ --------- By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances. The default can be overridden by defining *__slots__* in a new-style class definition. The *__slots__* declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because *__dict__* is not created for each instance. __slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. If defined in a new-style class, *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. New in version 2.2. Notes on using *__slots__* * When inheriting from a class without *__slots__*, the *__dict__* attribute of that class will always be accessible, so a *__slots__* definition in the subclass is meaningless. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__dict__'" to the *__slots__* declaration would not enable the assignment of new attributes not specifically listed in the sequence of instance variable names. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__weakref__'" to the *__slots__* declaration would not enable support for weak references. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is limited to the class where it is defined. As a result, subclasses will have a *__dict__* unless they also define *__slots__* (which must only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from "variable-length" built-in types such as "long", "str" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. Changed in version 2.6: Previously, *__class__* assignment raised an error if either new or old class had *__slots__*. Customizing class creation ========================== By default, new-style classes are constructed using "type()". A class definition is read into a separate namespace and the value of class name is bound to the result of "type(name, bases, dict)". When the class definition is read, if *__metaclass__* is defined then the callable assigned to it will be called instead of "type()". This allows classes or functions to be written which monitor or alter the class creation process: * Modifying the class dictionary prior to the class being created. * Returning an instance of another class -- essentially performing the role of a factory function. These steps will have to be performed in the metaclass's "__new__()" method -- "type.__new__()" can then be called from this method to create a class with different properties. This example adds a new element to the class dictionary before creating the class: class metacls(type): def __new__(mcs, name, bases, dict): dict['foo'] = 'metacls was here' return type.__new__(mcs, name, bases, dict) You can of course also override other class methods (or add new methods); for example defining a custom "__call__()" method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. __metaclass__ This variable can be any callable accepting arguments for "name", "bases", and "dict". Upon class creation, the callable is used instead of the built-in "type()". New in version 2.2. The appropriate metaclass is determined by the following precedence rules: * If "dict['__metaclass__']" exists, it is used. * Otherwise, if there is at least one base class, its metaclass is used (this looks for a *__class__* attribute first and if not found, uses its type). * Otherwise, if a global variable named __metaclass__ exists, it is used. * Otherwise, the old-style, classic metaclass (types.ClassType) is used. The potential uses for metaclasses are boundless. Some ideas that have been explored including logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. Customizing instance and subclass checks ======================================== New in version 2.6. The following methods are used to override the default behavior of the "isinstance()" and "issubclass()" built-in functions. In particular, the metaclass "abc.ABCMeta" implements these methods in order to allow the addition of Abstract Base Classes (ABCs) as "virtual base classes" to any class or type (including built-in types), including other ABCs. class.__instancecheck__(self, instance) Return true if *instance* should be considered a (direct or indirect) instance of *class*. If defined, called to implement "isinstance(instance, class)". class.__subclasscheck__(self, subclass) Return true if *subclass* should be considered a (direct or indirect) subclass of *class*. If defined, called to implement "issubclass(subclass, class)". Note that these methods are looked up on the type (metaclass) of a class. They cannot be defined as class methods in the actual class. This is consistent with the lookup of special methods that are called on instances, only in this case the instance is itself a class. See also: **PEP 3119** - Introducing Abstract Base Classes Includes the specification for customizing "isinstance()" and "issubclass()" behavior through "__instancecheck__()" and "__subclasscheck__()", with motivation for this functionality in the context of adding Abstract Base Classes (see the "abc" module) to the language. Emulating callable objects ========================== object.__call__(self[, args...]) Called when the instance is "called" as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". Emulating container types ========================= The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. (For backwards compatibility, the method "__getslice__()" (see below) can also be defined to handle simple, but not extended slices.) It is also recommended that mappings provide the methods "keys()", "values()", "items()", "has_key()", "get()", "clear()", "setdefault()", "iterkeys()", "itervalues()", "iteritems()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python's standard dictionary objects. The "UserDict" module provides a "DictMixin" class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define "__coerce__()" or other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should be equivalent of "has_key()"; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "iterkeys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn't define a "__nonzero__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__nonzero__()" method. object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method "iterkeys()". Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". New in version 2.6. The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don't define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. Additional methods for emulation of sequence types ================================================== The following optional methods can be defined to further emulate sequence objects. Immutable sequences methods should at most only define "__getslice__()"; mutable sequences might define all three methods. object.__getslice__(self, i, j) Deprecated since version 2.0: Support slice objects as parameters to the "__getitem__()" method. (However, built-in types in CPython currently still implement "__getslice__()". Therefore, you have to override it in derived classes when implementing slicing.) Called to implement evaluation of "self[i:j]". The returned object should be of the same type as *self*. Note that missing *i* or *j* in the slice expression are replaced by zero or "sys.maxsize", respectively. If negative indexes are used in the slice, the length of the sequence is added to that index. If the instance does not implement the "__len__()" method, an "AttributeError" is raised. No guarantee is made that indexes adjusted this way are not still negative. Indexes which are greater than the length of the sequence are not modified. If no "__getslice__()" is found, a slice object is created instead, and passed to "__getitem__()" instead. object.__setslice__(self, i, j, sequence) Called to implement assignment to "self[i:j]". Same notes for *i* and *j* as for "__getslice__()". This method is deprecated. If no "__setslice__()" is found, or for extended slicing of the form "self[i:j:k]", a slice object is created, and passed to "__setitem__()", instead of "__setslice__()" being called. object.__delslice__(self, i, j) Called to implement deletion of "self[i:j]". Same notes for *i* and *j* as for "__getslice__()". This method is deprecated. If no "__delslice__()" is found, or for extended slicing of the form "self[i:j:k]", a slice object is created, and passed to "__delitem__()", instead of "__delslice__()" being called. Notice that these methods are only invoked when a single slice with a single colon is used, and the slice method is available. For slice operations involving extended slice notation, or in absence of the slice methods, "__getitem__()", "__setitem__()" or "__delitem__()" is called with a slice object as argument. The following example demonstrate how to make your program or module compatible with earlier versions of Python (assuming that methods "__getitem__()", "__setitem__()" and "__delitem__()" support slice objects as arguments): class MyClass: ... def __getitem__(self, index): ... def __setitem__(self, index, value): ... def __delitem__(self, index): ... if sys.version_info < (2, 0): # They won't be defined if version is at least 2.0 final def __getslice__(self, i, j): return self[max(0, i):max(0, j):] def __setslice__(self, i, j, seq): self[max(0, i):max(0, j):] = seq def __delslice__(self, i, j): del self[max(0, i):max(0, j):] ... Note the calls to "max()"; these are necessary because of the handling of negative indices before the "__*slice__()" methods are called. When negative indexes are used, the "__*item__()" methods receive them as provided, but the "__*slice__()" methods get a "cooked" form of the index values. For each negative index value, the length of the sequence is added to the index before calling the method (which may still result in a negative index); this is the customary handling of negative indexes by the built-in sequence types, and the "__*item__()" methods are expected to do this as well. However, since they should already be doing that, negative indexes cannot be passed in; they must be constrained to the bounds of the sequence before being passed to the "__*item__()" methods. Calling "max(0, i)" conveniently returns the proper value. Emulating numeric types ======================= The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()" (described below). Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__div__(self, other) object.__truediv__(self, other) The division operator ("/") is implemented by these methods. The "__truediv__()" method is used when "__future__.division" is in effect, otherwise "__div__()" is used. If only one of these two methods is defined, the object will not support division in the alternate context; "TypeError" will be raised instead. object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rdiv__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "/", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types. [2] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand's type is a subclass of the left operand's type and that subclass provides the reflected method for the operation, this method will be called before the left operand's non-reflected method. This behavior allows subclasses to override their ancestors' operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__idiv__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, to execute the statement "x += y", where *x* is an instance of a class that has an "__iadd__()" method, "x.__iadd__(y)" is called. If *x* is an instance of a class that does not define a "__iadd__()" method, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__long__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()", "long()", and "float()". Should return a value of the appropriate type. object.__oct__(self) object.__hex__(self) Called to implement the built-in functions "oct()" and "hex()". Should return a string value. object.__index__(self) Called to implement "operator.index()". Also called whenever Python needs an integer object (such as in slicing). Must return an integer (int or long). New in version 2.5. object.__coerce__(self, other) Called to implement "mixed-mode" numeric arithmetic. Should either return a 2-tuple containing *self* and *other* converted to a common numeric type, or "None" if conversion is impossible. When the common type would be the type of "other", it is sufficient to return "None", since the interpreter will also ask the other object to attempt a coercion (but sometimes, if the implementation of the other type cannot be changed, it is useful to do the conversion to the other type here). A return value of "NotImplemented" is equivalent to returning "None". Coercion rules ============== This section used to document the rules for coercion. As the language has evolved, the coercion rules have become hard to document precisely; documenting what one version of one particular implementation does is undesirable. Instead, here are some informal guidelines regarding coercion. In Python 3, coercion will not be supported. * If the left operand of a % operator is a string or Unicode object, no coercion takes place and the string formatting operation is invoked instead. * It is no longer recommended to define a coercion operation. Mixed- mode operations on types that don't define coercion pass the original arguments to the operation. * New-style classes (those derived from "object") never invoke the "__coerce__()" method in response to a binary operator; the only time "__coerce__()" is invoked is when the built-in function "coerce()" is called. * For most intents and purposes, an operator that returns "NotImplemented" is treated the same as one that is not implemented at all. * Below, "__op__()" and "__rop__()" are used to signify the generic method names corresponding to an operator; "__iop__()" is used for the corresponding in-place operator. For example, for the operator '"+"', "__add__()" and "__radd__()" are used for the left and right variant of the binary operator, and "__iadd__()" for the in-place variant. * For objects *x* and *y*, first "x.__op__(y)" is tried. If this is not implemented or returns "NotImplemented", "y.__rop__(x)" is tried. If this is also not implemented or returns "NotImplemented", a "TypeError" exception is raised. But see the following exception: * Exception to the previous item: if the left operand is an instance of a built-in type or a new-style class, and the right operand is an instance of a proper subclass of that type or class and overrides the base's "__rop__()" method, the right operand's "__rop__()" method is tried *before* the left operand's "__op__()" method. This is done so that a subclass can completely override binary operators. Otherwise, the left operand's "__op__()" method would always accept the right operand: when an instance of a given class is expected, an instance of a subclass of that class is always acceptable. * When either operand type defines a coercion, this coercion is called before that type's "__op__()" or "__rop__()" method is called, but no sooner. If the coercion returns an object of a different type for the operand whose coercion is invoked, part of the process is redone using the new object. * When an in-place operator (like '"+="') is used, if the left operand implements "__iop__()", it is invoked without any coercion. When the operation falls back to "__op__()" and/or "__rop__()", the normal coercion rules apply. * In "x + y", if *x* is a sequence that implements sequence concatenation, sequence concatenation is invoked. * In "x * y", if one operand is a sequence that implements sequence repetition, and the other is an integer ("int" or "long"), sequence repetition is invoked. * Rich comparisons (implemented by methods "__eq__()" and so on) never use coercion. Three-way comparison (implemented by "__cmp__()") does use coercion under the same conditions as other binary operations use it. * In the current implementation, the built-in numeric types "int", "long", "float", and "complex" do not use coercion. All these types implement a "__coerce__()" method, for use by the built-in "coerce()" function. Changed in version 2.7: The complex type no longer makes implicit calls to the "__coerce__()" method for mixed-type binary arithmetic operations. With Statement Context Managers =============================== New in version 2.5. A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method's return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller's responsibility. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. Special method lookup for old-style classes =========================================== For old-style classes, special methods are always looked up in exactly the same way as any other method or attribute. This is the case regardless of whether the method is being looked up explicitly as in "x.__getitem__(i)" or implicitly as in "x[i]". This behaviour means that special methods may exhibit different behaviour for different instances of a single old-style class if the appropriate special attributes are set differently: >>> class C: ... pass ... >>> c1 = C() >>> c2 = C() >>> c1.__len__ = lambda: 5 >>> c2.__len__ = lambda: 9 >>> len(c1) 5 >>> len(c2) 9 Special method lookup for new-style classes =========================================== For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object's type, not in the object's instance dictionary. That behaviour is the reason why the following code raises an exception (unlike the equivalent example with old-style classes): >>> class C(object): ... pass ... >>> c = C() >>> c.__len__ = lambda: 5 >>> len(c) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such as "__hash__()" and "__repr__()" that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself: >>> 1 .__hash__() == hash(1) True >>> int.__hash__() == hash(int) Traceback (most recent call last): File "", line 1, in TypeError: descriptor '__hash__' of 'int' object needs an argument Incorrectly attempting to invoke an unbound method of a class in this way is sometimes referred to as 'metaclass confusion', and is avoided by bypassing the instance when looking up special methods: >>> type(1).__hash__(1) == hash(1) True >>> type(int).__hash__(int) == hash(int) True In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the "__getattribute__()" method even of the object's metaclass: >>> class Meta(type): ... def __getattribute__(*args): ... print "Metaclass getattribute invoked" ... return type.__getattribute__(*args) ... >>> class C(object): ... __metaclass__ = Meta ... def __len__(self): ... return 10 ... def __getattribute__(*args): ... print "Class getattribute invoked" ... return object.__getattribute__(*args) ... >>> c = C() >>> c.__len__() # Explicit lookup via instance Class getattribute invoked 10 >>> type(c).__len__(c) # Explicit lookup via type Metaclass getattribute invoked 10 >>> len(c) # Implicit lookup 10 Bypassing the "__getattribute__()" machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class object itself in order to be consistently invoked by the interpreter). -[ Footnotes ]- [1] It *is* possible in some cases to change an object's type, under certain controlled conditions. It generally isn't a good idea though, since it can lead to some very strange behaviour if it is handled incorrectly. [2] For operands of the same type, it is assumed that if the non- reflected method (such as "__add__()") fails the operation is not supported, which is why the reflected method is not called. t specialnamessK String Methods ************** Below are listed the string methods which both 8-bit strings and Unicode objects support. Some of them are also available on "bytearray" objects. In addition, Python's strings support the sequence type methods described in the Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange section. To output formatted strings use template strings or the "%" operator described in the String Formatting Operations section. Also, see the "re" module for string functions based on regular expressions. str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. For 8-bit strings, this method is locale-dependent. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). Changed in version 2.4: Support for the *fillchar* argument. str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.decode([encoding[, errors]]) Decodes the string using the codec registered for *encoding*. *encoding* defaults to the default string encoding. *errors* may be given to set a different error handling scheme. The default is "'strict'", meaning that encoding errors raise "UnicodeError". Other possible values are "'ignore'", "'replace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. New in version 2.2. Changed in version 2.3: Support for other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.encode([encoding[, errors]]) Return an encoded version of the string. Default encoding is the current default string encoding. *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings. New in version 2.0. Changed in version 2.3: Support for "'xmlcharrefreplace'" and "'backslashreplace'" and other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. Changed in version 2.5: Accept tuples as *suffix*. str.expandtabs([tabsize]) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. This method of string formatting is the new standard in Python 3, and should be preferred to the "%" formatting described in String Formatting Operations in new code. New in version 2.6. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. For 8-bit strings, this method is locale-dependent. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. For 8-bit strings, this method is locale-dependent. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' Changed in version 2.2.2: Support for the *chars* argument. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. New in version 2.5. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. New in version 2.5. str.rsplit([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. New in version 2.4. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' Changed in version 2.2.2: Support for the *chars* argument. str.split([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example, "' 1 2 3 '.split()" returns "['1', '2', '3']", and "' 1 2 3 '.split(None, 1)" returns "['1', '2 3 ']". str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. This method uses the *universal newlines* approach to splitting lines. Line breaks are not included in the resulting list unless *keepends* is given and true. Python recognizes ""\r"", ""\n"", and ""\r\n"" as line boundaries for 8-bit strings. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] unicode.splitlines([keepends]) Return a list of the lines in the string, like "str.splitlines()". However, the Unicode method splits on the following line boundaries, which are a superset of the *universal newlines* recognized for 8-bit strings. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 2.7: "\v" and "\f" added to list of line boundaries. str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. Changed in version 2.5: Accept tuples as *prefix*. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' Changed in version 2.2.2: Support for the *chars* argument. str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. For 8-bit strings, this method is locale-dependent. str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." For 8-bit strings, this method is locale-dependent. str.translate(table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument *deletechars* are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. You can use the "maketrans()" helper function in the "string" module to create a translation table. For string objects, set the *table* argument to "None" for translations that only delete characters: >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' New in version 2.6: Support for a "None" *table* argument. For Unicode objects, the "translate()" method does not accept the optional *deletechars* argument. Instead, it returns a copy of the *s* where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or "None". Unmapped characters are left untouched. Characters mapped to "None" are deleted. Note, a more flexible approach is to create a custom character mapping codec using the "codecs" module (see "encodings.cp1251" for an example). str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "str.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase). For 8-bit strings, this method is locale-dependent. str.zfill(width) Return the numeric string left filled with zeros in a string of length *width*. A sign prefix is handled correctly. The original string is returned if *width* is less than or equal to "len(s)". New in version 2.2.2. The following methods are present only on unicode objects: unicode.isnumeric() Return "True" if there are only numeric characters in S, "False" otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. unicode.isdecimal() Return "True" if there are only decimal characters in S, "False" otherwise. Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. sstring-methodssF String literals *************** String literals are described by the following lexical definitions: stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR" | "b" | "B" | "br" | "Br" | "bR" | "BR" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | escapeseq longstringitem ::= longstringchar | escapeseq shortstringchar ::= longstringchar ::= escapeseq ::= "\" One syntactic restriction not indicated by these productions is that whitespace is not allowed between the "stringprefix" and the rest of the string literal. The source character set is defined by the encoding declaration; it is ASCII if no encoding declaration is given in the source file; see section Encoding declarations. In plain English: String literals can be enclosed in matching single quotes ("'") or double quotes ("""). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as *triple-quoted strings*). The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter "'r'" or "'R'"; such strings are called *raw strings* and use different rules for interpreting backslash escape sequences. A prefix of "'u'" or "'U'" makes the string a Unicode string. Unicode strings use the Unicode character set as defined by the Unicode Consortium and ISO 10646. Some additional escape sequences, described below, are available in Unicode strings. A prefix of "'b'" or "'B'" is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A "'u'" or "'b'" prefix may be followed by an "'r'" prefix. In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A "quote" is the character used to open the string, i.e. either "'" or """.) Unless an "'r'" or "'R'" prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\newline" | Ignored | | +-------------------+-----------------------------------+---------+ | "\\" | Backslash ("\") | | +-------------------+-----------------------------------+---------+ | "\'" | Single quote ("'") | | +-------------------+-----------------------------------+---------+ | "\"" | Double quote (""") | | +-------------------+-----------------------------------+---------+ | "\a" | ASCII Bell (BEL) | | +-------------------+-----------------------------------+---------+ | "\b" | ASCII Backspace (BS) | | +-------------------+-----------------------------------+---------+ | "\f" | ASCII Formfeed (FF) | | +-------------------+-----------------------------------+---------+ | "\n" | ASCII Linefeed (LF) | | +-------------------+-----------------------------------+---------+ | "\N{name}" | Character named *name* in the | | | | Unicode database (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\r" | ASCII Carriage Return (CR) | | +-------------------+-----------------------------------+---------+ | "\t" | ASCII Horizontal Tab (TAB) | | +-------------------+-----------------------------------+---------+ | "\uxxxx" | Character with 16-bit hex value | (1) | | | *xxxx* (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\Uxxxxxxxx" | Character with 32-bit hex value | (2) | | | *xxxxxxxx* (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\v" | ASCII Vertical Tab (VT) | | +-------------------+-----------------------------------+---------+ | "\ooo" | Character with octal value *ooo* | (3,5) | +-------------------+-----------------------------------+---------+ | "\xhh" | Character with hex value *hh* | (4,5) | +-------------------+-----------------------------------+---------+ Notes: 1. Individual code units which form parts of a surrogate pair can be encoded using this escape sequence. 2. Any Unicode character can be encoded this way, but characters outside the Basic Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is compiled to use 16-bit code units (the default). 3. As in Standard C, up to three octal digits are accepted. 4. Unlike in Standard C, exactly two hex digits are required. 5. In a string literal, hexadecimal and octal escapes denote the byte with the given value; it is not necessary that the byte encodes a character in the source character set. In a Unicode literal, these escapes denote a Unicode character with the given value. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., *the backslash is left in the string*. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences marked as "(Unicode only)" in the table above fall into the category of unrecognized escapes for non-Unicode string literals. When an "'r'" or "'R'" prefix is present, a character following a backslash is included in the string without change, and *all backslashes are left in the string*. For example, the string literal "r"\n"" consists of two characters: a backslash and a lowercase "'n'". String quotes can be escaped with a backslash, but the backslash remains in the string; for example, "r"\""" is a valid string literal consisting of two characters: a backslash and a double quote; "r"\"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw string cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, *not* as a line continuation. When an "'r'" or "'R'" prefix is used in conjunction with a "'u'" or "'U'" prefix, then the "\uXXXX" and "\UXXXXXXXX" escape sequences are processed while *all other backslashes are left in the string*. For example, the string literal "ur"\u0062\n"" consists of three Unicode characters: 'LATIN SMALL LETTER B', 'REVERSE SOLIDUS', and 'LATIN SMALL LETTER N'. Backslashes can be escaped with a preceding backslash; however, both remain in the string. As a result, "\uXXXX" escape sequences are only recognized when there are an odd number of backslashes. tstringss Subscriptions ************* A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object of a sequence or mapping type. If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression (list) must evaluate to a plain integer. If this value is negative, the length of the sequence is added to it (so that, e.g., "x[-1]" selects the last item of "x".) The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). A string's items are characters. A character is not a separate data type but a string of exactly one character. t subscriptionss Truth Value Testing ******************* Any object can be tested for truth value, for use in an "if" or "while" condition or as operand of the Boolean operations below. The following values are considered false: * "None" * "False" * zero of any numeric type, for example, "0", "0L", "0.0", "0j". * any empty sequence, for example, "''", "()", "[]". * any empty mapping, for example, "{}". * instances of user-defined classes, if the class defines a "__nonzero__()" or "__len__()" method, when that method returns the integer zero or "bool" value "False". [1] All other values are considered true --- so objects of many types are always true. Operations and built-in functions that have a Boolean result always return "0" or "False" for false and "1" or "True" for true, unless otherwise stated. (Important exception: the Boolean operations "or" and "and" always return one of their operands.) ttruths The "try" statement ******************* The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression [("as" | ",") identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite Changed in version 2.5: In previous versions of Python, "try"..."except"..."finally" did not work. "try"..."except" had to be nested in "try"..."finally". The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) Before an except clause's suite is executed, details about the exception are assigned to three variables in the "sys" module: "sys.exc_type" receives the object identifying the exception; "sys.exc_value" receives the exception's parameter; "sys.exc_traceback" receives a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. These details are also available through the "sys.exc_info()" function, which returns a tuple "(exc_type, exc_value, exc_traceback)". Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if and when control flows off the end of the "try" clause. [2] Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a 'cleanup' handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception, it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception or executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"..."finally" statement, the "finally" clause is also executed 'on the way out.' A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation --- this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. ttrys The standard type hierarchy *************************** Below is a list of the types that are built into Python. Extension modules (written in C, Java, or other languages, depending on the implementation) can define additional types. Future versions of Python may add types to the type hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.). Some of the type descriptions below contain a paragraph listing 'special attributes.' These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future. None This type has a single value. There is a single object with this value. This object is accessed through the built-in name "None". It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don't explicitly return anything. Its truth value is false. NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name "NotImplemented". Numeric methods and rich comparison methods may return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. Ellipsis This type has a single value. There is a single object with this value. This object is accessed through the built-in name "Ellipsis". It is used to indicate the presence of the "..." syntax in a slice. Its truth value is true. "numbers.Number" These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers. Python distinguishes between integers, floating point numbers, and complex numbers: "numbers.Integral" These represent elements from the mathematical set of integers (positive and negative). There are three types of integers: Plain integers These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.) When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception "OverflowError" is raised instead). For the purpose of shift and mask operations, integers are assumed to have a binary, 2's complement notation using 32 or more bits, and hiding no bits from the user (i.e., all 4294967296 different bit patterns correspond to different values). Long integers These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2's complement which gives the illusion of an infinite string of sign bits extending to the left. Booleans These represent the truth values False and True. The two objects representing the values "False" and "True" are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ""False"" or ""True"" are returned, respectively. The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers and the least surprises when switching between the plain and long integer domains. Any operation, if it yields a result in the plain integer domain, will yield the same result in the long integer domain or when using mixed operands. The switch between domains is transparent to the programmer. "numbers.Real" ("float") These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single- precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers. "numbers.Complex" These represent complex numbers as a pair of machine-level double precision floating point numbers. The same caveats apply as for floating point numbers. The real and imaginary parts of a complex number "z" can be retrieved through the read-only attributes "z.real" and "z.imag". Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function "len()" returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* is selected by "a[i]". Sequences also support slicing: "a[i:j]" selects all items with index *k* such that *i* "<=" *k* "<" *j*. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support "extended slicing" with a third "step" parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*. Sequences are distinguished according to their mutability: Immutable sequences An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.) The following types are immutable sequences: Strings The items of a string are characters. There is no separate character type; a character is represented by a string of one item. Characters represent (at least) 8-bit bytes. The built-in functions "chr()" and "ord()" convert between characters and nonnegative integers representing the byte values. Bytes with the values 0--127 usually represent the corresponding ASCII values, but the interpretation of values is up to the program. The string data type is also used to represent arrays of bytes, e.g., to hold data read from a file. (On systems whose native character set is not ASCII, strings may use EBCDIC in their internal representation, provided the functions "chr()" and "ord()" implement a mapping between ASCII and EBCDIC, and string comparison preserves the ASCII order. Or perhaps someone can propose a better rule?) Unicode The items of a Unicode object are Unicode code units. A Unicode code unit is represented by a Unicode object of one item and can hold either a 16-bit or 32-bit value representing a Unicode ordinal (the maximum value for the ordinal is given in "sys.maxunicode", and depends on how Python is configured at compile time). Surrogate pairs may be present in the Unicode object, and will be reported as two separate items. The built-in functions "unichr()" and "ord()" convert between code units and nonnegative integers representing the Unicode ordinals as defined in the Unicode Standard 3.0. Conversion from and to other encodings are possible through the Unicode method "encode()" and the built- in function "unicode()". Tuples The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a 'singleton') can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses. Mutable sequences Mutable sequences can be changed after they are created. The subscription and slicing notations can be used as the target of assignment and "del" (delete) statements. There are currently two intrinsic mutable sequence types: Lists The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.) Byte Arrays A bytearray object is a mutable array. They are created by the built-in "bytearray()" constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable bytes objects. The extension module "array" provides an additional example of a mutable sequence type. Set types These represent unordered, finite sets of unique, immutable objects. As such, they cannot be indexed by any subscript. However, they can be iterated over, and the built-in function "len()" returns the number of items in a set. Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. For set elements, the same immutability rules apply as for dictionary keys. Note that numeric types obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0"), only one of them can be contained in a set. There are currently two intrinsic set types: Sets These represent a mutable set. They are created by the built-in "set()" constructor and can be modified afterwards by several methods, such as "add()". Frozen sets These represent an immutable set. They are created by the built-in "frozenset()" constructor. As a frozenset is immutable and *hashable*, it can be used again as an element of another set, or as a dictionary key. Mappings These represent finite sets of objects indexed by arbitrary index sets. The subscript notation "a[k]" selects the item indexed by "k" from the mapping "a"; this can be used in expressions and as the target of assignments or "del" statements. The built-in function "len()" returns the number of items in a mapping. There is currently a single intrinsic mapping type: Dictionaries These represent finite sets of objects indexed by nearly arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key's hash value to remain constant. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. Dictionaries are mutable; they can be created by the "{...}" notation (see section Dictionary displays). The extension modules "dbm", "gdbm", and "bsddb" provide additional examples of mapping types. Callable types These are the types to which the function call operation (see section Calls) can be applied: User-defined functions A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function's formal parameter list. Special attributes: +-------------------------+---------------------------------+-------------+ | Attribute | Meaning | | +=========================+=================================+=============+ | "__doc__" "func_doc" | The function's documentation | Writable | | | string, or "None" if | | | | unavailable. | | +-------------------------+---------------------------------+-------------+ | "__name__" "func_name" | The function's name | Writable | +-------------------------+---------------------------------+-------------+ | "__module__" | The name of the module the | Writable | | | function was defined in, or | | | | "None" if unavailable. | | +-------------------------+---------------------------------+-------------+ | "__defaults__" | A tuple containing default | Writable | | "func_defaults" | argument values for those | | | | arguments that have defaults, | | | | or "None" if no arguments have | | | | a default value. | | +-------------------------+---------------------------------+-------------+ | "__code__" "func_code" | The code object representing | Writable | | | the compiled function body. | | +-------------------------+---------------------------------+-------------+ | "__globals__" | A reference to the dictionary | Read-only | | "func_globals" | that holds the function's | | | | global variables --- the global | | | | namespace of the module in | | | | which the function was defined. | | +-------------------------+---------------------------------+-------------+ | "__dict__" "func_dict" | The namespace supporting | Writable | | | arbitrary function attributes. | | +-------------------------+---------------------------------+-------------+ | "__closure__" | "None" or a tuple of cells that | Read-only | | "func_closure" | contain bindings for the | | | | function's free variables. | | +-------------------------+---------------------------------+-------------+ Most of the attributes labelled "Writable" check the type of the assigned value. Changed in version 2.4: "func_name" is now writable. Changed in version 2.6: The double-underscore attributes "__closure__", "__code__", "__defaults__", and "__globals__" were introduced as aliases for the corresponding "func_*" attributes for forwards compatibility with Python 3. Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. *Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.* Additional information about a function's definition can be retrieved from its code object; see the description of internal types below. User-defined methods A user-defined method object combines a class, a class instance (or "None") and any callable object (normally a user-defined function). Special read-only attributes: "im_self" is the class instance object, "im_func" is the function object; "im_class" is the class of "im_self" for bound methods or the class that asked for the method for unbound methods; "__doc__" is the method's documentation (same as "im_func.__doc__"); "__name__" is the method name (same as "im_func.__name__"); "__module__" is the name of the module the method was defined in, or "None" if unavailable. Changed in version 2.2: "im_self" used to refer to the class that defined the method. Changed in version 2.6: For Python 3 forward-compatibility, "im_func" is also available as "__func__", and "im_self" as "__self__". Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object. User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is. When a user-defined method object is created by retrieving a user-defined function object from a class, its "im_self" attribute is "None" and the method object is said to be unbound. When one is created by retrieving a user-defined function object from a class via one of its instances, its "im_self" attribute is the instance, and the method object is said to be bound. In either case, the new method's "im_class" attribute is the class from which the retrieval takes place, and its "im_func" attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the "im_func" attribute of the new instance is not the original method object but its "im_func" attribute. When a user-defined method object is created by retrieving a class method object from a class or instance, its "im_self" attribute is the class itself, and its "im_func" attribute is the function object underlying the class method. When an unbound user-defined method object is called, the underlying function ("im_func") is called, with the restriction that the first argument must be an instance of the proper class ("im_class") or of a derived class thereof. When a bound user-defined method object is called, the underlying function ("im_func") is called, inserting the class instance ("im_self") in front of the argument list. For instance, when "C" is a class which contains a definition for a function "f()", and "x" is an instance of "C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)". When a user-defined method object is derived from a class method object, the "class instance" stored in "im_self" will actually be the class itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to calling "f(C,1)" where "f" is the underlying function. Note that the transformation from function object to (unbound or bound) method object happens each time the attribute is retrieved from the class or instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this *only* happens when the function is an attribute of the class. Generator functions A function or method which uses the "yield" statement (see section The yield statement) is called a *generator function*. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator's "next()" method will cause the function to execute until it provides a value using the "yield" statement. When the function executes a "return" statement or falls off the end, a "StopIteration" exception is raised and the iterator will have reached the end of the set of values to be returned. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are "len()" and "math.sin()" ("math" is a standard built-in module). The number and type of the arguments are determined by the C function. Special read- only attributes: "__doc__" is the function's documentation string, or "None" if unavailable; "__name__" is the function's name; "__self__" is set to "None" (but see the next item); "__module__" is the name of the module the function was defined in or "None" if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is "alist.append()", assuming *alist* is a list object. In this case, the special read-only attribute "__self__" is set to the object denoted by *alist*. Class Types Class types, or "new-style classes," are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override "__new__()". The arguments of the call are passed to "__new__()" and, in the typical case, to "__init__()" to initialize the new instance. Classic Classes Class objects are described below. When a class object is called, a new class instance (also described below) is created and returned. This implies a call to the class's "__init__()" method if it has one. Any arguments are passed on to the "__init__()" method. If there is no "__init__()" method, the class must be called without arguments. Class instances Class instances are described below. Class instances are callable only when the class has a "__call__()" method; "x(arguments)" is a shorthand for "x.__call__(arguments)". Modules Modules are imported by the "import" statement (see section The import statement). A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the func_globals attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object does not contain the code object used to initialize the module (since it isn't needed once the initialization is done). Attribute assignment updates the module's namespace dictionary, e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1". Special read-only attribute: "__dict__" is the module's namespace as a dictionary object. **CPython implementation detail:** Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly. Predefined (writable) attributes: "__name__" is the module's name; "__doc__" is the module's documentation string, or "None" if unavailable; "__file__" is the pathname of the file from which the module was loaded, if it was loaded from a file. The "__file__" attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file. Classes Both class types (new-style classes) and class objects (old- style/classic classes) are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., "C.x" is translated to "C.__dict__["x"]" (although for new-style classes in particular there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes. For old-style classes, the search is depth-first, left-to-right in the order of occurrence in the base class list. New-style classes use the more complex C3 method resolution order which behaves correctly even in the presence of 'diamond' inheritance structures where there are multiple inheritance paths leading back to a common ancestor. Additional details on the C3 MRO used by new-style classes can be found in the documentation accompanying the 2.3 release at https://www.python.org/download/releases/2.3/mro/. When a class attribute reference (for class "C", say) would yield a user-defined function object or an unbound user-defined method object whose associated class is either "C" or one of its base classes, it is transformed into an unbound user-defined method object whose "im_class" attribute is "C". When it would yield a class method object, it is transformed into a bound user-defined method object whose "im_self" attribute is "C". When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section Implementing Descriptors for another way in which attributes retrieved from a class may differ from those actually contained in its "__dict__" (note that only new-style classes support descriptors). Class attribute assignments update the class's dictionary, never the dictionary of a base class. A class object can be called (see above) to yield a class instance (see below). Special attributes: "__name__" is the class name; "__module__" is the module name in which the class was defined; "__dict__" is the dictionary containing the class's namespace; "__bases__" is a tuple (possibly empty or a singleton) containing the base classes, in the order of their occurrence in the base class list; "__doc__" is the class's documentation string, or "None" if undefined. Class instances A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance's class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object or an unbound user-defined method object whose associated class is the class (call it "C") of the instance for which the attribute reference was initiated or one of its bases, it is transformed into a bound user-defined method object whose "im_class" attribute is "C" and whose "im_self" attribute is the instance. Static method and class method objects are also transformed, as if they had been retrieved from class "C"; see above under "Classes". See section Implementing Descriptors for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class's "__dict__". If no class attribute is found, and the object's class has a "__getattr__()" method, that is called to satisfy the lookup. Attribute assignments and deletions update the instance's dictionary, never a class's dictionary. If the class has a "__setattr__()" or "__delattr__()" method, this is called instead of updating the instance dictionary directly. Class instances can pretend to be numbers, sequences, or mappings if they have methods with certain special names. See section Special method names. Special attributes: "__dict__" is the attribute dictionary; "__class__" is the instance's class. Files A file object represents an open file. File objects are created by the "open()" built-in function, and also by "os.popen()", "os.fdopen()", and the "makefile()" method of socket objects (and perhaps by other functions or methods provided by extension modules). The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized to file objects corresponding to the interpreter's standard input, output and error streams. See File Objects for complete documentation of file objects. Internal types A few types used internally by the interpreter are exposed to the user. Their definitions may change with future versions of the interpreter, but they are mentioned here for completeness. Code objects Code objects represent *byte-compiled* executable Python code, or *bytecode*. The difference between a code object and a function object is that the function object contains an explicit reference to the function's globals (the module in which it was defined), while a code object contains no context; also the default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time). Unlike function objects, code objects are immutable and contain no references (directly or indirectly) to mutable objects. Special read-only attributes: "co_name" gives the function name; "co_argcount" is the number of positional arguments (including arguments with default values); "co_nlocals" is the number of local variables used by the function (including arguments); "co_varnames" is a tuple containing the names of the local variables (starting with the argument names); "co_cellvars" is a tuple containing the names of local variables that are referenced by nested functions; "co_freevars" is a tuple containing the names of free variables; "co_code" is a string representing the sequence of bytecode instructions; "co_consts" is a tuple containing the literals used by the bytecode; "co_names" is a tuple containing the names used by the bytecode; "co_filename" is the filename from which the code was compiled; "co_firstlineno" is the first line number of the function; "co_lnotab" is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); "co_stacksize" is the required stack size (including local variables); "co_flags" is an integer encoding a number of flags for the interpreter. The following flag bits are defined for "co_flags": bit "0x04" is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit "0x08" is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; bit "0x20" is set if the function is a generator. Future feature declarations ("from __future__ import division") also use bits in "co_flags" to indicate whether a code object was compiled with a particular feature enabled: bit "0x2000" is set if the function was compiled with future division enabled; bits "0x10" and "0x1000" were used in earlier versions of Python. Other bits in "co_flags" are reserved for internal use. If a code object represents a function, the first item in "co_consts" is the documentation string of the function, or "None" if undefined. Frame objects Frame objects represent execution frames. They may occur in traceback objects (see below). Special read-only attributes: "f_back" is to the previous stack frame (towards the caller), or "None" if this is the bottom stack frame; "f_code" is the code object being executed in this frame; "f_locals" is the dictionary used to look up local variables; "f_globals" is used for global variables; "f_builtins" is used for built-in (intrinsic) names; "f_restricted" is a flag indicating whether the function is executing in restricted execution mode; "f_lasti" gives the precise instruction (this is an index into the bytecode string of the code object). Special writable attributes: "f_trace", if not "None", is a function called at the start of each source code line (this is used by the debugger); "f_exc_type", "f_exc_value", "f_exc_traceback" represent the last exception raised in the parent frame provided another exception was ever raised in the current frame (in all other cases they are "None"); "f_lineno" is the current line number of the frame --- writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno. Traceback objects Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as "sys.exc_traceback", and also as the third item of the tuple returned by "sys.exc_info()". The latter is the preferred interface, since it works correctly when the program is using multiple threads. When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as "sys.last_traceback". Special read-only attributes: "tb_next" is the next level in the stack trace (towards the frame where the exception occurred), or "None" if there is no next level; "tb_frame" points to the execution frame of the current level; "tb_lineno" gives the line number where the exception occurred; "tb_lasti" indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a "try" statement with no matching except clause or with a finally clause. Slice objects Slice objects are used to represent slices when *extended slice syntax* is used. This is a slice using two colons, or multiple slices or ellipses separated by commas, e.g., "a[i:j:step]", "a[i:j, k:l]", or "a[..., i:j]". They are also created by the built-in "slice()" function. Special read-only attributes: "start" is the lower bound; "stop" is the upper bound; "step" is the step value; each is "None" if omitted. These attributes can have any type. Slice objects support one method: slice.indices(self, length) This method takes a single integer argument *length* and computes information about the extended slice that the slice object would describe if applied to a sequence of *length* items. It returns a tuple of three integers; respectively these are the *start* and *stop* indices and the *step* or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices. New in version 2.3. Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in "staticmethod()" constructor. Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under "User-defined methods". Class method objects are created by the built-in "classmethod()" constructor. ttypess Functions ********* Function objects are created by function definitions. The only operation on a function object is to call it: "func(argument-list)". There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types. See Function definitions for more information. ttypesfunctionss/ Mapping Types --- "dict" ************************ A *mapping* object maps *hashable* values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the *dictionary*. (For other containers see the built in "list", "set", and "tuple" classes, and the "collections" module.) A dictionary's keys are *almost* arbitrary values. Values that are not *hashable*, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) Dictionaries can be created by placing a comma-separated list of "key: value" pairs within braces, for example: "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}", or by the "dict" constructor. class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an *iterable* object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary. If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument. To illustrate, the following examples all return a dictionary equal to "{"one": 1, "two": 2, "three": 3}": >>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. New in version 2.2. Changed in version 2.3: Support for building a dictionary from keyword arguments added. These are the operations that dictionaries support (and therefore, custom mapping types should support too): len(d) Return the number of items in the dictionary *d*. d[key] Return the item of *d* with key *key*. Raises a "KeyError" if *key* is not in the map. If a subclass of dict defines a method "__missing__()" and *key* is not present, the "d[key]" operation calls that method with the key *key* as argument. The "d[key]" operation then returns or raises whatever is returned or raised by the "__missing__(key)" call. No other operations or methods invoke "__missing__()". If "__missing__()" is not defined, "KeyError" is raised. "__missing__()" must be a method; it cannot be an instance variable: >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 The example above shows part of the implementation of "collections.Counter". A different "__missing__" method is used by "collections.defaultdict". New in version 2.5: Recognition of __missing__ methods of dict subclasses. d[key] = value Set "d[key]" to *value*. del d[key] Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not in the map. key in d Return "True" if *d* has a key *key*, else "False". New in version 2.2. key not in d Equivalent to "not key in d". New in version 2.2. iter(d) Return an iterator over the keys of the dictionary. This is a shortcut for "iterkeys()". clear() Remove all items from the dictionary. copy() Return a shallow copy of the dictionary. fromkeys(seq[, value]) Create a new dictionary with keys from *seq* and values set to *value*. "fromkeys()" is a class method that returns a new dictionary. *value* defaults to "None". New in version 2.3. get(key[, default]) Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to "None", so that this method never raises a "KeyError". has_key(key) Test for the presence of *key* in the dictionary. "has_key()" is deprecated in favor of "key in d". items() Return a copy of the dictionary's list of "(key, value)" pairs. **CPython implementation detail:** Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If "items()", "keys()", "values()", "iteritems()", "iterkeys()", and "itervalues()" are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". The same relationship holds for the "iterkeys()" and "itervalues()" methods: "pairs = zip(d.itervalues(), d.iterkeys())" provides the same value for "pairs". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.iteritems()]". iteritems() Return an iterator over the dictionary's "(key, value)" pairs. See the note for "dict.items()". Using "iteritems()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. iterkeys() Return an iterator over the dictionary's keys. See the note for "dict.items()". Using "iterkeys()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. itervalues() Return an iterator over the dictionary's values. See the note for "dict.items()". Using "itervalues()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. keys() Return a copy of the dictionary's list of keys. See the note for "dict.items()". pop(key[, default]) If *key* is in the dictionary, remove it and return its value, else return *default*. If *default* is not given and *key* is not in the dictionary, a "KeyError" is raised. New in version 2.3. popitem() Remove and return an arbitrary "(key, value)" pair from the dictionary. "popitem()" is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling "popitem()" raises a "KeyError". setdefault(key[, default]) If *key* is in the dictionary, return its value. If not, insert *key* with a value of *default* and return *default*. *default* defaults to "None". update([other]) Update the dictionary with the key/value pairs from *other*, overwriting existing keys. Return "None". "update()" accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: "d.update(red=1, blue=2)". Changed in version 2.4: Allowed the argument to be an iterable of key/value pairs and allowed keyword arguments. values() Return a copy of the dictionary's list of values. See the note for "dict.items()". viewitems() Return a new view of the dictionary's items ("(key, value)" pairs). See below for documentation of view objects. New in version 2.7. viewkeys() Return a new view of the dictionary's keys. See below for documentation of view objects. New in version 2.7. viewvalues() Return a new view of the dictionary's values. See below for documentation of view objects. New in version 2.7. Dictionaries compare equal if and only if they have the same "(key, value)" pairs. Dictionary view objects ======================= The objects returned by "dict.viewkeys()", "dict.viewvalues()" and "dict.viewitems()" are *view objects*. They provide a dynamic view on the dictionary's entries, which means that when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data, and support membership tests: len(dictview) Return the number of entries in the dictionary. iter(dictview) Return an iterator over the keys, values or items (represented as tuples of "(key, value)") in the dictionary. Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.items()]". Iterating views while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. x in dictview Return "True" if *x* is in the underlying dictionary's keys, values or items (in the latter case, *x* should be a "(key, value)" tuple). Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that (key, value) pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) Then these set operations are available ("other" refers either to another view or a set): dictview & other Return the intersection of the dictview and the other object as a new set. dictview | other Return the union of the dictview and the other object as a new set. dictview - other Return the difference between the dictview and the other object (all elements in *dictview* that aren't in *other*) as a new set. dictview ^ other Return the symmetric difference (all elements either in *dictview* or *other*, but not in both) of the dictview and the other object as a new set. An example of dictionary view usage: >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.viewkeys() >>> values = dishes.viewvalues() >>> # iteration >>> n = 0 >>> for val in values: ... n += val >>> print(n) 504 >>> # keys and values are iterated over in the same order >>> list(keys) ['eggs', 'bacon', 'sausage', 'spam'] >>> list(values) [2, 1, 1, 500] >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> del dishes['sausage'] >>> list(keys) ['spam', 'bacon'] >>> # set operations >>> keys & {'eggs', 'bacon', 'salad'} {'bacon'} t typesmappingsz Methods ******* Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as "append()" on lists) and class instance methods. Built-in methods are described with the types that support them. The implementation adds two special read-only attributes to class instance methods: "m.im_self" is the object on which the method operates, and "m.im_func" is the function implementing the method. Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to calling "m.im_func(m.im_self, arg-1, arg-2, ..., arg-n)". Class instance methods are either *bound* or *unbound*, referring to whether the method was accessed through an instance or a class, respectively. When a method is unbound, its "im_self" attribute will be "None" and if called, an explicit "self" object must be passed as the first argument. In this case, "self" must be an instance of the unbound method's class (or a subclass of that class), otherwise a "TypeError" is raised. Like function objects, methods objects support getting arbitrary attributes. However, since method attributes are actually stored on the underlying function object ("meth.im_func"), setting method attributes on either bound or unbound methods is disallowed. Attempting to set an attribute on a method results in an "AttributeError" being raised. In order to set a method attribute, you need to explicitly set it on the underlying function object: >>> class C: ... def method(self): ... pass ... >>> c = C() >>> c.method.whoami = 'my name is method' # can't set on the method Traceback (most recent call last): File "", line 1, in AttributeError: 'instancemethod' object has no attribute 'whoami' >>> c.method.im_func.whoami = 'my name is method' >>> c.method.whoami 'my name is method' See The standard type hierarchy for more information. t typesmethodss Modules ******* The only special operation on a module is attribute access: "m.name", where *m* is a module and *name* accesses a name defined in *m*'s symbol table. Module attributes can be assigned to. (Note that the "import" statement is not, strictly speaking, an operation on a module object; "import foo" does not require a module object named *foo* to exist, rather it requires an (external) *definition* for a module named *foo* somewhere.) A special attribute of every module is "__dict__". This is the dictionary containing the module's symbol table. Modifying this dictionary will actually change the module's symbol table, but direct assignment to the "__dict__" attribute is not possible (you can write "m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can't write "m.__dict__ = {}"). Modifying "__dict__" directly is not recommended. Modules built into the interpreter are written like this: "". If loaded from a file, they are written as "". t typesmodulessy Sequence Types --- "str", "unicode", "list", "tuple", "bytearray", "buffer", "xrange" ************************************************************************************* There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. For other containers see the built in "dict" and "set" classes, and the "collections" module. String literals are written in single or double quotes: "'xyzzy'", ""frobozz"". See String literals for more about string literals. Unicode strings are much like strings, but are specified in the syntax using a preceding "'u'" character: "u'abc'", "u"def"". In addition to the functionality described here, there are also string-specific methods described in the String Methods section. Lists are constructed with square brackets, separating items with commas: "[a, b, c]". Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as "a, b, c" or "()". A single item tuple must have a trailing comma, such as "(d,)". Bytearray objects are created with the built-in function "bytearray()". Buffer objects are not directly supported by Python syntax, but can be created by calling the built-in function "buffer()". They don't support concatenation or repetition. Objects of type xrange are similar to buffers in that there is no specific syntax to create them, but they are created using the "xrange()" function. They don't support slicing, concatenation or repetition, and using "in", "not in", "min()" or "max()" on them is inefficient. Most sequence types support the following operations. The "in" and "not in" operations have the same priorities as the comparison operations. The "+" and "*" operations have the same priority as the corresponding numeric operations. [3] Additional methods are provided for Mutable Sequence Types. This table lists the sequence operations sorted in ascending priority. In the table, *s* and *t* are sequences of the same type; *n*, *i* and *j* are integers: +--------------------+----------------------------------+------------+ | Operation | Result | Notes | +====================+==================================+============+ | "x in s" | "True" if an item of *s* is | (1) | | | equal to *x*, else "False" | | +--------------------+----------------------------------+------------+ | "x not in s" | "False" if an item of *s* is | (1) | | | equal to *x*, else "True" | | +--------------------+----------------------------------+------------+ | "s + t" | the concatenation of *s* and *t* | (6) | +--------------------+----------------------------------+------------+ | "s * n, n * s" | equivalent to adding *s* to | (2) | | | itself *n* times | | +--------------------+----------------------------------+------------+ | "s[i]" | *i*th item of *s*, origin 0 | (3) | +--------------------+----------------------------------+------------+ | "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) | +--------------------+----------------------------------+------------+ | "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) | | | with step *k* | | +--------------------+----------------------------------+------------+ | "len(s)" | length of *s* | | +--------------------+----------------------------------+------------+ | "min(s)" | smallest item of *s* | | +--------------------+----------------------------------+------------+ | "max(s)" | largest item of *s* | | +--------------------+----------------------------------+------------+ | "s.index(x)" | index of the first occurrence of | | | | *x* in *s* | | +--------------------+----------------------------------+------------+ | "s.count(x)" | total number of occurrences of | | | | *x* in *s* | | +--------------------+----------------------------------+------------+ Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.) Notes: 1. When *s* is a string or Unicode string object the "in" and "not in" operations act like a substring test. In Python versions before 2.3, *x* had to be a string of length 1. In Python 2.3 and beyond, *x* may be a string of any length. 2. Values of *n* less than "0" are treated as "0" (which yields an empty sequence of the same type as *s*). Note that items in the sequence *s* are not copied; they are referenced multiple times. This often haunts new Python programmers; consider: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] What has happened is that "[[]]" is a one-element list containing an empty list, so all three elements of "[[]] * 3" are references to this single empty list. Modifying any of the elements of "lists" modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]] Further explanation is available in the FAQ entry How do I create a multidimensional list?. 3. If *i* or *j* is negative, the index is relative to the end of sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But note that "-0" is still "0". 4. The slice of *s* from *i* to *j* is defined as the sequence of items with index *k* such that "i <= k < j". If *i* or *j* is greater than "len(s)", use "len(s)". If *i* is omitted or "None", use "0". If *j* is omitted or "None", use "len(s)". If *i* is greater than or equal to *j*, the slice is empty. 5. The slice of *s* from *i* to *j* with step *k* is defined as the sequence of items with index "x = i + n*k" such that "0 <= n < (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k", "i+3*k" and so on, stopping when *j* is reached (but never including *j*). When *k* is positive, *i* and *j* are reduced to "len(s)" if they are greater. When *k* is negative, *i* and *j* are reduced to "len(s) - 1" if they are greater. If *i* or *j* are omitted or "None", they become "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it is treated like "1". 6. **CPython implementation detail:** If *s* and *t* are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form "s = s + t" or "s += t". When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use the "str.join()" method which assures consistent linear concatenation performance across versions and implementations. Changed in version 2.4: Formerly, string concatenation never occurred in-place. String Methods ============== Below are listed the string methods which both 8-bit strings and Unicode objects support. Some of them are also available on "bytearray" objects. In addition, Python's strings support the sequence type methods described in the Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange section. To output formatted strings use template strings or the "%" operator described in the String Formatting Operations section. Also, see the "re" module for string functions based on regular expressions. str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. For 8-bit strings, this method is locale-dependent. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). Changed in version 2.4: Support for the *fillchar* argument. str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.decode([encoding[, errors]]) Decodes the string using the codec registered for *encoding*. *encoding* defaults to the default string encoding. *errors* may be given to set a different error handling scheme. The default is "'strict'", meaning that encoding errors raise "UnicodeError". Other possible values are "'ignore'", "'replace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. New in version 2.2. Changed in version 2.3: Support for other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.encode([encoding[, errors]]) Return an encoded version of the string. Default encoding is the current default string encoding. *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings. New in version 2.0. Changed in version 2.3: Support for "'xmlcharrefreplace'" and "'backslashreplace'" and other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. Changed in version 2.5: Accept tuples as *suffix*. str.expandtabs([tabsize]) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. This method of string formatting is the new standard in Python 3, and should be preferred to the "%" formatting described in String Formatting Operations in new code. New in version 2.6. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. For 8-bit strings, this method is locale-dependent. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. For 8-bit strings, this method is locale-dependent. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' Changed in version 2.2.2: Support for the *chars* argument. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. New in version 2.5. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. New in version 2.5. str.rsplit([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. New in version 2.4. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' Changed in version 2.2.2: Support for the *chars* argument. str.split([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example, "' 1 2 3 '.split()" returns "['1', '2', '3']", and "' 1 2 3 '.split(None, 1)" returns "['1', '2 3 ']". str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. This method uses the *universal newlines* approach to splitting lines. Line breaks are not included in the resulting list unless *keepends* is given and true. Python recognizes ""\r"", ""\n"", and ""\r\n"" as line boundaries for 8-bit strings. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] unicode.splitlines([keepends]) Return a list of the lines in the string, like "str.splitlines()". However, the Unicode method splits on the following line boundaries, which are a superset of the *universal newlines* recognized for 8-bit strings. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 2.7: "\v" and "\f" added to list of line boundaries. str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. Changed in version 2.5: Accept tuples as *prefix*. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' Changed in version 2.2.2: Support for the *chars* argument. str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. For 8-bit strings, this method is locale-dependent. str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." For 8-bit strings, this method is locale-dependent. str.translate(table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument *deletechars* are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. You can use the "maketrans()" helper function in the "string" module to create a translation table. For string objects, set the *table* argument to "None" for translations that only delete characters: >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' New in version 2.6: Support for a "None" *table* argument. For Unicode objects, the "translate()" method does not accept the optional *deletechars* argument. Instead, it returns a copy of the *s* where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or "None". Unmapped characters are left untouched. Characters mapped to "None" are deleted. Note, a more flexible approach is to create a custom character mapping codec using the "codecs" module (see "encodings.cp1251" for an example). str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "str.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase). For 8-bit strings, this method is locale-dependent. str.zfill(width) Return the numeric string left filled with zeros in a string of length *width*. A sign prefix is handled correctly. The original string is returned if *width* is less than or equal to "len(s)". New in version 2.2.2. The following methods are present only on unicode objects: unicode.isnumeric() Return "True" if there are only numeric characters in S, "False" otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. unicode.isdecimal() Return "True" if there are only decimal characters in S, "False" otherwise. Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. String Formatting Operations ============================ String and Unicode objects have one unique built-in operation: the "%" operator (modulo). This is also known as the string *formatting* or *interpolation* operator. Given "format % values" (where *format* is a string or Unicode object), "%" conversion specifications in *format* are replaced with zero or more elements of *values*. The effect is similar to the using "sprintf()" in the C language. If *format* is a Unicode object, or if any of the objects being converted using the "%s" conversion are Unicode objects, the result will also be a Unicode object. If *format* requires a single argument, *values* may be a single non- tuple object. [5] Otherwise, *values* must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary). A conversion specifier contains two or more characters and has the following components, which must occur in this order: 1. The "'%'" character, which marks the start of the specifier. 2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, "(somename)"). 3. Conversion flags (optional), which affect the result of some conversion types. 4. Minimum field width (optional). If specified as an "'*'" (asterisk), the actual width is read from the next element of the tuple in *values*, and the object to convert comes after the minimum field width and optional precision. 5. Precision (optional), given as a "'.'" (dot) followed by the precision. If specified as "'*'" (an asterisk), the actual width is read from the next element of the tuple in *values*, and the value to convert comes after the precision. 6. Length modifier (optional). 7. Conversion type. When the right argument is a dictionary (or other mapping type), then the formats in the string *must* include a parenthesised mapping key into that dictionary inserted immediately after the "'%'" character. The mapping key selects the value to be formatted from the mapping. For example: >>> print '%(language)s has %(number)03d quote types.' % \ ... {"language": "Python", "number": 2} Python has 002 quote types. In this case no "*" specifiers may occur in a format (since they require a sequential parameter list). The conversion flag characters are: +-----------+-----------------------------------------------------------------------+ | Flag | Meaning | +===========+=======================================================================+ | "'#'" | The value conversion will use the "alternate form" (where defined | | | below). | +-----------+-----------------------------------------------------------------------+ | "'0'" | The conversion will be zero padded for numeric values. | +-----------+-----------------------------------------------------------------------+ | "'-'" | The converted value is left adjusted (overrides the "'0'" conversion | | | if both are given). | +-----------+-----------------------------------------------------------------------+ | "' '" | (a space) A blank should be left before a positive number (or empty | | | string) produced by a signed conversion. | +-----------+-----------------------------------------------------------------------+ | "'+'" | A sign character ("'+'" or "'-'") will precede the conversion | | | (overrides a "space" flag). | +-----------+-----------------------------------------------------------------------+ A length modifier ("h", "l", or "L") may be present, but is ignored as it is not necessary for Python -- so e.g. "%ld" is identical to "%d". The conversion types are: +--------------+-------------------------------------------------------+---------+ | Conversion | Meaning | Notes | +==============+=======================================================+=========+ | "'d'" | Signed integer decimal. | | +--------------+-------------------------------------------------------+---------+ | "'i'" | Signed integer decimal. | | +--------------+-------------------------------------------------------+---------+ | "'o'" | Signed octal value. | (1) | +--------------+-------------------------------------------------------+---------+ | "'u'" | Obsolete type -- it is identical to "'d'". | (7) | +--------------+-------------------------------------------------------+---------+ | "'x'" | Signed hexadecimal (lowercase). | (2) | +--------------+-------------------------------------------------------+---------+ | "'X'" | Signed hexadecimal (uppercase). | (2) | +--------------+-------------------------------------------------------+---------+ | "'e'" | Floating point exponential format (lowercase). | (3) | +--------------+-------------------------------------------------------+---------+ | "'E'" | Floating point exponential format (uppercase). | (3) | +--------------+-------------------------------------------------------+---------+ | "'f'" | Floating point decimal format. | (3) | +--------------+-------------------------------------------------------+---------+ | "'F'" | Floating point decimal format. | (3) | +--------------+-------------------------------------------------------+---------+ | "'g'" | Floating point format. Uses lowercase exponential | (4) | | | format if exponent is less than -4 or not less than | | | | precision, decimal format otherwise. | | +--------------+-------------------------------------------------------+---------+ | "'G'" | Floating point format. Uses uppercase exponential | (4) | | | format if exponent is less than -4 or not less than | | | | precision, decimal format otherwise. | | +--------------+-------------------------------------------------------+---------+ | "'c'" | Single character (accepts integer or single character | | | | string). | | +--------------+-------------------------------------------------------+---------+ | "'r'" | String (converts any Python object using repr()). | (5) | +--------------+-------------------------------------------------------+---------+ | "'s'" | String (converts any Python object using "str()"). | (6) | +--------------+-------------------------------------------------------+---------+ | "'%'" | No argument is converted, results in a "'%'" | | | | character in the result. | | +--------------+-------------------------------------------------------+---------+ Notes: 1. The alternate form causes a leading zero ("'0'") to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero. 2. The alternate form causes a leading "'0x'" or "'0X'" (depending on whether the "'x'" or "'X'" format was used) to be inserted before the first digit. 3. The alternate form causes the result to always contain a decimal point, even if no digits follow it. The precision determines the number of digits after the decimal point and defaults to 6. 4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6. 5. The "%r" conversion was added in Python 2.0. The precision determines the maximal number of characters used. 6. If the object or format provided is a "unicode" string, the resulting string will also be "unicode". The precision determines the maximal number of characters used. 7. See **PEP 237**. Since Python strings have an explicit length, "%s" conversions do not assume that "'\0'" is the end of the string. Changed in version 2.7: "%f" conversions for numbers whose absolute value is over 1e50 are no longer replaced by "%g" conversions. Additional string operations are defined in standard modules "string" and "re". XRange Type =========== The "xrange" type is an immutable sequence which is commonly used for looping. The advantage of the "xrange" type is that an "xrange" object will always take the same amount of memory, no matter the size of the range it represents. There are no consistent performance advantages. XRange objects have very little behavior: they only support indexing, iteration, and the "len()" function. Mutable Sequence Types ====================== List and "bytearray" objects support additional operations that allow in-place modification of the object. Other mutable sequence types (when added to the language) should also support these operations. Strings and tuples are immutable sequence types: such objects cannot be modified once created. The following operations are defined on mutable sequence types (where *x* is an arbitrary object): +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | same as "s[len(s):len(s)] = [x]" | (2) | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | for the most part the same as | (3) | | | "s[len(s):len(s)] = t" | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (11) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.count(x)" | return number of *i*'s for which | | | | "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.index(x[, i[, j]])" | return smallest *k* such that | (4) | | | "s[k] == x" and "i <= k < j" | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | same as "s[i:i] = [x]" | (5) | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | same as "x = s[i]; del s[i]; | (6) | | | return x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | same as "del s[s.index(x)]" | (4) | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (7) | | | place | | +--------------------------------+----------------------------------+-----------------------+ | "s.sort([cmp[, key[, | sort the items of *s* in place | (7)(8)(9)(10) | | reverse]]])" | | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4. 3. *t* can be any iterable object. 4. Raises "ValueError" when *x* is not found in *s*. When a negative index is passed as the second or third parameter to the "index()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, "index()" didn't have arguments for specifying start and stop positions. 5. When a negative index is passed as the first parameter to the "insert()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, all negative indices were truncated to zero. 6. The "pop()" method's optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 7. The "sort()" and "reverse()" methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. 8. The "sort()" method takes optional arguments for controlling the comparisons. *cmp* specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: "cmp=lambda x,y: cmp(x.lower(), y.lower())". The default value is "None". *key* specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower". The default value is "None". *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. In general, the *key* and *reverse* conversion processes are much faster than specifying an equivalent *cmp* function. This is because *cmp* is called multiple times for each list element while *key* and *reverse* touch each element only once. Use "functools.cmp_to_key()" to convert an old-style *cmp* function to a *key* function. Changed in version 2.3: Support for "None" as an equivalent to omitting *cmp* was added. Changed in version 2.4: Support for *key* and *reverse* was added. 9. Starting with Python 2.3, the "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). 10. **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python 2.3 and newer makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. 11. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange. ttypesseqsI Mutable Sequence Types ********************** List and "bytearray" objects support additional operations that allow in-place modification of the object. Other mutable sequence types (when added to the language) should also support these operations. Strings and tuples are immutable sequence types: such objects cannot be modified once created. The following operations are defined on mutable sequence types (where *x* is an arbitrary object): +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | same as "s[len(s):len(s)] = [x]" | (2) | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | for the most part the same as | (3) | | | "s[len(s):len(s)] = t" | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (11) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.count(x)" | return number of *i*'s for which | | | | "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.index(x[, i[, j]])" | return smallest *k* such that | (4) | | | "s[k] == x" and "i <= k < j" | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | same as "s[i:i] = [x]" | (5) | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | same as "x = s[i]; del s[i]; | (6) | | | return x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | same as "del s[s.index(x)]" | (4) | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (7) | | | place | | +--------------------------------+----------------------------------+-----------------------+ | "s.sort([cmp[, key[, | sort the items of *s* in place | (7)(8)(9)(10) | | reverse]]])" | | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4. 3. *t* can be any iterable object. 4. Raises "ValueError" when *x* is not found in *s*. When a negative index is passed as the second or third parameter to the "index()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, "index()" didn't have arguments for specifying start and stop positions. 5. When a negative index is passed as the first parameter to the "insert()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, all negative indices were truncated to zero. 6. The "pop()" method's optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 7. The "sort()" and "reverse()" methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. 8. The "sort()" method takes optional arguments for controlling the comparisons. *cmp* specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: "cmp=lambda x,y: cmp(x.lower(), y.lower())". The default value is "None". *key* specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower". The default value is "None". *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. In general, the *key* and *reverse* conversion processes are much faster than specifying an equivalent *cmp* function. This is because *cmp* is called multiple times for each list element while *key* and *reverse* touch each element only once. Use "functools.cmp_to_key()" to convert an old-style *cmp* function to a *key* function. Changed in version 2.3: Support for "None" as an equivalent to omitting *cmp* was added. Changed in version 2.4: Support for *key* and *reverse* was added. 9. Starting with Python 2.3, the "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). 10. **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python 2.3 and newer makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. 11. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange. stypesseq-mutables Unary arithmetic and bitwise operations *************************************** All unary arithmetic and bitwise operations have the same priority: u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr The unary "-" (minus) operator yields the negation of its numeric argument. The unary "+" (plus) operator yields its numeric argument unchanged. The unary "~" (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of "x" is defined as "-(x+1)". It only applies to integral numbers. In all three cases, if the argument does not have the proper type, a "TypeError" exception is raised. tunarys The "while" statement ********************* The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. twhiles The "with" statement ******************** New in version 2.5. The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"..."except"..."finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one "item" proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager's "__exit__()" is loaded for later use. 3. The context manager's "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager's "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Note: In Python 2.5, the "with" statement is only allowed when the "with_statement" feature has been enabled. It is always enabled in Python 2.6. Changed in version 2.7: Support for multiple context expressions. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. twiths The "yield" statement ********************* yield_stmt ::= yield_expression The "yield" statement is only used when defining a generator function, and is only used in the body of the generator function. Using a "yield" statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator's "next()" method repeatedly until it raises an exception. When a "yield" statement is executed, the state of the generator is frozen and the value of "expression_list" is returned to "next()"'s caller. By "frozen" we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time "next()" is invoked, the function can proceed exactly as if the "yield" statement were just another external call. As of Python version 2.5, the "yield" statement is now allowed in the "try" clause of a "try" ... "finally" construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator's "close()" method will be called, allowing any pending "finally" clauses to execute. For full details of "yield" semantics, refer to the Yield expressions section. Note: In Python 2.2, the "yield" statement was only allowed when the "generators" feature has been enabled. This "__future__" import statement was used to enable the feature: from __future__ import generators See also: **PEP 255** - Simple Generators The proposal for adding generators and the "yield" statement to Python. **PEP 342** - Coroutines via Enhanced Generators The proposal that, among other generator enhancements, proposed allowing "yield" to appear inside a "try" ... "finally" block. tyieldN(ttopics(((s)/usr/lib64/python2.7/pydoc_data/topics.pyts ')9U   2 KMA#%-UJK<,1cU%+&{*;;MmD&" B&\QPKe&1]?$__pycache__/__init__.cpython-312.pycnu[ gjy)Nr,/usr/lib64/python3.12/pydoc_data/__init__.pyrsrPKe&1]?*__pycache__/__init__.cpython-312.opt-1.pycnu[ gjy)Nr,/usr/lib64/python3.12/pydoc_data/__init__.pyrsrPKe&1]?*__pycache__/__init__.cpython-312.opt-2.pycnu[ gjy)Nr,/usr/lib64/python3.12/pydoc_data/__init__.pyrsrPKa2]tkk topics.pyonu[ |fc@s3iOdd6dd6dd6dd6dd 6d d 6d d 6dd6dd6dd6dd6dd6dd6dd6dd6dd6d d!6d"d#6d$d%6d&d'6d(d)6d*d+6d,d-6d.d/6d0d16d2d36d4d56d6d76d8d96d:d;6d<d=6d>d?6d@dA6dBdC6dDdE6dFdG6dHdI6dJdK6dLdM6dNdO6dPdQ6d:dR6dSdT6dUdV6dWdX6dYdZ6d[d\6d]d^6d_d`6dadb6dcdd6dedf6dgdh6didj6dkdl6dmdn6dodp6dqdr6dsdt6dudv6dwdx6dydz6d{d|6d}d~6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6ZdS(st The "assert" statement ********************** Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= "assert" expression ["," expression] The simple form, "assert expression", is equivalent to if __debug__: if not expression: raise AssertionError The extended form, "assert expression1, expression2", is equivalent to if __debug__: if not expression1: raise AssertionError(expression2) These equivalences assume that "__debug__" and "AssertionError" refer to the built-in variables with those names. In the current implementation, the built-in variable "__debug__" is "True" under normal circumstances, "False" when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace. Assignments to "__debug__" are illegal. The value for the built-in variable is determined when the interpreter starts. tasserts Assignment statements ********************* Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects: assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression) target_list ::= target ("," target)* [","] target ::= identifier | "(" target_list ")" | "[" [target_list] "]" | attributeref | subscription | slicing (See section Primaries for the syntax definitions for the last three symbols.) An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right. Assignment is defined recursively depending on the form of the target (list). When a target is part of a mutable object (an attribute reference, subscription or slicing), the mutable object must ultimately perform the assignment and decide about its validity, and may raise an exception if the assignment is unacceptable. The rules observed by various types and the exceptions raised are given with the definition of the object types (see section The standard type hierarchy). Assignment of an object to a target list is recursively defined as follows. * If the target list is a single target: The object is assigned to that target. * If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets. Assignment of an object to a single target is recursively defined as follows. * If the target is an identifier (name): * If the name does not occur in a "global" statement in the current code block: the name is bound to the object in the current local namespace. * Otherwise: the name is bound to the object in the current global namespace. The name is rebound if it was already bound. This may cause the reference count for the object previously bound to the name to reach zero, causing the object to be deallocated and its destructor (if it has one) to be called. * If the target is a target list enclosed in parentheses or in square brackets: The object must be an iterable with the same number of items as there are targets in the target list, and its items are assigned, from left to right, to the corresponding targets. * If the target is an attribute reference: The primary expression in the reference is evaluated. It should yield an object with assignable attributes; if this is not the case, "TypeError" is raised. That object is then asked to assign the assigned object to the given attribute; if it cannot perform the assignment, it raises an exception (usually but not necessarily "AttributeError"). Note: If the object is a class instance and the attribute reference occurs on both sides of the assignment operator, the RHS expression, "a.x" can access either an instance attribute or (if no instance attribute exists) a class attribute. The LHS target "a.x" is always set as an instance attribute, creating it if necessary. Thus, the two occurrences of "a.x" do not necessarily refer to the same attribute: if the RHS expression refers to a class attribute, the LHS creates a new instance attribute as the target of the assignment: class Cls: x = 3 # class variable inst = Cls() inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3 This description does not necessarily apply to descriptor attributes, such as properties created with "property()". * If the target is a subscription: The primary expression in the reference is evaluated. It should yield either a mutable sequence object (such as a list) or a mapping object (such as a dictionary). Next, the subscript expression is evaluated. If the primary is a mutable sequence object (such as a list), the subscript must yield a plain integer. If it is negative, the sequence's length is added to it. The resulting value must be a nonnegative integer less than the sequence's length, and the sequence is asked to assign the assigned object to its item with that index. If the index is out of range, "IndexError" is raised (assignment to a subscripted sequence cannot add new items to a list). If the primary is a mapping object (such as a dictionary), the subscript must have a type compatible with the mapping's key type, and the mapping is then asked to create a key/datum pair which maps the subscript to the assigned object. This can either replace an existing key/value pair with the same key value, or insert a new key/value pair (if no key with the same value existed). * If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence's length. The bounds should evaluate to (small) integers. If either bound is negative, the sequence's length is added to it. The resulting bounds are clipped to lie between zero and the sequence's length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the object allows it. **CPython implementation detail:** In the current implementation, the syntax for targets is taken to be the same as for expressions, and invalid syntax is rejected during the code generation phase, causing less detailed error messages. WARNING: Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are 'safe' (for example "a, b = b, a" swaps two variables), overlaps *within* the collection of assigned-to variables are not safe! For instance, the following program prints "[0, 2]": x = [0, 1] i = 0 i, x[i] = 1, 2 print x Augmented assignment statements =============================== Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions for the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. t assignments Identifiers (Names) ******************* An identifier occurring as an atom is a name. See section 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. **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. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier "__spam" occurring in a class named "Ham" will be transformed to "_Ham__spam". This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done. satom-identifierss Literals ******** Python supports string literals and various numeric literals: literal ::= stringliteral | integer | longinteger | floatnumber | imagnumber Evaluation of a literal yields an object of the given type (string, integer, long integer, floating point number, complex number) with the given value. The value may be approximated in the case of floating point and imaginary (complex) literals. See section Literals for details. 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. s atom-literalssU* Customizing attribute access **************************** The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for "self"). "name" is the attribute name. This method should return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control in new-style classes. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should not simply execute "self.name = value" --- this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., "self.__dict__[name] = value". For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. More attribute access for new-style classes =========================================== The following methods only apply to new-style classes. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup for new-style classes. Implementing Descriptors ======================== The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner's class dictionary or in the class dictionary for one of its parents). In the examples below, "the attribute" refers to the attribute whose name is the key of the property in the owner class' "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. Invoking Descriptors ==================== In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass "object()" or "type()"). The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to a new-style object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a new-style class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object's instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ ========= By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances. The default can be overridden by defining *__slots__* in a new-style class definition. The *__slots__* declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because *__dict__* is not created for each instance. __slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. If defined in a new-style class, *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. New in version 2.2. Notes on using *__slots__* * When inheriting from a class without *__slots__*, the *__dict__* attribute of that class will always be accessible, so a *__slots__* definition in the subclass is meaningless. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__dict__'" to the *__slots__* declaration would not enable the assignment of new attributes not specifically listed in the sequence of instance variable names. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__weakref__'" to the *__slots__* declaration would not enable support for weak references. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is limited to the class where it is defined. As a result, subclasses will have a *__dict__* unless they also define *__slots__* (which must only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from "variable-length" built-in types such as "long", "str" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. Changed in version 2.6: Previously, *__class__* assignment raised an error if either new or old class had *__slots__*. sattribute-accesss_ Attribute references ******************** An attribute reference is a primary followed by a period and a name: attributeref ::= primary "." identifier The primary must evaluate to an object of a type that supports attribute references, e.g., a module, list, or an instance. This object is then asked to produce the attribute whose name is the identifier. If this attribute is not available, the exception "AttributeError" is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects. sattribute-referencess Augmented assignment statements ******************************* Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement: augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression) augtarget ::= identifier | attributeref | subscription | slicing augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" (See section Primaries for the syntax definitions for the last three symbols.) An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once. An augmented assignment expression like "x += 1" can be rewritten as "x = x + 1" to achieve a similar, but not exactly equal effect. In the augmented version, "x" is only evaluated once. Also, when possible, the actual operation is performed *in-place*, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. For targets which are attribute references, the same caveat about class and instance attributes applies as for regular assignments. t augassignsn Binary arithmetic operations **************************** The binary arithmetic operations have the conventional priority levels. Note that some of these operations also apply to certain non- numeric types. Apart from the power operator, there are only two levels, one for multiplicative operators and one for additive operators: m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr The "*" (multiplication) operator yields the product of its arguments. The arguments must either both be numbers, or one argument must be an integer (plain or long) and the other must be a sequence. In the former case, the numbers are converted to a common type and then multiplied together. In the latter case, sequence repetition is performed; a negative repetition factor yields an empty sequence. The "/" (division) and "//" (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the 'floor' function applied to the result. Division by zero raises the "ZeroDivisionError" exception. The "%" (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the "ZeroDivisionError" exception. The arguments may be floating point numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals "4*0.7 + 0.34".) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2]. The integer division and modulo operators are connected by the following identity: "x == (x/y)*y + (x%y)". Integer division and modulo are also connected with the built-in function "divmod()": "divmod(x, y) == (x/y, x%y)". These identities don't hold for floating point numbers; there similar identities hold approximately where "x/y" is replaced by "floor(x/y)" or "floor(x/y) - 1" [3]. In addition to performing the modulo operation on numbers, the "%" operator is also overloaded by string and unicode objects to perform string formatting (also known as interpolation). The syntax for string formatting is described in the Python Library Reference, section String Formatting Operations. Deprecated since version 2.3: The floor division operator, the modulo operator, and the "divmod()" function are no longer defined for complex numbers. Instead, convert to a floating point number using the "abs()" function if appropriate. The "+" (addition) operator yields the sum of its arguments. The arguments must either both be numbers or both sequences of the same type. In the former case, the numbers are converted to a common type and then added together. In the latter case, the sequences are concatenated. The "-" (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. tbinarys Binary bitwise operations ************************* Each of the three bitwise operations has a different priority level: and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr The "&" operator yields the bitwise AND of its arguments, which must be plain or long integers. The arguments are converted to a common type. The "^" operator yields the bitwise XOR (exclusive OR) of its arguments, which must be plain or long integers. The arguments are converted to a common type. The "|" operator yields the bitwise (inclusive) OR of its arguments, which must be plain or long integers. The arguments are converted to a common type. tbitwises~ Code Objects ************ Code objects are used by the implementation to represent "pseudo- compiled" executable Python code such as a function body. They differ from function objects because they don't contain a reference to their global execution environment. Code objects are returned by the built- in "compile()" function and can be extracted from function objects through their "func_code" attribute. See also the "code" module. A code object can be executed or evaluated by passing it (instead of a source string) to the "exec" statement or the built-in "eval()" function. See The standard type hierarchy for more information. sbltin-code-objectssE The Ellipsis Object ******************* This object is used by extended slice notation (see Slicings). It supports no special operations. There is exactly one ellipsis object, named "Ellipsis" (a built-in name). It is written as "Ellipsis". When in a subscript, it can also be written as "...", for example "seq[...]". sbltin-ellipsis-objects+ File Objects ************ File objects are implemented using C's "stdio" package and can be created with the built-in "open()" function. File objects are also returned by some other built-in functions and methods, such as "os.popen()" and "os.fdopen()" and the "makefile()" method of socket objects. Temporary files can be created using the "tempfile" module, and high-level file operations such as copying, moving, and deleting files and directories can be achieved with the "shutil" module. When a file operation fails for an I/O-related reason, the exception "IOError" is raised. This includes situations where the operation is not defined for some reason, like "seek()" on a tty device or writing a file opened for reading. Files have the following methods: file.close() Close the file. A closed file cannot be read or written any more. Any operation which requires that the file be open will raise a "ValueError" after the file has been closed. Calling "close()" more than once is allowed. As of Python 2.5, you can avoid having to call this method explicitly if you use the "with" statement. For example, the following code will automatically close *f* when the "with" block is exited: from __future__ import with_statement # This isn't required in Python 2.6 with open("hello.txt") as f: for line in f: print line, In older versions of Python, you would have needed to do this to get the same effect: f = open("hello.txt") try: for line in f: print line, finally: f.close() Note: Not all "file-like" types in Python support use as a context manager for the "with" statement. If your code is intended to work with any file-like object, you can use the function "contextlib.closing()" instead of using the object directly. file.flush() Flush the internal buffer, like "stdio"'s "fflush()". This may be a no-op on some file-like objects. Note: "flush()" does not necessarily write the file's data to disk. Use "flush()" followed by "os.fsync()" to ensure this behavior. file.fileno() Return the integer "file descriptor" that is used by the underlying implementation to request I/O operations from the operating system. This can be useful for other, lower level interfaces that use file descriptors, such as the "fcntl" module or "os.read()" and friends. Note: File-like objects which do not have a real file descriptor should *not* provide this method! file.isatty() Return "True" if the file is connected to a tty(-like) device, else "False". Note: If a file-like object is not associated with a real file, this method should *not* be implemented. file.next() A file object is its own iterator, for example "iter(f)" returns *f* (unless *f* is closed). When a file is used as an iterator, typically in a "for" loop (for example, "for line in f: print line.strip()"), the "next()" method is called repeatedly. This method returns the next input line, or raises "StopIteration" when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing). In order to make a "for" loop the most efficient way of looping over the lines of a file (a very common operation), the "next()" method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining "next()" with other file methods (like "readline()") does not work right. However, using "seek()" to reposition the file to an absolute position will flush the read-ahead buffer. New in version 2.3. file.read([size]) Read at most *size* bytes from the file (less if the read hits EOF before obtaining *size* bytes). If the *size* argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function "fread()" more than once in an effort to acquire as close to *size* bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no *size* parameter was given. Note: This function is simply a wrapper for the underlying "fread()" C function, and will behave the same in corner cases, such as whether the EOF value is cached. file.readline([size]) Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). [6] If the *size* argument is present and non- negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. When *size* is not 0, an empty string is returned *only* when EOF is encountered immediately. Note: Unlike "stdio"'s "fgets()", the returned string contains null characters ("'\0'") if they occurred in the input. file.readlines([sizehint]) Read until EOF using "readline()" and return a list containing the lines thus read. If the optional *sizehint* argument is present, instead of reading up to EOF, whole lines totalling approximately *sizehint* bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore *sizehint* if it cannot be implemented, or cannot be implemented efficiently. file.xreadlines() This method returns the same thing as "iter(f)". New in version 2.1. Deprecated since version 2.3: Use "for line in file" instead. file.seek(offset[, whence]) Set the file's current position, like "stdio"'s "fseek()". The *whence* argument is optional and defaults to "os.SEEK_SET" or "0" (absolute file positioning); other values are "os.SEEK_CUR" or "1" (seek relative to the current position) and "os.SEEK_END" or "2" (seek relative to the file's end). There is no return value. For example, "f.seek(2, os.SEEK_CUR)" advances the position by two and "f.seek(-3, os.SEEK_END)" sets the position to the third to last. Note that if the file is opened for appending (mode "'a'" or "'a+'"), any "seek()" operations will be undone at the next write. If the file is only opened for writing in append mode (mode "'a'"), this method is essentially a no-op, but it remains useful for files opened in append mode with reading enabled (mode "'a+'"). If the file is opened in text mode (without "'b'"), only offsets returned by "tell()" are legal. Use of other offsets causes undefined behavior. Note that not all file objects are seekable. Changed in version 2.6: Passing float values as offset has been deprecated. file.tell() Return the file's current position, like "stdio"'s "ftell()". Note: On Windows, "tell()" can return illegal values (after an "fgets()") when reading files with Unix-style line-endings. Use binary mode ("'rb'") to circumvent this problem. file.truncate([size]) Truncate the file's size. If the optional *size* argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file's current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. Availability: Windows, many Unix variants. file.write(str) Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the "flush()" or "close()" method is called. file.writelines(sequence) Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. (The name is intended to match "readlines()"; "writelines()" does not add line separators.) Files support the iterator protocol. Each iteration returns the same result as "readline()", and iteration ends when the "readline()" method returns an empty string. File objects also offer a number of other interesting attributes. These are not required for file-like objects, but should be implemented if they make sense for the particular object. file.closed bool indicating the current state of the file object. This is a read-only attribute; the "close()" method changes the value. It may not be available on all file-like objects. file.encoding The encoding that this file uses. When Unicode strings are written to a file, they will be converted to byte strings using this encoding. In addition, when the file is connected to a terminal, the attribute gives the encoding that the terminal is likely to use (that information might be incorrect if the user has misconfigured the terminal). The attribute is read-only and may not be present on all file-like objects. It may also be "None", in which case the file uses the system default encoding for converting Unicode strings. New in version 2.3. file.errors The Unicode error handler used along with the encoding. New in version 2.6. file.mode The I/O mode for the file. If the file was created using the "open()" built-in function, this will be the value of the *mode* parameter. This is a read-only attribute and may not be present on all file-like objects. file.name If the file object was created using "open()", the name of the file. Otherwise, some string that indicates the source of the file object, of the form "<...>". This is a read-only attribute and may not be present on all file-like objects. file.newlines If Python was built with *universal newlines* enabled (the default) this read-only attribute exists, and for files opened in universal newline read mode it keeps track of the types of newlines encountered while reading the file. The values it can take are "'\r'", "'\n'", "'\r\n'", "None" (unknown, no newlines read yet) or a tuple containing all the newline types seen, to indicate that multiple newline conventions were encountered. For files not opened in universal newlines read mode the value of this attribute will be "None". file.softspace Boolean that indicates whether a space character needs to be printed before another value when using the "print" statement. Classes that are trying to simulate a file object should also have a writable "softspace" attribute, which should be initialized to zero. This will be automatic for most classes implemented in Python (care may be needed for objects that override attribute access); types implemented in C will have to provide a writable "softspace" attribute. Note: This attribute is not used to control the "print" statement, but to allow the implementation of "print" to keep track of its internal state. sbltin-file-objectss The Null Object *************** This object is returned by functions that don't explicitly return a value. It supports no special operations. There is exactly one null object, named "None" (a built-in name). It is written as "None". sbltin-null-objects3 Type Objects ************ Type objects represent the various object types. An object's type is accessed by the built-in function "type()". There are no special operations on types. The standard module "types" defines names for all standard built-in types. Types are written like this: "". sbltin-type-objectss Boolean operations ****************** or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: "False", "None", numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. (See the "__nonzero__()" special method for a way to change this.) The operator "not" yields "True" if its argument is false, "False" otherwise. The expression "x and y" first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The expression "x or y" first evaluates *x*; if *x* is true, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. (Note that neither "and" nor "or" restrict the value and type they return to "False" and "True", but rather return the last evaluated argument. This is sometimes useful, e.g., if "s" is a string that should be replaced by a default value if it is empty, the expression "s or 'foo'" yields the desired value. Because "not" has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., "not 'foo'" yields "False", not "''".) tbooleanss% The "break" statement ********************* break_stmt ::= "break" "break" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop, skipping the optional "else" clause if the loop has one. If a "for" loop is terminated by "break", the loop control target keeps its current value. When "break" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the loop. tbreaks Emulating callable objects ************************** object.__call__(self[, args...]) Called when the instance is "called" as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". scallable-typess Calls ***** A call calls a callable object (e.g., a *function*) with a possibly empty series of *arguments*: call ::= primary "(" [argument_list [","] | expression genexpr_for] ")" argument_list ::= positional_arguments ["," keyword_arguments] ["," "*" expression] ["," keyword_arguments] ["," "**" expression] | keyword_arguments ["," "*" expression] ["," "**" expression] | "*" expression ["," keyword_arguments] ["," "**" expression] | "**" expression positional_arguments ::= expression ("," expression)* keyword_arguments ::= keyword_item ("," keyword_item)* keyword_item ::= identifier "=" expression A trailing comma may be present after the positional and keyword arguments but does not affect the semantics. The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and certain class instances themselves are callable; extensions may define additional callable object types). All argument expressions are evaluated before the call is attempted. Please refer to section Function definitions for the syntax of formal *parameter* lists. If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a "TypeError" exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is "None", it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. (Default values are calculated, once, when the function is defined; thus, a mutable object such as a list or dictionary used as default value will be shared by all calls that don't specify an argument value for the corresponding slot; this should usually be avoided.) If there are any unfilled slots for which no default value is specified, a "TypeError" exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. **CPython implementation detail:** An implementation may provide built-in functions whose positional parameters do not have names, even if they are 'named' for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use "PyArg_ParseTuple()" to parse their arguments. If there are more positional arguments than there are formal parameter slots, a "TypeError" exception is raised, unless a formal parameter using the syntax "*identifier" is present; in this case, that formal parameter receives a tuple containing the excess positional arguments (or an empty tuple if there were no excess positional arguments). If any keyword argument does not correspond to a formal parameter name, a "TypeError" exception is raised, unless a formal parameter using the syntax "**identifier" is present; in this case, that formal parameter receives a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary if there were no excess keyword arguments. If the syntax "*expression" appears in the function call, "expression" must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments *x1*, ..., *xN*, and "expression" evaluates to a sequence *y1*, ..., *yM*, this is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*. A consequence of this is that although the "*expression" syntax may appear *after* some keyword arguments, it is processed *before* the keyword arguments (and the "**expression" argument, if any -- see below). So: >>> def f(a, b): ... print a, b ... >>> f(b=1, *(2,)) 2 1 >>> f(a=1, *(2,)) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'a' >>> f(1, *(2,)) 1 2 It is unusual for both keyword arguments and the "*expression" syntax to be used in the same call, so in practice this confusion does not arise. If the syntax "**expression" appears in the function call, "expression" must evaluate to a mapping, the contents of which are treated as additional keyword arguments. In the case of a keyword appearing in both "expression" and as an explicit keyword argument, a "TypeError" exception is raised. Formal parameters using the syntax "*identifier" or "**identifier" cannot be used as positional argument slots or as keyword argument names. Formal parameters using the syntax "(sublist)" cannot be used as keyword argument names; the outermost sublist corresponds to a single unnamed argument slot, and the argument value is assigned to the sublist using the usual tuple assignment rules after all other parameter processing is done. A call always returns some value, possibly "None", unless it raises an exception. How this value is computed depends on the type of the callable object. If it is--- a user-defined function: The code block for the function is executed, passing it the argument list. The first thing the code block will do is bind the formal parameters to the arguments; this is described in section Function definitions. When the code block executes a "return" statement, this specifies the return value of the function call. a built-in function or method: The result is up to the interpreter; see Built-in Functions for the descriptions of built-in functions and methods. a class object: A new instance of that class is returned. a class instance method: The corresponding user-defined function is called, with an argument list that is one longer than the argument list of the call: the instance becomes the first argument. a class instance: The class must define a "__call__()" method; the effect is then the same as if that method was called. tcallssJ Class definitions ***************** A class definition defines a class object (see section The standard type hierarchy): classdef ::= "class" classname [inheritance] ":" suite inheritance ::= "(" [expression_list] ")" classname ::= identifier A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class's suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class's suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. **Programmer's note:** Variables defined in the class definition are class variables; they are shared by all instances. To create instance variables, they can be set in a method with "self.name = value". Both class and instance variables are accessible through the notation ""self.name"", and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For *new-style class*es, descriptors can be used to create instance variables with different implementation details. Class definitions, like function definitions, may be wrapped by one or more *decorator* expressions. The evaluation rules for the decorator expressions are the same as for functions. The result must be a class object, which is then bound to the class name. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] Currently, control "flows off the end" except in the case of an exception or the execution of a "return", "continue", or "break" statement. [3] A string literal appearing as the first statement in the function body is transformed into the function's "__doc__" attribute and therefore the function's *docstring*. [4] A string literal appearing as the first statement in the class body is transformed into the namespace's "__doc__" item and therefore the class's *docstring*. tclasss$ Comparisons *********** Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like "a < b < c" have the interpretation that is conventional in mathematics: comparison ::= or_expr ( comp_operator or_expr )* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!=" | "is" ["not"] | ["not"] "in" Comparisons yield boolean values: "True" or "False". Comparisons can be chained arbitrarily, e.g., "x < y <= z" is equivalent to "x < y and y <= z", except that "y" is evaluated only once (but in both cases "z" is not evaluated at all when "x < y" is found to be false). Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ..., *opN* are comparison operators, then "a op1 b op2 c ... y opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", except that each expression is evaluated at most once. Note that "a op1 b op2 c" doesn't imply any kind of comparison between *a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though perhaps not pretty). The forms "<>" and "!=" are equivalent; for consistency with C, "!=" is preferred; where "!=" is mentioned below "<>" is also accepted. The "<>" spelling is considered obsolescent. Value comparisons ================= The operators "<", ">", "==", ">=", "<=", and "!=" compare the values of two objects. The objects do not need to have the same type. Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object's value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation. Types can customize their comparison behavior by implementing a "__cmp__()" method or *rich comparison methods* like "__lt__()", described in Basic customization. The default behavior for equality comparison ("==" and "!=") is based on the identity of the objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of instances with different identities results in inequality. A motivation for this default behavior is the desire that all objects should be reflexive (i.e. "x is y" implies "x == y"). The default order comparison ("<", ">", "<=", and ">=") gives a consistent but arbitrary order. (This unusual definition of comparison was used to simplify the definition of operations like sorting and the "in" and "not in" operators. In the future, the comparison rules for objects of different types are likely to change.) The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that. The following list describes the comparison behavior of the most important built-in types. * Numbers of built-in numeric types (Numeric Types --- int, float, long, complex) and of the standard library types "fractions.Fraction" and "decimal.Decimal" can be compared within and across their types, with the restriction that complex numbers do not support order comparison. Within the limits of the types involved, they compare mathematically (algorithmically) correct without loss of precision. * Strings (instances of "str" or "unicode") compare lexicographically using the numeric equivalents (the result of the built-in function "ord()") of their characters. [4] When comparing an 8-bit string and a Unicode string, the 8-bit string is converted to Unicode. If the conversion fails, the strings are considered unequal. * Instances of "tuple" or "list" can be compared only within each of their types. Equality comparison across these types results in unequality, and ordering comparison across these types gives an arbitrary order. These sequences compare lexicographically using comparison of corresponding elements, whereby reflexivity of the elements is enforced. In enforcing reflexivity of elements, the comparison of collections assumes that for a collection element "x", "x == x" is always true. Based on that assumption, element identity is compared first, and element comparison is performed only for distinct elements. This approach yields the same result as a strict element comparison would, if the compared elements are reflexive. For non-reflexive elements, the result is different than for strict element comparison. Lexicographical comparison between built-in collections works as follows: * For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, "[1,2] == (1,2)" is false because the type is not the same). * Collections are ordered the same as their first unequal elements (for example, "cmp([1,2,x], [1,2,y])" returns the same as "cmp(x,y)"). If a corresponding element does not exist, the shorter collection is ordered first (for example, "[1,2] < [1,2,3]" is true). * Mappings (instances of "dict") compare equal if and only if they have equal *(key, value)* pairs. Equality comparison of the keys and values enforces reflexivity. Outcomes other than equality are resolved consistently, but are not otherwise defined. [5] * Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program. User-defined classes that customize their comparison behavior should follow some consistency rules, if possible: * Equality comparison should be reflexive. In other words, identical objects should compare equal: "x is y" implies "x == y" * Comparison should be symmetric. In other words, the following expressions should have the same result: "x == y" and "y == x" "x != y" and "y != x" "x < y" and "y > x" "x <= y" and "y >= x" * Comparison should be transitive. The following (non-exhaustive) examples illustrate that: "x > y and y > z" implies "x > z" "x < y and y <= z" implies "x < z" * Inverse comparison should result in the boolean negation. In other words, the following expressions should have the same result: "x == y" and "not x != y" "x < y" and "not x >= y" (for total ordering) "x > y" and "not x <= y" (for total ordering) The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See also the "total_ordering()" decorator. * The "hash()" result should be consistent with equality. Objects that are equal should either have the same hash value, or be marked as unhashable. Python does not enforce these consistency rules. Membership test operations ========================== The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. "x is not y" yields the inverse truth value. [6] t comparisonsspP Compound statements ******************* Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way. In general, compound statements span multiple lines, although in simple incarnations a whole compound statement may be contained in one line. The "if", "while" and "for" statements implement traditional control flow constructs. "try" specifies exception handlers and/or cleanup code for a group of statements. Function and class definitions are also syntactically compound statements. Compound statements consist of one or more 'clauses.' A clause consists of a header and a 'suite.' The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which "if" clause a following "else" clause would belong: if test1: if test2: print x Also note that the semicolon binds tighter than the colon in this context, so that in the following example, either all or none of the "print" statements are executed: if x < y < z: print x; print y; print z Summarizing: compound_stmt ::= if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT statement ::= stmt_list NEWLINE | compound_stmt stmt_list ::= simple_stmt (";" simple_stmt)* [";"] Note that statements always end in a "NEWLINE" possibly followed by a "DEDENT". Also note that optional continuation clauses always begin with a keyword that cannot start a statement, thus there are no ambiguities (the 'dangling "else"' problem is solved in Python by requiring nested "if" statements to be indented). The formatting of the grammar rules in the following sections places each clause on a separate line for clarity. The "if" statement ================== The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. The "while" statement ===================== The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. The "for" statement =================== The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there was no next item. The suite may assign to the variable(s) in the target list; this does not affect the next item assigned to it. The target list is not deleted when the loop is finished, but if the sequence is empty, it will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns a sequence of integers suitable to emulate the effect of Pascal's "for i := a to b do"; e.g., "range(3)" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) The "try" statement =================== The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression [("as" | ",") identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite Changed in version 2.5: In previous versions of Python, "try"..."except"..."finally" did not work. "try"..."except" had to be nested in "try"..."finally". The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) Before an except clause's suite is executed, details about the exception are assigned to three variables in the "sys" module: "sys.exc_type" receives the object identifying the exception; "sys.exc_value" receives the exception's parameter; "sys.exc_traceback" receives a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. These details are also available through the "sys.exc_info()" function, which returns a tuple "(exc_type, exc_value, exc_traceback)". Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if and when control flows off the end of the "try" clause. [2] Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a 'cleanup' handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception, it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception or executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"..."finally" statement, the "finally" clause is also executed 'on the way out.' A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation --- this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. The "with" statement ==================== New in version 2.5. The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"..."except"..."finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one "item" proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager's "__exit__()" is loaded for later use. 3. The context manager's "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager's "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Note: In Python 2.5, the "with" statement is only allowed when the "with_statement" feature has been enabled. It is always enabled in Python 2.6. Changed in version 2.7: Support for multiple context expressions. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. Function definitions ==================== A function definition defines a user-defined function object (see section The standard type hierarchy): decorated ::= decorators (classdef | funcdef) decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE funcdef ::= "def" funcname "(" [parameter_list] ")" ":" suite dotted_name ::= identifier ("." identifier)* parameter_list ::= (defparameter ",")* ( "*" identifier ["," "**" identifier] | "**" identifier | defparameter [","] ) defparameter ::= parameter ["=" expression] sublist ::= parameter ("," parameter)* [","] parameter ::= identifier | "(" sublist ")" funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [3] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code: @f1(arg) @f2 def func(): pass is equivalent to: def func(): pass func = f1(arg)(f2(func)) When one or more top-level *parameters* have the form *parameter* "=" *expression*, the function is said to have "default parameter values." For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form ""*identifier"" is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ""**identifier"" is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a ""def"" statement can be passed around or assigned to another name just like a function defined by a lambda expression. The ""def"" form is actually more powerful since it allows the execution of multiple statements. **Programmer's note:** Functions are first-class objects. A ""def"" form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. Class definitions ================= A class definition defines a class object (see section The standard type hierarchy): classdef ::= "class" classname [inheritance] ":" suite inheritance ::= "(" [expression_list] ")" classname ::= identifier A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class's suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class's suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace. **Programmer's note:** Variables defined in the class definition are class variables; they are shared by all instances. To create instance variables, they can be set in a method with "self.name = value". Both class and instance variables are accessible through the notation ""self.name"", and an instance variable hides a class variable with the same name when accessed in this way. Class variables can be used as defaults for instance variables, but using mutable values there can lead to unexpected results. For *new-style class*es, descriptors can be used to create instance variables with different implementation details. Class definitions, like function definitions, may be wrapped by one or more *decorator* expressions. The evaluation rules for the decorator expressions are the same as for functions. The result must be a class object, which is then bound to the class name. -[ Footnotes ]- [1] The exception is propagated to the invocation stack unless there is a "finally" clause which happens to raise another exception. That new exception causes the old one to be lost. [2] Currently, control "flows off the end" except in the case of an exception or the execution of a "return", "continue", or "break" statement. [3] A string literal appearing as the first statement in the function body is transformed into the function's "__doc__" attribute and therefore the function's *docstring*. [4] A string literal appearing as the first statement in the class body is transformed into the namespace's "__doc__" item and therefore the class's *docstring*. tcompounds With Statement Context Managers ******************************* New in version 2.5. A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method's return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller's responsibility. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. scontext-managerss The "continue" statement ************************ continue_stmt ::= "continue" "continue" may only occur syntactically nested in a "for" or "while" loop, but not nested in a function or class definition or "finally" clause within that loop. It continues with the next cycle of the nearest enclosing loop. When "continue" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really starting the next loop cycle. tcontinuesB Arithmetic conversions ********************** When a description of an arithmetic operator below uses the phrase "the numeric arguments are converted to a common type," the arguments are coerced using the coercion rules listed at Coercion rules. If both arguments are standard numeric types, the following coercions are applied: * If either argument is a complex number, the other is converted to complex; * otherwise, if either argument is a floating point number, the other is converted to floating point; * otherwise, if either argument is a long integer, the other is converted to long integer; * otherwise, both must be plain integers and no conversion is necessary. Some additional rules apply for certain operators (e.g., a string left argument to the '%' operator). Extensions can define their own coercions. t conversionss/ Basic customization ******************* object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass's "__new__()" method using "super(currentclass, cls).__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance's "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance's "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class's "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "BaseClass.__init__(self, [args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customise it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a "__del__()" method, the derived class's "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn't directly call "x.__del__()" --- the former decrements the reference count for "x" by one, and the latter is only called when "x"'s reference count reaches zero. Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in "sys.exc_traceback" keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in "sys.last_traceback" keeps the stack frame alive). The first situation can only be remedied by explicitly breaking the cycles; the latter two situations can be resolved by storing "None" in "sys.exc_traceback" or "sys.last_traceback". Circular references which are garbage are detected when the option cycle detector is enabled (it's on by default), but can only be cleaned up if there are no Python-level "__del__()" methods involved. Refer to the documentation for the "gc" module for more information about how "__del__()" methods are handled by the cycle detector, particularly the description of the "garbage" value. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. Also, when "__del__()" is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the "__del__()" method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, "__del__()" methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. See also the "-R" command-line option. object.__repr__(self) Called by the "repr()" built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an "informal" string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by the "str()" built-in function and by the "print" statement to compute the "informal" string representation of an object. This differs from "__repr__()" in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) New in version 2.1. These are the so-called "rich comparison" methods, and are called for comparison operators in preference to "__cmp__()" below. The correspondence between operator symbols and method names is as follows: "xy" call "x.__ne__(y)", "x>y" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. There are no implied relationships among the comparison operators. The truth of "x==y" does not imply that "x!=y" is false. Accordingly, when defining "__eq__()", one should also define "__ne__()" so that the operators will behave as expected. See the paragraph on "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, "__lt__()" and "__gt__()" are each other's reflection, "__le__()" and "__ge__()" are each other's reflection, and "__eq__()" and "__ne__()" are their own reflection. Arguments to rich comparison methods are never coerced. To automatically generate ordering operations from a single root operation, see "functools.total_ordering()". object.__cmp__(self, other) Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if "self < other", zero if "self == other", a positive integer if "self > other". If no "__cmp__()", "__eq__()" or "__ne__()" operation is defined, class instances are compared by object identity ("address"). See also the description of "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by "__cmp__()" has been removed since Python 1.5.) object.__rcmp__(self, other) Changed in version 2.1: No longer supported. object.__hash__(self) Called by built-in function "hash()" and for operations on members of hashed collections including "set", "frozenset", and "dict". "__hash__()" should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example: def __hash__(self): return hash((self.name, self.nick, self.color)) If a class does not define a "__cmp__()" or "__eq__()" method it should not define a "__hash__()" operation either; if it defines "__cmp__()" or "__eq__()" but not "__hash__()", its instances will not be usable in hashed collections. If a class defines mutable objects and implements a "__cmp__()" or "__eq__()" method, it should not implement "__hash__()", since hashable collection implementations require that an object's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket). User-defined classes have "__cmp__()" and "__hash__()" methods by default; with them, all objects compare unequal (except with themselves) and "x.__hash__()" returns a result derived from "id(x)". Classes which inherit a "__hash__()" method from a parent class but change the meaning of "__cmp__()" or "__eq__()" such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting "__hash__ = None" in the class definition. Doing so means that not only will instances of the class raise an appropriate "TypeError" when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking "isinstance(obj, collections.Hashable)" (unlike classes which define their own "__hash__()" to explicitly raise "TypeError"). Changed in version 2.5: "__hash__()" may now also return a long integer object; the 32-bit integer is then derived from the hash of that object. Changed in version 2.6: "__hash__" may now be set to "None" to explicitly flag instances of a class as unhashable. object.__nonzero__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True", or their integer equivalents "0" or "1". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__nonzero__()", all its instances are considered true. object.__unicode__(self) Called to implement "unicode()" built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding. t customizations "pdb" --- The Python Debugger ***************************** **Source code:** Lib/pdb.py ====================================================================== The module "pdb" defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control. The debugger is extensible --- it is actually defined as the class "Pdb". This is currently undocumented but easily understood by reading the source. The extension interface uses the modules "bdb" and "cmd". The debugger's prompt is "(Pdb)". Typical usage to run a program under control of the debugger is: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') > (0)?() (Pdb) continue > (1)?() (Pdb) continue NameError: 'spam' > (1)?() (Pdb) "pdb.py" can also be invoked as a script to debug other scripts. For example: python -m pdb myscript.py When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post- mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb's state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program's exit. New in version 2.4: Restarting post-mortem behavior added. The typical usage to break into the debugger from a running program is to insert import pdb; pdb.set_trace() at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the "c" command. The typical usage to inspect a crashed program is: >>> import pdb >>> import mymodule >>> mymodule.test() Traceback (most recent call last): File "", line 1, in File "./mymodule.py", line 4, in test test2() File "./mymodule.py", line 3, in test2 print spam NameError: spam >>> pdb.pm() > ./mymodule.py(3)test2() -> print spam (Pdb) The module defines the following functions; each enters the debugger in a slightly different way: pdb.run(statement[, globals[, locals]]) Execute the *statement* (given as a string) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type "continue", or you can step through the statement using "step" or "next" (all these commands are explained below). The optional *globals* and *locals* arguments specify the environment in which the code is executed; by default the dictionary of the module "__main__" is used. (See the explanation of the "exec" statement or the "eval()" built-in function.) pdb.runeval(expression[, globals[, locals]]) Evaluate the *expression* (given as a string) under debugger control. When "runeval()" returns, it returns the value of the expression. Otherwise this function is similar to "run()". pdb.runcall(function[, argument, ...]) Call the *function* (a function or method object, not a string) with the given arguments. When "runcall()" returns, it returns whatever the function call returned. The debugger prompt appears as soon as the function is entered. pdb.set_trace() Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). pdb.post_mortem([traceback]) Enter post-mortem debugging of the given *traceback* object. If no *traceback* is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used). pdb.pm() Enter post-mortem debugging of the traceback found in "sys.last_traceback". The "run*" functions and "set_trace()" are aliases for instantiating the "Pdb" class and calling the method of the same name. If you want to access further features, you have to do this yourself: class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None) "Pdb" is the debugger class. The *completekey*, *stdin* and *stdout* arguments are passed to the underlying "cmd.Cmd" class; see the description there. The *skip* argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] Example call to enable tracing with *skip*: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 2.7: The *skip* argument. run(statement[, globals[, locals]]) runeval(expression[, globals[, locals]]) runcall(function[, argument, ...]) set_trace() See the documentation for the functions explained above. tdebuggers The "del" statement ******************* del_stmt ::= "del" target_list Deletion is recursively defined very similar to the way assignment is defined. Rather than spelling it out in full details, here are some hints. Deletion of a target list recursively deletes each target, from left to right. Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a "global" statement in the same code block. If the name is unbound, a "NameError" exception will be raised. It is illegal to delete a name from the local namespace if it occurs as a free variable in a nested block. Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the right type (but even this is determined by the sliced object). tdels Dictionary displays ******************* A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression dict_comprehension ::= expression ":" expression comp_for A dictionary display yields a new dictionary object. If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary's value for that key will be the last one given. A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual "for" and "if" clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced. Restrictions on the types of the key values are listed earlier in section The standard type hierarchy. (To summarize, the key type should be *hashable*, which excludes all mutable objects.) Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails. tdicts+ Interaction with dynamic features ********************************* There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. sdynamic-featuressE The "if" statement ****************** The "if" statement is used for conditional execution: if_stmt ::= "if" expression ":" suite ( "elif" expression ":" suite )* ["else" ":" suite] It selects exactly one of the suites by evaluating the expressions one by one until one is found to be true (see section Boolean operations for the definition of true and false); then that suite is executed (and no other part of the "if" statement is executed or evaluated). If all expressions are false, the suite of the "else" clause, if present, is executed. telsesh Exceptions ********** Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" ... "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the "termination" model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Exceptions can also be identified by strings, in which case the "except" clause is selected by object identity. An arbitrary value can be raised along with the identifying string which can be passed to the handler. Note: Messages to exceptions are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. t exceptionss The "exec" statement ******************** exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] This statement supports dynamic execution of Python code. The first expression should evaluate to either a Unicode string, a *Latin-1* encoded string, an open file object, a code object, or a tuple. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1] If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed. For the interpretation of a tuple, see below. In all cases, the code that's executed is expected to be valid as file input (see section File input). Be aware that the "return" and "yield" statements may not be used outside of function definitions even within the context of code passed to the "exec" statement. In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after "in" is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, they are used for the global and local variables, respectively. If provided, *locals* can be any mapping object. Remember that at module level, globals and locals are the same dictionary. If two separate objects are given as *globals* and *locals*, the code will be executed as if it were embedded in a class definition. The first expression may also be a tuple of length 2 or 3. In this case, the optional parts must be omitted. The form "exec(expr, globals)" is equivalent to "exec expr in globals", while the form "exec(expr, globals, locals)" is equivalent to "exec expr in globals, locals". The tuple form of "exec" provides compatibility with Python 3, where "exec" is a function rather than a statement. Changed in version 2.4: Formerly, *locals* was required to be a dictionary. As a side effect, an implementation may insert additional keys into the dictionaries given besides those corresponding to variable names set by the executed code. For example, the current implementation may add a reference to the dictionary of the built-in module "__builtin__" under the key "__builtins__" (!). **Programmer's hints:** dynamic evaluation of expressions is supported by the built-in function "eval()". The built-in functions "globals()" and "locals()" return the current global and local dictionary, respectively, which may be useful to pass around for use by "exec". -[ Footnotes ]- [1] Note that the parser only accepts the Unix-style end of line convention. If you are reading the code from a file, make sure to use *universal newlines* mode to convert Windows or Mac-style newlines. texecs& Execution model *************** Naming and binding ================== *Names* refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the *binding* of that name established in the innermost function block containing the use. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the '**-c**' option) is a code block. The file read by the built-in function "execfile()" is a code block. The string argument passed to the built-in function "eval()" and to the "exec" statement is a code block. The expression read and evaluated by the built-in function "input()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block's execution has completed. A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods -- this includes generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block's *environment*. If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. When a name is not found at all, a "NameError" exception is raised. If the name refers to a local variable that has not been bound, a "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, in the second position of an "except" clause header or after "as" in a "with" statement. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). It is illegal to unbind a name that is referenced by an enclosing scope; the compiler will report a "SyntaxError". Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "__builtin__". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module's dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "__builtin__" (note: no 's'); when in any other module, "__builtins__" is an alias for the dictionary of the "__builtin__" module itself. "__builtins__" can be set to a user-created dictionary to create a weak form of restricted execution. **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "__builtin__" (no 's') module and modify its attributes appropriately. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class. Names defined at the class scope are not visible in methods. Interaction with dynamic features --------------------------------- There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. Exceptions ========== Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is *raised* at the point where the error is detected; it may be *handled* by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the "raise" statement. Exception handlers are specified with the "try" ... "except" statement. The "finally" clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Python uses the "termination" model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top). When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is "SystemExit". Exceptions are identified by class instances. The "except" clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. Exceptions can also be identified by strings, in which case the "except" clause is selected by object identity. An arbitrary value can be raised along with the identifying string which can be passed to the handler. Note: Messages to exceptions are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. See also the description of the "try" statement in section The try statement and "raise" statement in section The raise statement. -[ Footnotes ]- [1] This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. t execmodelsK Expression lists **************** expression_list ::= expression ( "," expression )* [","] An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. The trailing comma is required only to create a single tuple (a.k.a. a *singleton*); it is optional in all other cases. A single expression without a trailing comma doesn't create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: "()".) t exprlistss Floating point literals *********************** Floating point literals are described by the following lexical definitions: floatnumber ::= pointfloat | exponentfloat pointfloat ::= [intpart] fraction | intpart "." exponentfloat ::= (intpart | pointfloat) exponent intpart ::= digit+ fraction ::= "." digit+ exponent ::= ("e" | "E") ["+" | "-"] digit+ Note that the integer and exponent parts of floating point numbers can look like octal integers, but are interpreted using radix 10. For example, "077e010" is legal, and denotes the same number as "77e10". The allowed range of floating point literals is implementation- dependent. Some examples of floating point literals: 3.14 10. .001 1e100 3.14e-10 0e0 Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator "-" and the literal "1". tfloatingsZ The "for" statement ******************* The "for" statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the "expression_list". The suite is then executed once for each item provided by the iterator, in the order of ascending indices. Each item in turn is assigned to the target list using the standard rules for assignments, and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty), the suite in the "else" clause, if present, is executed, and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and continues with the next item, or with the "else" clause if there was no next item. The suite may assign to the variable(s) in the target list; this does not affect the next item assigned to it. The target list is not deleted when the loop is finished, but if the sequence is empty, it will not have been assigned to at all by the loop. Hint: the built-in function "range()" returns a sequence of integers suitable to emulate the effect of Pascal's "for i := a to b do"; e.g., "range(3)" returns the list "[0, 1, 2]". Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, i.e. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., for x in a[:]: if x < 0: a.remove(x) tforsQ Format String Syntax ******************** The "str.format()" method and the "Formatter" class share the same syntax for format strings (although in the case of "Formatter", subclasses can define their own format string syntax). Format strings contain "replacement fields" surrounded by curly braces "{}". Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: "{{" and "}}". The grammar for a replacement field is as follows: replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}" field_name ::= arg_name ("." attribute_name | "[" element_index "]")* arg_name ::= [identifier | integer] attribute_name ::= identifier element_index ::= integer | index_string index_string ::= + conversion ::= "r" | "s" format_spec ::= In less formal terms, the replacement field can start with a *field_name* that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is preceded by an exclamation point "'!'", and a *format_spec*, which is preceded by a colon "':'". These specify a non-default format for the replacement value. See also the Format Specification Mini-Language section. The *field_name* itself begins with an *arg_name* that is either a number or a keyword. If it's a number, it refers to a positional argument, and if it's a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings "'10'" or "':-]'") within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form "'.name'" selects the named attribute using "getattr()", while an expression of the form "'[index]'" does an index lookup using "__getitem__()". Changed in version 2.7: The positional argument specifiers can be omitted, so "'{} {}'" is equivalent to "'{0} {1}'". Some simple format string examples: "First, thou shalt count to {0}" # References first positional argument "Bring me a {}" # Implicitly references the first positional argument "From {} to {}" # Same as "From {0} to {1}" "My quest is {name}" # References keyword argument 'name' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the "__format__()" method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling "__format__()", the normal formatting logic is bypassed. Two conversion flags are currently supported: "'!s'" which calls "str()" on the value, and "'!r'" which calls "repr()". Some examples: "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own "formatting mini-language" or interpretation of the *format_spec*. Most built-in types support a common formatting mini-language, which is described in the next section. A *format_spec* field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the *format_spec* string is interpreted. This allows the formatting of a value to be dynamically specified. See the Format examples section for some examples. Format Specification Mini-Language ================================== "Format specifications" are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax). They can also be passed directly to the built-in "format()" function. Each formattable type may define how the format specification is to be interpreted. Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types. A general convention is that an empty format string ("""") produces the same result as if you had called "str()" on the value. A non-empty format string typically modifies the result. The general form of a *standard format specifier* is: format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::= align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= integer precision ::= integer type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" If a valid *align* value is specified, it can be preceded by a *fill* character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (""{"" or ""}"") as the *fill* character when using the "str.format()" method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn't affect the "format()" function. The meaning of the various alignment options is as follows: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'<'" | Forces the field to be left-aligned within the available | | | space (this is the default for most objects). | +-----------+------------------------------------------------------------+ | "'>'" | Forces the field to be right-aligned within the available | | | space (this is the default for numbers). | +-----------+------------------------------------------------------------+ | "'='" | Forces the padding to be placed after the sign (if any) | | | but before the digits. This is used for printing fields | | | in the form '+000000120'. This alignment option is only | | | valid for numeric types. It becomes the default when '0' | | | immediately precedes the field width. | +-----------+------------------------------------------------------------+ | "'^'" | Forces the field to be centered within the available | | | space. | +-----------+------------------------------------------------------------+ Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The *sign* option is only valid for number types, and can be one of the following: +-----------+------------------------------------------------------------+ | Option | Meaning | +===========+============================================================+ | "'+'" | indicates that a sign should be used for both positive as | | | well as negative numbers. | +-----------+------------------------------------------------------------+ | "'-'" | indicates that a sign should be used only for negative | | | numbers (this is the default behavior). | +-----------+------------------------------------------------------------+ | space | indicates that a leading space should be used on positive | | | numbers, and a minus sign on negative numbers. | +-----------+------------------------------------------------------------+ The "'#'" option is only valid for integers, and only for binary, octal, or hexadecimal output. If present, it specifies that the output will be prefixed by "'0b'", "'0o'", or "'0x'", respectively. The "','" option signals the use of a comma for a thousands separator. For a locale aware separator, use the "'n'" integer presentation type instead. Changed in version 2.7: Added the "','" option (see also **PEP 378**). *width* is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the *width* field by a zero ("'0'") character enables sign-aware zero-padding for numeric types. This is equivalent to a *fill* character of "'0'" with an *alignment* type of "'='". The *precision* is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with "'f'" and "'F'", or before and after the decimal point for a floating point value formatted with "'g'" or "'G'". For non- number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The *precision* is not allowed for integer values. Finally, the *type* determines how the data should be presented. The available string presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'s'" | String format. This is the default type for strings and | | | may be omitted. | +-----------+------------------------------------------------------------+ | None | The same as "'s'". | +-----------+------------------------------------------------------------+ The available integer presentation types are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'b'" | Binary format. Outputs the number in base 2. | +-----------+------------------------------------------------------------+ | "'c'" | Character. Converts the integer to the corresponding | | | unicode character before printing. | +-----------+------------------------------------------------------------+ | "'d'" | Decimal Integer. Outputs the number in base 10. | +-----------+------------------------------------------------------------+ | "'o'" | Octal format. Outputs the number in base 8. | +-----------+------------------------------------------------------------+ | "'x'" | Hex format. Outputs the number in base 16, using lower- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'X'" | Hex format. Outputs the number in base 16, using upper- | | | case letters for the digits above 9. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'d'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | None | The same as "'d'". | +-----------+------------------------------------------------------------+ In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except "'n'" and "None"). When doing so, "float()" is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are: +-----------+------------------------------------------------------------+ | Type | Meaning | +===========+============================================================+ | "'e'" | Exponent notation. Prints the number in scientific | | | notation using the letter 'e' to indicate the exponent. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'E'" | Exponent notation. Same as "'e'" except it uses an upper | | | case 'E' as the separator character. | +-----------+------------------------------------------------------------+ | "'f'" | Fixed point. Displays the number as a fixed-point number. | | | The default precision is "6". | +-----------+------------------------------------------------------------+ | "'F'" | Fixed point. Same as "'f'". | +-----------+------------------------------------------------------------+ | "'g'" | General format. For a given precision "p >= 1", this | | | rounds the number to "p" significant digits and then | | | formats the result in either fixed-point format or in | | | scientific notation, depending on its magnitude. The | | | precise rules are as follows: suppose that the result | | | formatted with presentation type "'e'" and precision "p-1" | | | would have exponent "exp". Then if "-4 <= exp < p", the | | | number is formatted with presentation type "'f'" and | | | precision "p-1-exp". Otherwise, the number is formatted | | | with presentation type "'e'" and precision "p-1". In both | | | cases insignificant trailing zeros are removed from the | | | significand, and the decimal point is also removed if | | | there are no remaining digits following it. Positive and | | | negative infinity, positive and negative zero, and nans, | | | are formatted as "inf", "-inf", "0", "-0" and "nan" | | | respectively, regardless of the precision. A precision of | | | "0" is treated as equivalent to a precision of "1". The | | | default precision is "6". | +-----------+------------------------------------------------------------+ | "'G'" | General format. Same as "'g'" except switches to "'E'" if | | | the number gets too large. The representations of infinity | | | and NaN are uppercased, too. | +-----------+------------------------------------------------------------+ | "'n'" | Number. This is the same as "'g'", except that it uses the | | | current locale setting to insert the appropriate number | | | separator characters. | +-----------+------------------------------------------------------------+ | "'%'" | Percentage. Multiplies the number by 100 and displays in | | | fixed ("'f'") format, followed by a percent sign. | +-----------+------------------------------------------------------------+ | None | The same as "'g'". | +-----------+------------------------------------------------------------+ Format examples =============== This section contains examples of the "str.format()" syntax and comparison with the old "%"-formatting. In most of the cases the syntax is similar to the old "%"-formatting, with the addition of the "{}" and with ":" used instead of "%". For example, "'%03.2f'" can be translated to "'{:03.2f}'". The new format syntax also supports new and different options, shown in the follow examples. Accessing arguments by position: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 'abracadabra' Accessing arguments by name: >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 'Coordinates: 37.24N, -115.81W' >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 'Coordinates: 37.24N, -115.81W' Accessing arguments' attributes: >>> c = 3-5j >>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c) 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' >>> class Point(object): ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ... >>> str(Point(4, 2)) 'Point(4, 2)' Accessing arguments' items: >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5' Replacing "%s" and "%r": >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') "repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign: >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases: >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma as a thousands separator: >>> '{:,}'.format(1234567890) '1,234,567,890' Expressing a percentage: >>> points = 19.5 >>> total = 22 >>> 'Correct answers: {:.2%}'.format(points/total) 'Correct answers: 88.64%' Using type-specific formatting: >>> import datetime >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) '2010-07-04 12:15:58' Nesting arguments and more complex examples: >>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ... 'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right' >>> >>> octets = [192, 168, 0, 1] >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 'C0A80001' >>> int(_, 16) 3232235521 >>> >>> width = 5 >>> for num in range(5,12): ... for base in 'dXob': ... print '{0:{width}{base}}'.format(num, base=base, width=width), ... print ... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 t formatstringssz Function definitions ******************** A function definition defines a user-defined function object (see section The standard type hierarchy): decorated ::= decorators (classdef | funcdef) decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE funcdef ::= "def" funcname "(" [parameter_list] ")" ":" suite dotted_name ::= identifier ("." identifier)* parameter_list ::= (defparameter ",")* ( "*" identifier ["," "**" identifier] | "**" identifier | defparameter [","] ) defparameter ::= parameter ["=" expression] sublist ::= parameter ("," parameter)* [","] parameter ::= identifier | "(" sublist ")" funcname ::= identifier A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called. The function definition does not execute the function body; this gets executed only when the function is called. [3] A function definition may be wrapped by one or more *decorator* expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code: @f1(arg) @f2 def func(): pass is equivalent to: def func(): pass func = f1(arg)(f2(func)) When one or more top-level *parameters* have the form *parameter* "=" *expression*, the function is said to have "default parameter values." For a parameter with a default value, the corresponding *argument* may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. **Default parameter values are evaluated when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use "None" as the default, and explicitly test for it in the body of the function, e.g.: def whats_on_the_telly(penguin=None): if penguin is None: penguin = [] penguin.append("property of the zoo") return penguin Function call semantics are described in more detail in section Calls. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form ""*identifier"" is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ""**identifier"" is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary. It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section Lambdas. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a ""def"" statement can be passed around or assigned to another name just like a function defined by a lambda expression. The ""def"" form is actually more powerful since it allows the execution of multiple statements. **Programmer's note:** Functions are first-class objects. A ""def"" form executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. See section Naming and binding for details. tfunctions The "global" statement ********************** global_stmt ::= "global" identifier ("," identifier)* The "global" statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without "global", although free variables may refer to globals without being declared global. Names listed in a "global" statement must not be used in the same code block textually preceding that "global" statement. Names listed in a "global" statement must not be defined as formal parameters or in a "for" loop control target, "class" definition, function definition, or "import" statement. **CPython implementation detail:** The current implementation does not enforce the latter two restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program. **Programmer's note:** "global" is a directive to the parser. It applies only to code parsed at the same time as the "global" statement. In particular, a "global" statement contained in an "exec" statement does not affect the code block *containing* the "exec" statement, and code contained in an "exec" statement is unaffected by "global" statements in the code containing the "exec" statement. The same applies to the "eval()", "execfile()" and "compile()" functions. tglobals Reserved classes of identifiers ******************************* Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "__builtin__" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between "private" attributes of base and derived classes. See section Identifiers (Names). s id-classess Identifiers and keywords ************************ Identifiers (also referred to as *names*) are described by the following lexical definitions: identifier ::= (letter|"_") (letter | digit | "_")* letter ::= lowercase | uppercase lowercase ::= "a"..."z" uppercase ::= "A"..."Z" digit ::= "0"..."9" Identifiers are unlimited in length. Case is significant. Keywords ======== The following identifiers are used as reserved words, or *keywords* of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here: and del from not while as elif global or with assert else if pass yield break except import print class exec in raise continue finally is return def for lambda try Changed in version 2.4: "None" became a constant and is now recognized by the compiler as a name for the built-in object "None". Although it is not a keyword, you cannot assign a different object to it. Changed in version 2.5: Using "as" and "with" as identifiers triggers a warning. To use them as keywords, enable the "with_statement" future feature . Changed in version 2.6: "as" and "with" are full keywords. Reserved classes of identifiers =============================== Certain classes of identifiers (besides keywords) have special meanings. These classes are identified by the patterns of leading and trailing underscore characters: "_*" Not imported by "from module import *". The special identifier "_" is used in the interactive interpreter to store the result of the last evaluation; it is stored in the "__builtin__" module. When not in interactive mode, "_" has no special meaning and is not defined. See section The import statement. Note: The name "_" is often used in conjunction with internationalization; refer to the documentation for the "gettext" module for more information on this convention. "__*__" System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. *Any* use of "__*__" names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. "__*" Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between "private" attributes of base and derived classes. See section Identifiers (Names). t identifierstifs% Imaginary literals ****************** Imaginary literals are described by the following lexical definitions: imagnumber ::= (floatnumber | intpart) ("j" | "J") An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., "(3+4j)". Some examples of imaginary literals: 3.14j 10.j 10j .001j 1e100j 3.14e-10j t imaginarysK. The "import" statement ********************** import_stmt ::= "import" module ["as" name] ( "," module ["as" name] )* | "from" relative_module "import" identifier ["as" name] ( "," identifier ["as" name] )* | "from" relative_module "import" "(" identifier ["as" name] ( "," identifier ["as" name] )* [","] ")" | "from" module "import" "*" module ::= (identifier ".")* identifier relative_module ::= "."* module | "."+ name ::= identifier Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the "import" statement occurs). The statement comes in two forms differing on whether it uses the "from" keyword. The first form (without "from") repeats these steps for each identifier in the list. The form with "from" performs step (1) once, and then performs step (2) repeatedly. To understand how step (1) occurs, one must first understand how Python handles hierarchical naming of modules. To help organize modules and provide a hierarchy in naming, Python has a concept of packages. A package can contain other packages and modules while modules cannot contain other modules or packages. From a file system perspective, packages are directories and modules are files. Once the name of the module is known (unless otherwise specified, the term "module" will refer to both packages and modules), searching for the module or package can begin. The first place checked is "sys.modules", the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import. If the module is not found in the cache, then "sys.meta_path" is searched (the specification for "sys.meta_path" can be found in **PEP 302**). The object is a list of *finder* objects which are queried in order as to whether they know how to load the module by calling their "find_module()" method with the name of the module. If the module happens to be contained within a package (as denoted by the existence of a dot in the name), then a second argument to "find_module()" is given as the value of the "__path__" attribute from the parent package (everything up to the last dot in the name of the module being imported). If a finder can find the module it returns a *loader* (discussed later) or returns "None". If none of the finders on "sys.meta_path" are able to find the module then some implicitly defined finders are queried. Implementations of Python vary in what implicit meta path finders are defined. The one they all do define, though, is one that handles "sys.path_hooks", "sys.path_importer_cache", and "sys.path". The implicit finder searches for the requested module in the "paths" specified in one of two places ("paths" do not have to be file system paths). If the module being imported is supposed to be contained within a package then the second argument passed to "find_module()", "__path__" on the parent package, is used as the source of paths. If the module is not contained in a package then "sys.path" is used as the source of paths. Once the source of paths is chosen it is iterated over to find a finder that can handle that path. The dict at "sys.path_importer_cache" caches finders for paths and is checked for a finder. If the path does not have a finder cached then "sys.path_hooks" is searched by calling each object in the list with a single argument of the path, returning a finder or raises "ImportError". If a finder is returned then it is cached in "sys.path_importer_cache" and then used for that path entry. If no finder can be found but the path exists then a value of "None" is stored in "sys.path_importer_cache" to signify that an implicit, file- based finder that handles modules stored as individual files should be used for that path. If the path does not exist then a finder which always returns "None" is placed in the cache for the path. If no finder can find the module then "ImportError" is raised. Otherwise some finder returned a loader whose "load_module()" method is called with the name of the module to load (see **PEP 302** for the original definition of loaders). A loader has several responsibilities to perform on a module it loads. First, if the module already exists in "sys.modules" (a possibility if the loader is called outside of the import machinery) then it is to use that module for initialization and not a new module. But if the module does not exist in "sys.modules" then it is to be added to that dict before initialization begins. If an error occurs during loading of the module and it was added to "sys.modules" it is to be removed from the dict. If an error occurs but the module was already in "sys.modules" it is left in the dict. The loader must set several attributes on the module. "__name__" is to be set to the name of the module. "__file__" is to be the "path" to the file unless the module is built-in (and thus listed in "sys.builtin_module_names") in which case the attribute is not set. If what is being imported is a package then "__path__" is to be set to a list of paths to be searched when looking for modules and packages contained within the package being imported. "__package__" is optional but should be set to the name of package that contains the module or package (the empty string is used for module not contained in a package). "__loader__" is also optional but should be set to the loader object that is loading the module. If an error occurs during loading then the loader raises "ImportError" if some other exception is not already being propagated. Otherwise the loader returns the module that was loaded and initialized. When step (1) finishes without raising an exception, step (2) can begin. The first form of "import" statement binds the module name in the local namespace to the module object, and then goes on to import the next identifier, if any. If the module name is followed by "as", the name following "as" is used as the local name for the module. The "from" form does not bind the module name: it goes through the list of identifiers, looks each one of them up in the module found in step (1), and binds the name in the local namespace to the object thus found. As with the first form of "import", an alternate local name can be supplied by specifying ""as" localname". If a name is not found, "ImportError" is raised. If the list of identifiers is replaced by a star ("'*'"), all public names defined in the module are bound in the local namespace of the "import" statement.. The *public names* defined by a module are determined by checking the module's namespace for a variable named "__all__"; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in "__all__" are all considered public and are required to exist. If "__all__" is not defined, the set of public names includes all names found in the module's namespace which do not begin with an underscore character ("'_'"). "__all__" should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module). The "from" form with "*" may only occur in a module scope. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after "from" you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute "from . import mod" from a module in the "pkg" package then you will end up importing "pkg.mod". If you execute "from ..subpkg2 import mod" from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The specification for relative imports is contained within **PEP 328**. "importlib.import_module()" is provided to support applications that determine which modules need to be loaded dynamically. Future statements ================= A *future statement* is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard. future_statement ::= "from" "__future__" "import" feature ["as" name] ("," feature ["as" name])* | "from" "__future__" "import" "(" feature ["as" name] ("," feature ["as" name])* [","] ")" feature ::= identifier name ::= identifier A future statement must appear near the top of the module. The only lines that can appear before a future statement are: * the module docstring (if any), * comments, * blank lines, and * other future statements. The features recognized by Python 2.6 are "unicode_literals", "print_function", "absolute_import", "division", "generators", "nested_scopes" and "with_statement". "generators", "with_statement", "nested_scopes" are redundant in Python version 2.6 and above because they are always enabled. A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime. For any given release, the compiler knows which feature names have been defined, and raises a compile-time error if a future statement contains a feature not known to it. The direct runtime semantics are the same as for any import statement: there is a standard module "__future__", described later, and it will be imported in the usual way at the time the future statement is executed. The interesting runtime semantics depend on the specific feature enabled by the future statement. Note that there is nothing special about the statement: import __future__ [as name] That is not a future statement; it's an ordinary import statement with no special semantics or syntax restrictions. Code compiled by an "exec" statement or calls to the built-in functions "compile()" and "execfile()" that occur in a module "M" containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can, starting with Python 2.2 be controlled by optional arguments to "compile()" --- see the documentation of that function for details. A future statement typed at an interactive interpreter prompt will take effect for the rest of the interpreter session. If an interpreter is started with the "-i" option, is passed a script name to execute, and the script includes a future statement, it will be in effect in the interactive session started after the script is executed. See also: **PEP 236** - Back to the __future__ The original proposal for the __future__ mechanism. timportsO Membership test operations ************************** The operators "in" and "not in" test for membership. "x in s" evaluates to "True" if *x* is a member of *s*, and "False" otherwise. "x not in s" returns the negation of "x in s". All built-in sequences and set types support this as well as dictionary, for which "in" tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression "x in y" is equivalent to "any(x is e or x == e for e in y)". For the string and bytes types, "x in y" is "True" if and only if *x* is a substring of *y*. An equivalent test is "y.find(x) != -1". Empty strings are always considered to be a substring of any other string, so """ in "abc"" will return "True". For user-defined classes which define the "__contains__()" method, "x in y" returns "True" if "y.__contains__(x)" returns a true value, and "False" otherwise. For user-defined classes which do not define "__contains__()" but do define "__iter__()", "x in y" is "True" if some value "z" with "x == z" is produced while iterating over "y". If an exception is raised during the iteration, it is as if "in" raised that exception. Lastly, the old-style iteration protocol is tried: if a class defines "__getitem__()", "x in y" is "True" if and only if there is a non- negative integer index *i* such that "x == y[i]", and all lower integer indices do not raise "IndexError" exception. (If any other exception is raised, it is as if "in" raised that exception). The operator "not in" is defined to have the inverse true value of "in". tinso Integer and long integer literals ********************************* Integer and long integer literals are described by the following lexical definitions: longinteger ::= integer ("l" | "L") integer ::= decimalinteger | octinteger | hexinteger | bininteger decimalinteger ::= nonzerodigit digit* | "0" octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+ hexinteger ::= "0" ("x" | "X") hexdigit+ bininteger ::= "0" ("b" | "B") bindigit+ nonzerodigit ::= "1"..."9" octdigit ::= "0"..."7" bindigit ::= "0" | "1" hexdigit ::= digit | "a"..."f" | "A"..."F" Although both lower case "'l'" and upper case "'L'" are allowed as suffix for long integers, it is strongly recommended to always use "'L'", since the letter "'l'" looks too much like the digit "'1'". Plain integer literals that are above the largest representable plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted as if they were long integers instead. [1] There is no limit for long integer literals apart from what can be stored in available memory. Some examples of plain integer literals (first row) and long integer literals (second and third rows): 7 2147483647 0177 3L 79228162514264337593543950336L 0377L 0x100000000L 79228162514264337593543950336 0xdeadbeef tintegerssx Lambdas ******* lambda_expr ::= "lambda" [parameter_list]: expression old_lambda_expr ::= "lambda" [parameter_list]: old_expression Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. They are a shorthand to create anonymous functions; the expression "lambda arguments: expression" yields a function object. The unnamed object behaves like a function object defined with def name(arguments): return expression See section Function definitions for the syntax of parameter lists. Note that functions created with lambda expressions cannot contain statements. tlambdas List displays ************* A list display is a possibly empty series of expressions enclosed in square brackets: list_display ::= "[" [expression_list | list_comprehension] "]" list_comprehension ::= expression list_for list_for ::= "for" target_list "in" old_expression_list [list_iter] old_expression_list ::= old_expression [("," old_expression)+ [","]] old_expression ::= or_test | old_lambda_expr list_iter ::= list_for | list_if list_if ::= "if" old_expression [list_iter] A list display yields a new list object. Its contents are specified by providing either a list of expressions or a list comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a list comprehension is supplied, it consists of a single expression followed by at least one "for" clause and zero or more "for" or "if" clauses. In this case, the elements of the new list are those that would be produced by considering each of the "for" or "if" clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached [1]. tlistss Naming and binding ****************** *Names* refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the *binding* of that name established in the innermost function block containing the use. A *block* is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the '**-c**' option) is a code block. The file read by the built-in function "execfile()" is a code block. The string argument passed to the built-in function "eval()" and to the "exec" statement is a code block. The expression read and evaluated by the built-in function "input()" is a code block. A code block is executed in an *execution frame*. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block's execution has completed. A *scope* defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods -- this includes generator expressions since they are implemented using a function scope. This means that the following will fail: class A: a = 42 b = list(a + i for i in range(10)) When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block's *environment*. If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a *free variable*. When a name is not found at all, a "NameError" exception is raised. If the name refers to a local variable that has not been bound, a "UnboundLocalError" exception is raised. "UnboundLocalError" is a subclass of "NameError". The following constructs bind names: formal parameters to functions, "import" statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, "for" loop header, in the second position of an "except" clause header or after "as" in a "with" statement. The "import" statement of the form "from ... import *" binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level. A target occurring in a "del" statement is also considered bound for this purpose (though the actual semantics are to unbind the name). It is illegal to unbind a name that is referenced by an enclosing scope; the compiler will report a "SyntaxError". Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block). If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module "__builtin__". The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name. The builtins namespace associated with the execution of a code block is actually found by looking up the name "__builtins__" in its global namespace; this should be a dictionary or a module (in the latter case the module's dictionary is used). By default, when in the "__main__" module, "__builtins__" is the built-in module "__builtin__" (note: no 's'); when in any other module, "__builtins__" is an alias for the dictionary of the "__builtin__" module itself. "__builtins__" can be set to a user-created dictionary to create a weak form of restricted execution. **CPython implementation detail:** Users should not touch "__builtins__"; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should "import" the "__builtin__" (no 's') module and modify its attributes appropriately. The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called "__main__". The "global" statement has the same scope as a name binding operation in the same block. If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution. The namespace of the class definition becomes the attribute dictionary of the class. Names defined at the class scope are not visible in methods. Interaction with dynamic features ================================= There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosing scope, it is illegal to delete the name. An error will be reported at compile time. If the wild card form of import --- "import *" --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError". If "exec" is used in a function and the function contains or is a nested block with free variables, the compiler will raise a "SyntaxError" unless the exec explicitly specifies the local namespace for the "exec". (In other words, "exec obj" would be illegal, but "exec obj in ns" would be legal.) The "eval()", "execfile()", and "input()" functions and the "exec" statement do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. [1] The "exec" statement and the "eval()" and "execfile()" functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. tnamings Numeric literals **************** There are four types of numeric literals: plain integers, long integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number). Note that numeric literals do not include a sign; a phrase like "-1" is actually an expression composed of the unary operator '"-"' and the literal "1". tnumberssy Emulating numeric types *********************** The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()" (described below). Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__div__(self, other) object.__truediv__(self, other) The division operator ("/") is implemented by these methods. The "__truediv__()" method is used when "__future__.division" is in effect, otherwise "__div__()" is used. If only one of these two methods is defined, the object will not support division in the alternate context; "TypeError" will be raised instead. object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rdiv__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "/", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types. [2] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand's type is a subclass of the left operand's type and that subclass provides the reflected method for the operation, this method will be called before the left operand's non-reflected method. This behavior allows subclasses to override their ancestors' operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__idiv__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, to execute the statement "x += y", where *x* is an instance of a class that has an "__iadd__()" method, "x.__iadd__(y)" is called. If *x* is an instance of a class that does not define a "__iadd__()" method, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__long__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()", "long()", and "float()". Should return a value of the appropriate type. object.__oct__(self) object.__hex__(self) Called to implement the built-in functions "oct()" and "hex()". Should return a string value. object.__index__(self) Called to implement "operator.index()". Also called whenever Python needs an integer object (such as in slicing). Must return an integer (int or long). New in version 2.5. object.__coerce__(self, other) Called to implement "mixed-mode" numeric arithmetic. Should either return a 2-tuple containing *self* and *other* converted to a common numeric type, or "None" if conversion is impossible. When the common type would be the type of "other", it is sufficient to return "None", since the interpreter will also ask the other object to attempt a coercion (but sometimes, if the implementation of the other type cannot be changed, it is useful to do the conversion to the other type here). A return value of "NotImplemented" is equivalent to returning "None". s numeric-typessZ Objects, values and types ************************* *Objects* are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann's model of a "stored program computer," code is also represented by objects.) Every object has an identity, a type and a value. An object's *identity* never changes once it has been created; you may think of it as the object's address in memory. The '"is"' operator compares the identity of two objects; the "id()" function returns an integer representing its identity (currently implemented as its address). An object's *type* is also unchangeable. [1] An object's type determines the operations that the object supports (e.g., "does it have a length?") and also defines the possible values for objects of that type. The "type()" function returns an object's type (which is an object itself). The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter's value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object's mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable. Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether --- it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable. **CPython implementation detail:** CPython currently uses a reference- counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the "gc" module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (ex: always close files). Note that the use of the implementation's tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a '"try"..."except"' statement may keep objects alive. Some objects contain references to "external" resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a "close()" method. Programs are strongly recommended to explicitly close such objects. The '"try"..."finally"' statement provides a convenient way to do this. Some objects contain references to other objects; these are called *containers*. Examples of containers are tuples, lists and dictionaries. The references are part of a container's value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after "a = 1; b = 1", "a" and "b" may or may not refer to the same object with the value one, depending on the implementation, but after "c = []; d = []", "c" and "d" are guaranteed to refer to two different, unique, newly created empty lists. (Note that "c = d = []" assigns the same object to both "c" and "d".) tobjectss Operator precedence ******************* The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right --- see section Comparisons --- and exponentiation, which groups from right to left). +-------------------------------------------------+---------------------------------------+ | Operator | Description | +=================================================+=======================================+ | "lambda" | Lambda expression | +-------------------------------------------------+---------------------------------------+ | "if" -- "else" | Conditional expression | +-------------------------------------------------+---------------------------------------+ | "or" | Boolean OR | +-------------------------------------------------+---------------------------------------+ | "and" | Boolean AND | +-------------------------------------------------+---------------------------------------+ | "not" "x" | Boolean NOT | +-------------------------------------------------+---------------------------------------+ | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership | | ">=", "<>", "!=", "==" | tests and identity tests | +-------------------------------------------------+---------------------------------------+ | "|" | Bitwise OR | +-------------------------------------------------+---------------------------------------+ | "^" | Bitwise XOR | +-------------------------------------------------+---------------------------------------+ | "&" | Bitwise AND | +-------------------------------------------------+---------------------------------------+ | "<<", ">>" | Shifts | +-------------------------------------------------+---------------------------------------+ | "+", "-" | Addition and subtraction | +-------------------------------------------------+---------------------------------------+ | "*", "/", "//", "%" | Multiplication, division, remainder | | | [7] | +-------------------------------------------------+---------------------------------------+ | "+x", "-x", "~x" | Positive, negative, bitwise NOT | +-------------------------------------------------+---------------------------------------+ | "**" | Exponentiation [8] | +-------------------------------------------------+---------------------------------------+ | "x[index]", "x[index:index]", | Subscription, slicing, call, | | "x(arguments...)", "x.attribute" | attribute reference | +-------------------------------------------------+---------------------------------------+ | "(expressions...)", "[expressions...]", "{key: | Binding or tuple display, list | | value...}", "`expressions...`" | display, dictionary display, string | | | conversion | +-------------------------------------------------+---------------------------------------+ -[ Footnotes ]- [1] In Python 2.3 and later releases, a list comprehension "leaks" the control variables of each "for" it contains into the containing scope. However, this behavior is deprecated, and relying on it will not work in Python 3. [2] While "abs(x%y) < abs(y)" is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double- precision number, in order that "-1e-100 % 1e100" have the same sign as "1e100", the computed result is "-1e-100 + 1e100", which is numerically exactly equal to "1e100". The function "math.fmod()" returns a result whose sign matches the sign of the first argument instead, and so returns "-1e-100" in this case. Which approach is more appropriate depends on the application. [3] If x is very close to an exact integer multiple of y, it's possible for "floor(x/y)" to be one larger than "(x-x%y)/y" due to rounding. In such cases, Python returns the latter result, in order to preserve that "divmod(x,y)[0] * y + x % y" be very close to "x". [4] The Unicode standard distinguishes between *code points* (e.g. U+0041) and *abstract characters* (e.g. "LATIN CAPITAL LETTER A"). While most abstract characters in Unicode are only represented using one code point, there is a number of abstract characters that can in addition be represented using a sequence of more than one code point. For example, the abstract character "LATIN CAPITAL LETTER C WITH CEDILLA" can be represented as a single *precomposed character* at code position U+00C7, or as a sequence of a *base character* at code position U+0043 (LATIN CAPITAL LETTER C), followed by a *combining character* at code position U+0327 (COMBINING CEDILLA). The comparison operators on unicode strings compare at the level of Unicode code points. This may be counter-intuitive to humans. For example, "u"\u00C7" == u"\u0043\u0327"" is "False", even though both strings represent the same abstract character "LATIN CAPITAL LETTER C WITH CEDILLA". To compare strings at the level of abstract characters (that is, in a way intuitive to humans), use "unicodedata.normalize()". [5] Earlier versions of Python used lexicographic comparison of the sorted (key, value) lists, but this was very expensive for the common case of comparing for equality. An even earlier version of Python compared dictionaries by identity only, but this caused surprises because people expected to be able to test a dictionary for emptiness by comparing it to "{}". [6] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the "is" operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [7] The "%" operator is also used for string formatting; the same precedence applies. [8] The power operator "**" binds less tightly than an arithmetic or bitwise unary operator on its right, that is, "2**-1" is "0.5". soperator-summarysx The "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation --- when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) tpasss The power operator ****************** The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right. The syntax is: power ::= primary ["**" u_expr] Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation order for the operands): "-1**2" results in "-1". The power operator has the same semantics as the built-in "pow()" function, when called with two arguments: it yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common type. The result type is that of the arguments after coercion. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, "10**2" returns "100", but "10**-2" returns "0.01". (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised). Raising "0.0" to a negative power results in a "ZeroDivisionError". Raising a negative number to a fractional power results in a "ValueError". tpowers The "print" statement ********************* print_stmt ::= "print" ([expression ("," expression)* [","]] | ">>" expression [("," expression)+ [","]]) "print" evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is a whitespace character except "' '", or (3) when the last write operation on standard output was not a "print" statement. (In some cases it may be functional to write an empty string to standard output for this reason.) Note: Objects which act like file objects but which are not the built-in file objects often do not properly emulate this aspect of the file object's behavior, so it is best not to rely on this. A "'\n'" character is written at the end, unless the "print" statement ends with a comma. This is the only action if the statement contains just the keyword "print". Standard output is defined as the file object named "stdout" in the built-in module "sys". If no such object exists, or if it does not have a "write()" method, a "RuntimeError" exception is raised. "print" also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as ""print" chevron." In this form, the first expression after the ">>" must evaluate to a "file-like" object, specifically an object that has a "write()" method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to "None", then "sys.stdout" is used as the file for output. tprints The "raise" statement ********************* raise_stmt ::= "raise" [expression ["," expression ["," expression]]] If no expressions are present, "raise" re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a "TypeError" exception is raised indicating that this is an error (if running under IDLE, a "Queue.Empty" exception is raised instead). Otherwise, "raise" evaluates the expressions to get three objects, using "None" as the value of omitted expressions. The first two objects are used to determine the *type* and *value* of the exception. If the first object is an instance, the type of the exception is the class of the instance, the instance itself is the value, and the second object must be "None". If the first object is a class, it becomes the type of the exception. The second object is used to determine the exception value: If it is an instance of the class, the instance becomes the exception value. If the second object is a tuple, it is used as the argument list for the class constructor; if it is "None", an empty argument list is used, and any other object is treated as a single argument to the constructor. The instance so created by calling the constructor is used as the exception value. If a third object is present and not "None", it must be a traceback object (see section The standard type hierarchy), and it is substituted instead of the current location as the place where the exception occurred. If the third object is present and not a traceback object or "None", a "TypeError" exception is raised. The three-expression form of "raise" is useful to re-raise an exception transparently in an except clause, but "raise" with no expressions should be preferred if the exception to be re-raised was the most recently active exception in the current scope. Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement. traises The "return" statement ********************** return_stmt ::= "return" [expression_list] "return" may only occur syntactically nested in a function definition, not within a nested class definition. If an expression list is present, it is evaluated, else "None" is substituted. "return" leaves the current function call with the expression list (or "None") as return value. When "return" passes control out of a "try" statement with a "finally" clause, that "finally" clause is executed before really leaving the function. In a generator function, the "return" statement is not allowed to include an "expression_list". In that context, a bare "return" indicates that the generator is done and will cause "StopIteration" to be raised. treturns Emulating container types ************************* The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. (For backwards compatibility, the method "__getslice__()" (see below) can also be defined to handle simple, but not extended slices.) It is also recommended that mappings provide the methods "keys()", "values()", "items()", "has_key()", "get()", "clear()", "setdefault()", "iterkeys()", "itervalues()", "iteritems()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python's standard dictionary objects. The "UserDict" module provides a "DictMixin" class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define "__coerce__()" or other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should be equivalent of "has_key()"; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "iterkeys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn't define a "__nonzero__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__nonzero__()" method. object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method "iterkeys()". Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". New in version 2.6. The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don't define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. ssequence-typess Shifting operations ******************* The shifting operations have lower priority than the arithmetic operations: shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument. A right shift by *n* bits is defined as division by "pow(2, n)". A left shift by *n* bits is defined as multiplication with "pow(2, n)". Negative shift counts raise a "ValueError" exception. Note: In the current implementation, the right-hand operand is required to be at most "sys.maxsize". If the right-hand operand is larger than "sys.maxsize" an "OverflowError" exception is raised. tshiftings Slicings ******** A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or "del" statements. The syntax for a slicing: slicing ::= simple_slicing | extended_slicing simple_slicing ::= primary "[" short_slice "]" extended_slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice | ellipsis proper_slice ::= short_slice | long_slice short_slice ::= [lower_bound] ":" [upper_bound] long_slice ::= short_slice ":" [stride] lower_bound ::= expression upper_bound ::= expression stride ::= expression ellipsis ::= "..." There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice nor ellipses). Similarly, when the slice list has exactly one short slice and no trailing comma, the interpretation as a simple slicing takes priority over that as an extended slicing. The semantics for a simple slicing are as follows. The primary must evaluate to a sequence object. The lower and upper bound expressions, if present, must evaluate to plain integers; defaults are zero and the "sys.maxint", respectively. If either bound is negative, the sequence's length is added to it. The slicing now selects all items with index *k* such that "i <= k < j" where *i* and *j* are the specified lower and upper bounds. This may be an empty sequence. It is not an error if *i* or *j* lie outside the range of valid indexes (such items don't exist so they aren't selected). The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, and it is indexed with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in "Ellipsis" object. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose "start", "stop" and "step" attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting "None" for missing expressions. tslicingss Special Attributes ****************** The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the "dir()" built-in function. object.__dict__ A dictionary or other mapping object used to store an object's (writable) attributes. object.__methods__ Deprecated since version 2.2: Use the built-in function "dir()" to get a list of an object's attributes. This attribute is no longer available. object.__members__ Deprecated since version 2.2: Use the built-in function "dir()" to get a list of an object's attributes. This attribute is no longer available. instance.__class__ The class to which a class instance belongs. class.__bases__ The tuple of base classes of a class object. definition.__name__ The name of the class, type, function, method, descriptor, or generator instance. The following attributes are only supported by *new-style class*es. class.__mro__ This attribute is a tuple of classes that are considered when looking for base classes during method resolution. class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in "__mro__". class.__subclasses__() Each new-style class keeps a list of weak references to its immediate subclasses. This method returns a list of all those references still alive. Example: >>> int.__subclasses__() [] -[ Footnotes ]- [1] Additional information on these special methods may be found in the Python Reference Manual (Basic customization). [2] As a consequence, the list "[1, 2]" is considered equal to "[1.0, 2.0]", and similarly for tuples. [3] They must have since the parser can't tell the type of the operands. [4] Cased characters are those with general category property being one of "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase). [5] To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted. [6] The advantage of leaving the newline on is that returning an empty string is then an unambiguous EOF indication. It is also possible (in cases where it might matter, for example, if you want to make an exact copy of a file while scanning its lines) to tell whether the last line of a file ended in a newline or not (yes this happens!). t specialattrssa Special method names ******************** A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python's approach to *operator overloading*, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named "__getitem__()", and "x" is an instance of this class, then "x[i]" is roughly equivalent to "x.__getitem__(i)" for old-style classes and "type(x).__getitem__(x, i)" for new-style classes. Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically "AttributeError" or "TypeError"). When implementing a class that emulates any built-in type, it is important that the emulation only be implemented to the degree that it makes sense for the object being modelled. For example, some sequences may work well with retrieval of individual elements, but extracting a slice may not make sense. (One example of this is the "NodeList" interface in the W3C's Document Object Model.) Basic customization =================== object.__new__(cls[, ...]) Called to create a new instance of class *cls*. "__new__()" is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of "__new__()" should be the new object instance (usually an instance of *cls*). Typical implementations create a new instance of the class by invoking the superclass's "__new__()" method using "super(currentclass, cls).__new__(cls[, ...])" with appropriate arguments and then modifying the newly-created instance as necessary before returning it. If "__new__()" returns an instance of *cls*, then the new instance's "__init__()" method will be invoked like "__init__(self[, ...])", where *self* is the new instance and the remaining arguments are the same as were passed to "__new__()". If "__new__()" does not return an instance of *cls*, then the new instance's "__init__()" method will not be invoked. "__new__()" is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[, ...]) Called after the instance has been created (by "__new__()"), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an "__init__()" method, the derived class's "__init__()" method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "BaseClass.__init__(self, [args...])". Because "__new__()" and "__init__()" work together in constructing objects ("__new__()" to create it, and "__init__()" to customise it), no non-"None" value may be returned by "__init__()"; doing so will cause a "TypeError" to be raised at runtime. object.__del__(self) Called when the instance is about to be destroyed. This is also called a destructor. If a base class has a "__del__()" method, the derived class's "__del__()" method, if any, must explicitly call it to ensure proper deletion of the base class part of the instance. Note that it is possible (though not recommended!) for the "__del__()" method to postpone destruction of the instance by creating a new reference to it. It may then be called at a later time when this new reference is deleted. It is not guaranteed that "__del__()" methods are called for objects that still exist when the interpreter exits. Note: "del x" doesn't directly call "x.__del__()" --- the former decrements the reference count for "x" by one, and the latter is only called when "x"'s reference count reaches zero. Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in "sys.exc_traceback" keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in "sys.last_traceback" keeps the stack frame alive). The first situation can only be remedied by explicitly breaking the cycles; the latter two situations can be resolved by storing "None" in "sys.exc_traceback" or "sys.last_traceback". Circular references which are garbage are detected when the option cycle detector is enabled (it's on by default), but can only be cleaned up if there are no Python-level "__del__()" methods involved. Refer to the documentation for the "gc" module for more information about how "__del__()" methods are handled by the cycle detector, particularly the description of the "garbage" value. Warning: Due to the precarious circumstances under which "__del__()" methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to "sys.stderr" instead. Also, when "__del__()" is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the "__del__()" method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, "__del__()" methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the "__del__()" method is called. See also the "-R" command-line option. object.__repr__(self) Called by the "repr()" built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines "__repr__()" but not "__str__()", then "__repr__()" is also used when an "informal" string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. object.__str__(self) Called by the "str()" built-in function and by the "print" statement to compute the "informal" string representation of an object. This differs from "__repr__()" in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object. object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) New in version 2.1. These are the so-called "rich comparison" methods, and are called for comparison operators in preference to "__cmp__()" below. The correspondence between operator symbols and method names is as follows: "xy" call "x.__ne__(y)", "x>y" calls "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)". A rich comparison method may return the singleton "NotImplemented" if it does not implement the operation for a given pair of arguments. By convention, "False" and "True" are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an "if" statement), Python will call "bool()" on the value to determine if the result is true or false. There are no implied relationships among the comparison operators. The truth of "x==y" does not imply that "x!=y" is false. Accordingly, when defining "__eq__()", one should also define "__ne__()" so that the operators will behave as expected. See the paragraph on "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, "__lt__()" and "__gt__()" are each other's reflection, "__le__()" and "__ge__()" are each other's reflection, and "__eq__()" and "__ne__()" are their own reflection. Arguments to rich comparison methods are never coerced. To automatically generate ordering operations from a single root operation, see "functools.total_ordering()". object.__cmp__(self, other) Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if "self < other", zero if "self == other", a positive integer if "self > other". If no "__cmp__()", "__eq__()" or "__ne__()" operation is defined, class instances are compared by object identity ("address"). See also the description of "__hash__()" for some important notes on creating *hashable* objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by "__cmp__()" has been removed since Python 1.5.) object.__rcmp__(self, other) Changed in version 2.1: No longer supported. object.__hash__(self) Called by built-in function "hash()" and for operations on members of hashed collections including "set", "frozenset", and "dict". "__hash__()" should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example: def __hash__(self): return hash((self.name, self.nick, self.color)) If a class does not define a "__cmp__()" or "__eq__()" method it should not define a "__hash__()" operation either; if it defines "__cmp__()" or "__eq__()" but not "__hash__()", its instances will not be usable in hashed collections. If a class defines mutable objects and implements a "__cmp__()" or "__eq__()" method, it should not implement "__hash__()", since hashable collection implementations require that an object's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket). User-defined classes have "__cmp__()" and "__hash__()" methods by default; with them, all objects compare unequal (except with themselves) and "x.__hash__()" returns a result derived from "id(x)". Classes which inherit a "__hash__()" method from a parent class but change the meaning of "__cmp__()" or "__eq__()" such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting "__hash__ = None" in the class definition. Doing so means that not only will instances of the class raise an appropriate "TypeError" when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking "isinstance(obj, collections.Hashable)" (unlike classes which define their own "__hash__()" to explicitly raise "TypeError"). Changed in version 2.5: "__hash__()" may now also return a long integer object; the 32-bit integer is then derived from the hash of that object. Changed in version 2.6: "__hash__" may now be set to "None" to explicitly flag instances of a class as unhashable. object.__nonzero__(self) Called to implement truth value testing and the built-in operation "bool()"; should return "False" or "True", or their integer equivalents "0" or "1". When this method is not defined, "__len__()" is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither "__len__()" nor "__nonzero__()", all its instances are considered true. object.__unicode__(self) Called to implement "unicode()" built-in; should return a Unicode object. When this method is not defined, string conversion is attempted, and the result of string conversion is converted to Unicode using the system default encoding. Customizing attribute access ============================ The following methods can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of "x.name") for class instances. object.__getattr__(self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for "self"). "name" is the attribute name. This method should return the (computed) attribute value or raise an "AttributeError" exception. Note that if the attribute is found through the normal mechanism, "__getattr__()" is not called. (This is an intentional asymmetry between "__getattr__()" and "__setattr__()".) This is done both for efficiency reasons and because otherwise "__getattr__()" would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the "__getattribute__()" method below for a way to actually get total control in new-style classes. object.__setattr__(self, name, value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). *name* is the attribute name, *value* is the value to be assigned to it. If "__setattr__()" wants to assign to an instance attribute, it should not simply execute "self.name = value" --- this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., "self.__dict__[name] = value". For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, "object.__setattr__(self, name, value)". object.__delattr__(self, name) Like "__setattr__()" but for attribute deletion instead of assignment. This should only be implemented if "del obj.name" is meaningful for the object. More attribute access for new-style classes ------------------------------------------- The following methods only apply to new-style classes. object.__getattribute__(self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines "__getattr__()", the latter will not be called unless "__getattribute__()" either calls it explicitly or raises an "AttributeError". This method should return the (computed) attribute value or raise an "AttributeError" exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". Note: This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup for new-style classes. Implementing Descriptors ------------------------ The following methods only apply when an instance of the class containing the method (a so-called *descriptor* class) appears in an *owner* class (the descriptor must be in either the owner's class dictionary or in the class dictionary for one of its parents). In the examples below, "the attribute" refers to the attribute whose name is the key of the property in the owner class' "__dict__". object.__get__(self, instance, owner) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). *owner* is always the owner class, while *instance* is the instance that the attribute was accessed through, or "None" when the attribute is accessed through the *owner*. This method should return the (computed) attribute value or raise an "AttributeError" exception. object.__set__(self, instance, value) Called to set the attribute on an instance *instance* of the owner class to a new value, *value*. object.__delete__(self, instance) Called to delete the attribute on an instance *instance* of the owner class. Invoking Descriptors -------------------- In general, a descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol: "__get__()", "__set__()", and "__delete__()". If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary. For instance, "a.x" has a lookup chain starting with "a.__dict__['x']", then "type(a).__dict__['x']", and continuing through the base classes of "type(a)" excluding metaclasses. However, if the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined and how they were called. Note that descriptors are only invoked for new style objects or classes (ones that subclass "object()" or "type()"). The starting point for descriptor invocation is a binding, "a.x". How the arguments are assembled depends on "a": Direct Call The simplest and least common call is when user code directly invokes a descriptor method: "x.__get__(a)". Instance Binding If binding to a new-style object instance, "a.x" is transformed into the call: "type(a).__dict__['x'].__get__(a, type(a))". Class Binding If binding to a new-style class, "A.x" is transformed into the call: "A.__dict__['x'].__get__(None, A)". Super Binding If "a" is an instance of "super", then the binding "super(B, obj).m()" searches "obj.__class__.__mro__" for the base class "A" immediately preceding "B" and then invokes the descriptor with the call: "A.__dict__['m'].__get__(obj, obj.__class__)". For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined. A descriptor can define any combination of "__get__()", "__set__()" and "__delete__()". If it does not define "__get__()", then accessing the attribute will return the descriptor object itself unless there is a value in the object's instance dictionary. If the descriptor defines "__set__()" and/or "__delete__()", it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both "__get__()" and "__set__()", while non-data descriptors have just the "__get__()" method. Data descriptors with "__set__()" and "__get__()" defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances. Python methods (including "staticmethod()" and "classmethod()") are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class. The "property()" function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. __slots__ --------- By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance variables. The space consumption can become acute when creating large numbers of instances. The default can be overridden by defining *__slots__* in a new-style class definition. The *__slots__* declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because *__dict__* is not created for each instance. __slots__ This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. If defined in a new-style class, *__slots__* reserves space for the declared variables and prevents the automatic creation of *__dict__* and *__weakref__* for each instance. New in version 2.2. Notes on using *__slots__* * When inheriting from a class without *__slots__*, the *__dict__* attribute of that class will always be accessible, so a *__slots__* definition in the subclass is meaningless. * Without a *__dict__* variable, instances cannot be assigned new variables not listed in the *__slots__* definition. Attempts to assign to an unlisted variable name raises "AttributeError". If dynamic assignment of new variables is desired, then add "'__dict__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__dict__'" to the *__slots__* declaration would not enable the assignment of new attributes not specifically listed in the sequence of instance variable names. * Without a *__weakref__* variable for each instance, classes defining *__slots__* do not support weak references to its instances. If weak reference support is needed, then add "'__weakref__'" to the sequence of strings in the *__slots__* declaration. Changed in version 2.3: Previously, adding "'__weakref__'" to the *__slots__* declaration would not enable support for weak references. * *__slots__* are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by *__slots__*; otherwise, the class attribute would overwrite the descriptor assignment. * The action of a *__slots__* declaration is limited to the class where it is defined. As a result, subclasses will have a *__dict__* unless they also define *__slots__* (which must only contain names of any *additional* slots). * If a class defines a slot also defined in a base class, the instance variable defined by the base class slot is inaccessible (except by retrieving its descriptor directly from the base class). This renders the meaning of the program undefined. In the future, a check may be added to prevent this. * Nonempty *__slots__* does not work for classes derived from "variable-length" built-in types such as "long", "str" and "tuple". * Any non-string iterable may be assigned to *__slots__*. Mappings may also be used; however, in the future, special meaning may be assigned to the values corresponding to each key. * *__class__* assignment works only if both classes have the same *__slots__*. Changed in version 2.6: Previously, *__class__* assignment raised an error if either new or old class had *__slots__*. Customizing class creation ========================== By default, new-style classes are constructed using "type()". A class definition is read into a separate namespace and the value of class name is bound to the result of "type(name, bases, dict)". When the class definition is read, if *__metaclass__* is defined then the callable assigned to it will be called instead of "type()". This allows classes or functions to be written which monitor or alter the class creation process: * Modifying the class dictionary prior to the class being created. * Returning an instance of another class -- essentially performing the role of a factory function. These steps will have to be performed in the metaclass's "__new__()" method -- "type.__new__()" can then be called from this method to create a class with different properties. This example adds a new element to the class dictionary before creating the class: class metacls(type): def __new__(mcs, name, bases, dict): dict['foo'] = 'metacls was here' return type.__new__(mcs, name, bases, dict) You can of course also override other class methods (or add new methods); for example defining a custom "__call__()" method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. __metaclass__ This variable can be any callable accepting arguments for "name", "bases", and "dict". Upon class creation, the callable is used instead of the built-in "type()". New in version 2.2. The appropriate metaclass is determined by the following precedence rules: * If "dict['__metaclass__']" exists, it is used. * Otherwise, if there is at least one base class, its metaclass is used (this looks for a *__class__* attribute first and if not found, uses its type). * Otherwise, if a global variable named __metaclass__ exists, it is used. * Otherwise, the old-style, classic metaclass (types.ClassType) is used. The potential uses for metaclasses are boundless. Some ideas that have been explored including logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. Customizing instance and subclass checks ======================================== New in version 2.6. The following methods are used to override the default behavior of the "isinstance()" and "issubclass()" built-in functions. In particular, the metaclass "abc.ABCMeta" implements these methods in order to allow the addition of Abstract Base Classes (ABCs) as "virtual base classes" to any class or type (including built-in types), including other ABCs. class.__instancecheck__(self, instance) Return true if *instance* should be considered a (direct or indirect) instance of *class*. If defined, called to implement "isinstance(instance, class)". class.__subclasscheck__(self, subclass) Return true if *subclass* should be considered a (direct or indirect) subclass of *class*. If defined, called to implement "issubclass(subclass, class)". Note that these methods are looked up on the type (metaclass) of a class. They cannot be defined as class methods in the actual class. This is consistent with the lookup of special methods that are called on instances, only in this case the instance is itself a class. See also: **PEP 3119** - Introducing Abstract Base Classes Includes the specification for customizing "isinstance()" and "issubclass()" behavior through "__instancecheck__()" and "__subclasscheck__()", with motivation for this functionality in the context of adding Abstract Base Classes (see the "abc" module) to the language. Emulating callable objects ========================== object.__call__(self[, args...]) Called when the instance is "called" as a function; if this method is defined, "x(arg1, arg2, ...)" is a shorthand for "x.__call__(arg1, arg2, ...)". Emulating container types ========================= The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which "0 <= k < N" where *N* is the length of the sequence, or slice objects, which define a range of items. (For backwards compatibility, the method "__getslice__()" (see below) can also be defined to handle simple, but not extended slices.) It is also recommended that mappings provide the methods "keys()", "values()", "items()", "has_key()", "get()", "clear()", "setdefault()", "iterkeys()", "itervalues()", "iteritems()", "pop()", "popitem()", "copy()", and "update()" behaving similar to those for Python's standard dictionary objects. The "UserDict" module provides a "DictMixin" class to help create those methods from a base set of "__getitem__()", "__setitem__()", "__delitem__()", and "keys()". Mutable sequences should provide methods "append()", "count()", "index()", "extend()", "insert()", "pop()", "remove()", "reverse()" and "sort()", like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods "__add__()", "__radd__()", "__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" described below; they should not define "__coerce__()" or other numerical operators. It is recommended that both mappings and sequences implement the "__contains__()" method to allow efficient use of the "in" operator; for mappings, "in" should be equivalent of "has_key()"; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the "__iter__()" method to allow efficient iteration through the container; for mappings, "__iter__()" should be the same as "iterkeys()"; for sequences, it should iterate through the values. object.__len__(self) Called to implement the built-in function "len()". Should return the length of the object, an integer ">=" 0. Also, an object that doesn't define a "__nonzero__()" method and whose "__len__()" method returns zero is considered to be false in a Boolean context. **CPython implementation detail:** In CPython, the length is required to be at most "sys.maxsize". If the length is larger than "sys.maxsize" some features (such as "len()") may raise "OverflowError". To prevent raising "OverflowError" by truth value testing, an object must define a "__nonzero__()" method. object.__getitem__(self, key) Called to implement evaluation of "self[key]". For sequence types, the accepted keys should be integers and slice objects. Note that the special interpretation of negative indexes (if the class wishes to emulate a sequence type) is up to the "__getitem__()" method. If *key* is of an inappropriate type, "TypeError" may be raised; if of a value outside the set of indexes for the sequence (after any special interpretation of negative values), "IndexError" should be raised. For mapping types, if *key* is missing (not in the container), "KeyError" should be raised. Note: "for" loops expect that an "IndexError" will be raised for illegal indexes to allow proper detection of the end of the sequence. object.__missing__(self, key) Called by "dict"."__getitem__()" to implement "self[key]" for dict subclasses when key is not in the dictionary. object.__setitem__(self, key, value) Called to implement assignment to "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support changes to the values for keys, or if new keys can be added, or for sequences if elements can be replaced. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__delitem__(self, key) Called to implement deletion of "self[key]". Same note as for "__getitem__()". This should only be implemented for mappings if the objects support removal of keys, or for sequences if elements can be removed from the sequence. The same exceptions should be raised for improper *key* values as for the "__getitem__()" method. object.__iter__(self) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method "iterkeys()". Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types. object.__reversed__(self) Called (if present) by the "reversed()" built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order. If the "__reversed__()" method is not provided, the "reversed()" built-in will fall back to using the sequence protocol ("__len__()" and "__getitem__()"). Objects that support the sequence protocol should only provide "__reversed__()" if they can provide an implementation that is more efficient than the one provided by "reversed()". New in version 2.6. The membership test operators ("in" and "not in") are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. object.__contains__(self, item) Called to implement membership test operators. Should return true if *item* is in *self*, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don't define "__contains__()", the membership test first tries iteration via "__iter__()", then the old sequence iteration protocol via "__getitem__()", see this section in the language reference. Additional methods for emulation of sequence types ================================================== The following optional methods can be defined to further emulate sequence objects. Immutable sequences methods should at most only define "__getslice__()"; mutable sequences might define all three methods. object.__getslice__(self, i, j) Deprecated since version 2.0: Support slice objects as parameters to the "__getitem__()" method. (However, built-in types in CPython currently still implement "__getslice__()". Therefore, you have to override it in derived classes when implementing slicing.) Called to implement evaluation of "self[i:j]". The returned object should be of the same type as *self*. Note that missing *i* or *j* in the slice expression are replaced by zero or "sys.maxsize", respectively. If negative indexes are used in the slice, the length of the sequence is added to that index. If the instance does not implement the "__len__()" method, an "AttributeError" is raised. No guarantee is made that indexes adjusted this way are not still negative. Indexes which are greater than the length of the sequence are not modified. If no "__getslice__()" is found, a slice object is created instead, and passed to "__getitem__()" instead. object.__setslice__(self, i, j, sequence) Called to implement assignment to "self[i:j]". Same notes for *i* and *j* as for "__getslice__()". This method is deprecated. If no "__setslice__()" is found, or for extended slicing of the form "self[i:j:k]", a slice object is created, and passed to "__setitem__()", instead of "__setslice__()" being called. object.__delslice__(self, i, j) Called to implement deletion of "self[i:j]". Same notes for *i* and *j* as for "__getslice__()". This method is deprecated. If no "__delslice__()" is found, or for extended slicing of the form "self[i:j:k]", a slice object is created, and passed to "__delitem__()", instead of "__delslice__()" being called. Notice that these methods are only invoked when a single slice with a single colon is used, and the slice method is available. For slice operations involving extended slice notation, or in absence of the slice methods, "__getitem__()", "__setitem__()" or "__delitem__()" is called with a slice object as argument. The following example demonstrate how to make your program or module compatible with earlier versions of Python (assuming that methods "__getitem__()", "__setitem__()" and "__delitem__()" support slice objects as arguments): class MyClass: ... def __getitem__(self, index): ... def __setitem__(self, index, value): ... def __delitem__(self, index): ... if sys.version_info < (2, 0): # They won't be defined if version is at least 2.0 final def __getslice__(self, i, j): return self[max(0, i):max(0, j):] def __setslice__(self, i, j, seq): self[max(0, i):max(0, j):] = seq def __delslice__(self, i, j): del self[max(0, i):max(0, j):] ... Note the calls to "max()"; these are necessary because of the handling of negative indices before the "__*slice__()" methods are called. When negative indexes are used, the "__*item__()" methods receive them as provided, but the "__*slice__()" methods get a "cooked" form of the index values. For each negative index value, the length of the sequence is added to the index before calling the method (which may still result in a negative index); this is the customary handling of negative indexes by the built-in sequence types, and the "__*item__()" methods are expected to do this as well. However, since they should already be doing that, negative indexes cannot be passed in; they must be constrained to the bounds of the sequence before being passed to the "__*item__()" methods. Calling "max(0, i)" conveniently returns the proper value. Emulating numeric types ======================= The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. object.__add__(self, other) object.__sub__(self, other) object.__mul__(self, other) object.__floordiv__(self, other) object.__mod__(self, other) object.__divmod__(self, other) object.__pow__(self, other[, modulo]) object.__lshift__(self, other) object.__rshift__(self, other) object.__and__(self, other) object.__xor__(self, other) object.__or__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "//", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, to evaluate the expression "x + y", where *x* is an instance of a class that has an "__add__()" method, "x.__add__(y)" is called. The "__divmod__()" method should be the equivalent to using "__floordiv__()" and "__mod__()"; it should not be related to "__truediv__()" (described below). Note that "__pow__()" should be defined to accept an optional third argument if the ternary version of the built-in "pow()" function is to be supported. If one of those methods does not support the operation with the supplied arguments, it should return "NotImplemented". object.__div__(self, other) object.__truediv__(self, other) The division operator ("/") is implemented by these methods. The "__truediv__()" method is used when "__future__.division" is in effect, otherwise "__div__()" is used. If only one of these two methods is defined, the object will not support division in the alternate context; "TypeError" will be raised instead. object.__radd__(self, other) object.__rsub__(self, other) object.__rmul__(self, other) object.__rdiv__(self, other) object.__rtruediv__(self, other) object.__rfloordiv__(self, other) object.__rmod__(self, other) object.__rdivmod__(self, other) object.__rpow__(self, other) object.__rlshift__(self, other) object.__rrshift__(self, other) object.__rand__(self, other) object.__rxor__(self, other) object.__ror__(self, other) These methods are called to implement the binary arithmetic operations ("+", "-", "*", "/", "%", "divmod()", "pow()", "**", "<<", ">>", "&", "^", "|") with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types. [2] For instance, to evaluate the expression "x - y", where *y* is an instance of a class that has an "__rsub__()" method, "y.__rsub__(x)" is called if "x.__sub__(y)" returns *NotImplemented*. Note that ternary "pow()" will not try calling "__rpow__()" (the coercion rules would become too complicated). Note: If the right operand's type is a subclass of the left operand's type and that subclass provides the reflected method for the operation, this method will be called before the left operand's non-reflected method. This behavior allows subclasses to override their ancestors' operations. object.__iadd__(self, other) object.__isub__(self, other) object.__imul__(self, other) object.__idiv__(self, other) object.__itruediv__(self, other) object.__ifloordiv__(self, other) object.__imod__(self, other) object.__ipow__(self, other[, modulo]) object.__ilshift__(self, other) object.__irshift__(self, other) object.__iand__(self, other) object.__ixor__(self, other) object.__ior__(self, other) These methods are called to implement the augmented arithmetic assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", "<<=", ">>=", "&=", "^=", "|="). These methods should attempt to do the operation in-place (modifying *self*) and return the result (which could be, but does not have to be, *self*). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, to execute the statement "x += y", where *x* is an instance of a class that has an "__iadd__()" method, "x.__iadd__(y)" is called. If *x* is an instance of a class that does not define a "__iadd__()" method, "x.__add__(y)" and "y.__radd__(x)" are considered, as with the evaluation of "x + y". object.__neg__(self) object.__pos__(self) object.__abs__(self) object.__invert__(self) Called to implement the unary arithmetic operations ("-", "+", "abs()" and "~"). object.__complex__(self) object.__int__(self) object.__long__(self) object.__float__(self) Called to implement the built-in functions "complex()", "int()", "long()", and "float()". Should return a value of the appropriate type. object.__oct__(self) object.__hex__(self) Called to implement the built-in functions "oct()" and "hex()". Should return a string value. object.__index__(self) Called to implement "operator.index()". Also called whenever Python needs an integer object (such as in slicing). Must return an integer (int or long). New in version 2.5. object.__coerce__(self, other) Called to implement "mixed-mode" numeric arithmetic. Should either return a 2-tuple containing *self* and *other* converted to a common numeric type, or "None" if conversion is impossible. When the common type would be the type of "other", it is sufficient to return "None", since the interpreter will also ask the other object to attempt a coercion (but sometimes, if the implementation of the other type cannot be changed, it is useful to do the conversion to the other type here). A return value of "NotImplemented" is equivalent to returning "None". Coercion rules ============== This section used to document the rules for coercion. As the language has evolved, the coercion rules have become hard to document precisely; documenting what one version of one particular implementation does is undesirable. Instead, here are some informal guidelines regarding coercion. In Python 3, coercion will not be supported. * If the left operand of a % operator is a string or Unicode object, no coercion takes place and the string formatting operation is invoked instead. * It is no longer recommended to define a coercion operation. Mixed- mode operations on types that don't define coercion pass the original arguments to the operation. * New-style classes (those derived from "object") never invoke the "__coerce__()" method in response to a binary operator; the only time "__coerce__()" is invoked is when the built-in function "coerce()" is called. * For most intents and purposes, an operator that returns "NotImplemented" is treated the same as one that is not implemented at all. * Below, "__op__()" and "__rop__()" are used to signify the generic method names corresponding to an operator; "__iop__()" is used for the corresponding in-place operator. For example, for the operator '"+"', "__add__()" and "__radd__()" are used for the left and right variant of the binary operator, and "__iadd__()" for the in-place variant. * For objects *x* and *y*, first "x.__op__(y)" is tried. If this is not implemented or returns "NotImplemented", "y.__rop__(x)" is tried. If this is also not implemented or returns "NotImplemented", a "TypeError" exception is raised. But see the following exception: * Exception to the previous item: if the left operand is an instance of a built-in type or a new-style class, and the right operand is an instance of a proper subclass of that type or class and overrides the base's "__rop__()" method, the right operand's "__rop__()" method is tried *before* the left operand's "__op__()" method. This is done so that a subclass can completely override binary operators. Otherwise, the left operand's "__op__()" method would always accept the right operand: when an instance of a given class is expected, an instance of a subclass of that class is always acceptable. * When either operand type defines a coercion, this coercion is called before that type's "__op__()" or "__rop__()" method is called, but no sooner. If the coercion returns an object of a different type for the operand whose coercion is invoked, part of the process is redone using the new object. * When an in-place operator (like '"+="') is used, if the left operand implements "__iop__()", it is invoked without any coercion. When the operation falls back to "__op__()" and/or "__rop__()", the normal coercion rules apply. * In "x + y", if *x* is a sequence that implements sequence concatenation, sequence concatenation is invoked. * In "x * y", if one operand is a sequence that implements sequence repetition, and the other is an integer ("int" or "long"), sequence repetition is invoked. * Rich comparisons (implemented by methods "__eq__()" and so on) never use coercion. Three-way comparison (implemented by "__cmp__()") does use coercion under the same conditions as other binary operations use it. * In the current implementation, the built-in numeric types "int", "long", "float", and "complex" do not use coercion. All these types implement a "__coerce__()" method, for use by the built-in "coerce()" function. Changed in version 2.7: The complex type no longer makes implicit calls to the "__coerce__()" method for mixed-type binary arithmetic operations. With Statement Context Managers =============================== New in version 2.5. A *context manager* is an object that defines the runtime context to be established when executing a "with" statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the "with" statement (described in section The with statement), but can also be used by directly invoking their methods. Typical uses of context managers include saving and restoring various kinds of global state, locking and unlocking resources, closing opened files, etc. For more information on context managers, see Context Manager Types. object.__enter__(self) Enter the runtime context related to this object. The "with" statement will bind this method's return value to the target(s) specified in the "as" clause of the statement, if any. object.__exit__(self, exc_type, exc_value, traceback) Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be "None". If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit from this method. Note that "__exit__()" methods should not reraise the passed-in exception; this is the caller's responsibility. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. Special method lookup for old-style classes =========================================== For old-style classes, special methods are always looked up in exactly the same way as any other method or attribute. This is the case regardless of whether the method is being looked up explicitly as in "x.__getitem__(i)" or implicitly as in "x[i]". This behaviour means that special methods may exhibit different behaviour for different instances of a single old-style class if the appropriate special attributes are set differently: >>> class C: ... pass ... >>> c1 = C() >>> c2 = C() >>> c1.__len__ = lambda: 5 >>> c2.__len__ = lambda: 9 >>> len(c1) 5 >>> len(c2) 9 Special method lookup for new-style classes =========================================== For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object's type, not in the object's instance dictionary. That behaviour is the reason why the following code raises an exception (unlike the equivalent example with old-style classes): >>> class C(object): ... pass ... >>> c = C() >>> c.__len__ = lambda: 5 >>> len(c) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'C' has no len() The rationale behind this behaviour lies with a number of special methods such as "__hash__()" and "__repr__()" that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself: >>> 1 .__hash__() == hash(1) True >>> int.__hash__() == hash(int) Traceback (most recent call last): File "", line 1, in TypeError: descriptor '__hash__' of 'int' object needs an argument Incorrectly attempting to invoke an unbound method of a class in this way is sometimes referred to as 'metaclass confusion', and is avoided by bypassing the instance when looking up special methods: >>> type(1).__hash__(1) == hash(1) True >>> type(int).__hash__(int) == hash(int) True In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the "__getattribute__()" method even of the object's metaclass: >>> class Meta(type): ... def __getattribute__(*args): ... print "Metaclass getattribute invoked" ... return type.__getattribute__(*args) ... >>> class C(object): ... __metaclass__ = Meta ... def __len__(self): ... return 10 ... def __getattribute__(*args): ... print "Class getattribute invoked" ... return object.__getattribute__(*args) ... >>> c = C() >>> c.__len__() # Explicit lookup via instance Class getattribute invoked 10 >>> type(c).__len__(c) # Explicit lookup via type Metaclass getattribute invoked 10 >>> len(c) # Implicit lookup 10 Bypassing the "__getattribute__()" machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method *must* be set on the class object itself in order to be consistently invoked by the interpreter). -[ Footnotes ]- [1] It *is* possible in some cases to change an object's type, under certain controlled conditions. It generally isn't a good idea though, since it can lead to some very strange behaviour if it is handled incorrectly. [2] For operands of the same type, it is assumed that if the non- reflected method (such as "__add__()") fails the operation is not supported, which is why the reflected method is not called. t specialnamessK String Methods ************** Below are listed the string methods which both 8-bit strings and Unicode objects support. Some of them are also available on "bytearray" objects. In addition, Python's strings support the sequence type methods described in the Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange section. To output formatted strings use template strings or the "%" operator described in the String Formatting Operations section. Also, see the "re" module for string functions based on regular expressions. str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. For 8-bit strings, this method is locale-dependent. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). Changed in version 2.4: Support for the *fillchar* argument. str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.decode([encoding[, errors]]) Decodes the string using the codec registered for *encoding*. *encoding* defaults to the default string encoding. *errors* may be given to set a different error handling scheme. The default is "'strict'", meaning that encoding errors raise "UnicodeError". Other possible values are "'ignore'", "'replace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. New in version 2.2. Changed in version 2.3: Support for other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.encode([encoding[, errors]]) Return an encoded version of the string. Default encoding is the current default string encoding. *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings. New in version 2.0. Changed in version 2.3: Support for "'xmlcharrefreplace'" and "'backslashreplace'" and other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. Changed in version 2.5: Accept tuples as *suffix*. str.expandtabs([tabsize]) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. This method of string formatting is the new standard in Python 3, and should be preferred to the "%" formatting described in String Formatting Operations in new code. New in version 2.6. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. For 8-bit strings, this method is locale-dependent. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. For 8-bit strings, this method is locale-dependent. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' Changed in version 2.2.2: Support for the *chars* argument. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. New in version 2.5. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. New in version 2.5. str.rsplit([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. New in version 2.4. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' Changed in version 2.2.2: Support for the *chars* argument. str.split([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example, "' 1 2 3 '.split()" returns "['1', '2', '3']", and "' 1 2 3 '.split(None, 1)" returns "['1', '2 3 ']". str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. This method uses the *universal newlines* approach to splitting lines. Line breaks are not included in the resulting list unless *keepends* is given and true. Python recognizes ""\r"", ""\n"", and ""\r\n"" as line boundaries for 8-bit strings. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] unicode.splitlines([keepends]) Return a list of the lines in the string, like "str.splitlines()". However, the Unicode method splits on the following line boundaries, which are a superset of the *universal newlines* recognized for 8-bit strings. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 2.7: "\v" and "\f" added to list of line boundaries. str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. Changed in version 2.5: Accept tuples as *prefix*. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' Changed in version 2.2.2: Support for the *chars* argument. str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. For 8-bit strings, this method is locale-dependent. str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." For 8-bit strings, this method is locale-dependent. str.translate(table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument *deletechars* are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. You can use the "maketrans()" helper function in the "string" module to create a translation table. For string objects, set the *table* argument to "None" for translations that only delete characters: >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' New in version 2.6: Support for a "None" *table* argument. For Unicode objects, the "translate()" method does not accept the optional *deletechars* argument. Instead, it returns a copy of the *s* where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or "None". Unmapped characters are left untouched. Characters mapped to "None" are deleted. Note, a more flexible approach is to create a custom character mapping codec using the "codecs" module (see "encodings.cp1251" for an example). str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "str.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase). For 8-bit strings, this method is locale-dependent. str.zfill(width) Return the numeric string left filled with zeros in a string of length *width*. A sign prefix is handled correctly. The original string is returned if *width* is less than or equal to "len(s)". New in version 2.2.2. The following methods are present only on unicode objects: unicode.isnumeric() Return "True" if there are only numeric characters in S, "False" otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. unicode.isdecimal() Return "True" if there are only decimal characters in S, "False" otherwise. Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. sstring-methodssF String literals *************** String literals are described by the following lexical definitions: stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR" | "b" | "B" | "br" | "Br" | "bR" | "BR" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | escapeseq longstringitem ::= longstringchar | escapeseq shortstringchar ::= longstringchar ::= escapeseq ::= "\" One syntactic restriction not indicated by these productions is that whitespace is not allowed between the "stringprefix" and the rest of the string literal. The source character set is defined by the encoding declaration; it is ASCII if no encoding declaration is given in the source file; see section Encoding declarations. In plain English: String literals can be enclosed in matching single quotes ("'") or double quotes ("""). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as *triple-quoted strings*). The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter "'r'" or "'R'"; such strings are called *raw strings* and use different rules for interpreting backslash escape sequences. A prefix of "'u'" or "'U'" makes the string a Unicode string. Unicode strings use the Unicode character set as defined by the Unicode Consortium and ISO 10646. Some additional escape sequences, described below, are available in Unicode strings. A prefix of "'b'" or "'B'" is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A "'u'" or "'b'" prefix may be followed by an "'r'" prefix. In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. (A "quote" is the character used to open the string, i.e. either "'" or """.) Unless an "'r'" or "'R'" prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are: +-------------------+-----------------------------------+---------+ | Escape Sequence | Meaning | Notes | +===================+===================================+=========+ | "\newline" | Ignored | | +-------------------+-----------------------------------+---------+ | "\\" | Backslash ("\") | | +-------------------+-----------------------------------+---------+ | "\'" | Single quote ("'") | | +-------------------+-----------------------------------+---------+ | "\"" | Double quote (""") | | +-------------------+-----------------------------------+---------+ | "\a" | ASCII Bell (BEL) | | +-------------------+-----------------------------------+---------+ | "\b" | ASCII Backspace (BS) | | +-------------------+-----------------------------------+---------+ | "\f" | ASCII Formfeed (FF) | | +-------------------+-----------------------------------+---------+ | "\n" | ASCII Linefeed (LF) | | +-------------------+-----------------------------------+---------+ | "\N{name}" | Character named *name* in the | | | | Unicode database (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\r" | ASCII Carriage Return (CR) | | +-------------------+-----------------------------------+---------+ | "\t" | ASCII Horizontal Tab (TAB) | | +-------------------+-----------------------------------+---------+ | "\uxxxx" | Character with 16-bit hex value | (1) | | | *xxxx* (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\Uxxxxxxxx" | Character with 32-bit hex value | (2) | | | *xxxxxxxx* (Unicode only) | | +-------------------+-----------------------------------+---------+ | "\v" | ASCII Vertical Tab (VT) | | +-------------------+-----------------------------------+---------+ | "\ooo" | Character with octal value *ooo* | (3,5) | +-------------------+-----------------------------------+---------+ | "\xhh" | Character with hex value *hh* | (4,5) | +-------------------+-----------------------------------+---------+ Notes: 1. Individual code units which form parts of a surrogate pair can be encoded using this escape sequence. 2. Any Unicode character can be encoded this way, but characters outside the Basic Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is compiled to use 16-bit code units (the default). 3. As in Standard C, up to three octal digits are accepted. 4. Unlike in Standard C, exactly two hex digits are required. 5. In a string literal, hexadecimal and octal escapes denote the byte with the given value; it is not necessary that the byte encodes a character in the source character set. In a Unicode literal, these escapes denote a Unicode character with the given value. Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., *the backslash is left in the string*. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences marked as "(Unicode only)" in the table above fall into the category of unrecognized escapes for non-Unicode string literals. When an "'r'" or "'R'" prefix is present, a character following a backslash is included in the string without change, and *all backslashes are left in the string*. For example, the string literal "r"\n"" consists of two characters: a backslash and a lowercase "'n'". String quotes can be escaped with a backslash, but the backslash remains in the string; for example, "r"\""" is a valid string literal consisting of two characters: a backslash and a double quote; "r"\"" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw string cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, *not* as a line continuation. When an "'r'" or "'R'" prefix is used in conjunction with a "'u'" or "'U'" prefix, then the "\uXXXX" and "\UXXXXXXXX" escape sequences are processed while *all other backslashes are left in the string*. For example, the string literal "ur"\u0062\n"" consists of three Unicode characters: 'LATIN SMALL LETTER B', 'REVERSE SOLIDUS', and 'LATIN SMALL LETTER N'. Backslashes can be escaped with a preceding backslash; however, both remain in the string. As a result, "\uXXXX" escape sequences are only recognized when there are an odd number of backslashes. tstringss Subscriptions ************* A subscription selects an item of a sequence (string, tuple or list) or mapping (dictionary) object: subscription ::= primary "[" expression_list "]" The primary must evaluate to an object of a sequence or mapping type. If the primary is a mapping, the expression list must evaluate to an object whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) If the primary is a sequence, the expression (list) must evaluate to a plain integer. If this value is negative, the length of the sequence is added to it (so that, e.g., "x[-1]" selects the last item of "x".) The resulting value must be a nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is that value (counting from zero). A string's items are characters. A character is not a separate data type but a string of exactly one character. t subscriptionss Truth Value Testing ******************* Any object can be tested for truth value, for use in an "if" or "while" condition or as operand of the Boolean operations below. The following values are considered false: * "None" * "False" * zero of any numeric type, for example, "0", "0L", "0.0", "0j". * any empty sequence, for example, "''", "()", "[]". * any empty mapping, for example, "{}". * instances of user-defined classes, if the class defines a "__nonzero__()" or "__len__()" method, when that method returns the integer zero or "bool" value "False". [1] All other values are considered true --- so objects of many types are always true. Operations and built-in functions that have a Boolean result always return "0" or "False" for false and "1" or "True" for true, unless otherwise stated. (Important exception: the Boolean operations "or" and "and" always return one of their operands.) ttruths The "try" statement ******************* The "try" statement specifies exception handlers and/or cleanup code for a group of statements: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression [("as" | ",") identifier]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite Changed in version 2.5: In previous versions of Python, "try"..."except"..."finally" did not work. "try"..."except" had to be nested in "try"..."finally". The "except" clause(s) specify one or more exception handlers. When no exception occurs in the "try" clause, no exception handler is executed. When an exception occurs in the "try" suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression- less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is "compatible" with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception. If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. [1] If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire "try" statement raised the exception). When a matching except clause is found, the exception is assigned to the target specified in that except clause, if present, and the except clause's suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) Before an except clause's suite is executed, details about the exception are assigned to three variables in the "sys" module: "sys.exc_type" receives the object identifying the exception; "sys.exc_value" receives the exception's parameter; "sys.exc_traceback" receives a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred. These details are also available through the "sys.exc_info()" function, which returns a tuple "(exc_type, exc_value, exc_traceback)". Use of the corresponding variables is deprecated in favor of this function, since their use is unsafe in a threaded program. As of Python 1.5, the variables are restored to their previous values (before the call) when returning from a function that handled an exception. The optional "else" clause is executed if and when control flows off the end of the "try" clause. [2] Exceptions in the "else" clause are not handled by the preceding "except" clauses. If "finally" is present, it specifies a 'cleanup' handler. The "try" clause is executed, including any "except" and "else" clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The "finally" clause is executed. If there is a saved exception, it is re-raised at the end of the "finally" clause. If the "finally" clause raises another exception or executes a "return" or "break" statement, the saved exception is discarded: >>> def f(): ... try: ... 1/0 ... finally: ... return 42 ... >>> f() 42 The exception information is not available to the program during execution of the "finally" clause. When a "return", "break" or "continue" statement is executed in the "try" suite of a "try"..."finally" statement, the "finally" clause is also executed 'on the way out.' A "continue" statement is illegal in the "finally" clause. (The reason is a problem with the current implementation --- this restriction may be lifted in the future). The return value of a function is determined by the last "return" statement executed. Since the "finally" clause always executes, a "return" statement executed in the "finally" clause will always be the last one executed: >>> def foo(): ... try: ... return 'try' ... finally: ... return 'finally' ... >>> foo() 'finally' Additional information on exceptions can be found in section Exceptions, and information on using the "raise" statement to generate exceptions may be found in section The raise statement. ttrys The standard type hierarchy *************************** Below is a list of the types that are built into Python. Extension modules (written in C, Java, or other languages, depending on the implementation) can define additional types. Future versions of Python may add types to the type hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.). Some of the type descriptions below contain a paragraph listing 'special attributes.' These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future. None This type has a single value. There is a single object with this value. This object is accessed through the built-in name "None". It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don't explicitly return anything. Its truth value is false. NotImplemented This type has a single value. There is a single object with this value. This object is accessed through the built-in name "NotImplemented". Numeric methods and rich comparison methods may return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true. Ellipsis This type has a single value. There is a single object with this value. This object is accessed through the built-in name "Ellipsis". It is used to indicate the presence of the "..." syntax in a slice. Its truth value is true. "numbers.Number" These are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. Numeric objects are immutable; once created their value never changes. Python numbers are of course strongly related to mathematical numbers, but subject to the limitations of numerical representation in computers. Python distinguishes between integers, floating point numbers, and complex numbers: "numbers.Integral" These represent elements from the mathematical set of integers (positive and negative). There are three types of integers: Plain integers These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.) When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception "OverflowError" is raised instead). For the purpose of shift and mask operations, integers are assumed to have a binary, 2's complement notation using 32 or more bits, and hiding no bits from the user (i.e., all 4294967296 different bit patterns correspond to different values). Long integers These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2's complement which gives the illusion of an infinite string of sign bits extending to the left. Booleans These represent the truth values False and True. The two objects representing the values "False" and "True" are the only Boolean objects. The Boolean type is a subtype of plain integers, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings ""False"" or ""True"" are returned, respectively. The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers and the least surprises when switching between the plain and long integer domains. Any operation, if it yields a result in the plain integer domain, will yield the same result in the long integer domain or when using mixed operands. The switch between domains is transparent to the programmer. "numbers.Real" ("float") These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single- precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers. "numbers.Complex" These represent complex numbers as a pair of machine-level double precision floating point numbers. The same caveats apply as for floating point numbers. The real and imaginary parts of a complex number "z" can be retrieved through the read-only attributes "z.real" and "z.imag". Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function "len()" returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* is selected by "a[i]". Sequences also support slicing: "a[i:j]" selects all items with index *k* such that *i* "<=" *k* "<" *j*. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0. Some sequences also support "extended slicing" with a third "step" parameter: "a[i:j:k]" selects all items of *a* with index *x* where "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*. Sequences are distinguished according to their mutability: Immutable sequences An object of an immutable sequence type cannot change once it is created. (If the object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change.) The following types are immutable sequences: Strings The items of a string are characters. There is no separate character type; a character is represented by a string of one item. Characters represent (at least) 8-bit bytes. The built-in functions "chr()" and "ord()" convert between characters and nonnegative integers representing the byte values. Bytes with the values 0--127 usually represent the corresponding ASCII values, but the interpretation of values is up to the program. The string data type is also used to represent arrays of bytes, e.g., to hold data read from a file. (On systems whose native character set is not ASCII, strings may use EBCDIC in their internal representation, provided the functions "chr()" and "ord()" implement a mapping between ASCII and EBCDIC, and string comparison preserves the ASCII order. Or perhaps someone can propose a better rule?) Unicode The items of a Unicode object are Unicode code units. A Unicode code unit is represented by a Unicode object of one item and can hold either a 16-bit or 32-bit value representing a Unicode ordinal (the maximum value for the ordinal is given in "sys.maxunicode", and depends on how Python is configured at compile time). Surrogate pairs may be present in the Unicode object, and will be reported as two separate items. The built-in functions "unichr()" and "ord()" convert between code units and nonnegative integers representing the Unicode ordinals as defined in the Unicode Standard 3.0. Conversion from and to other encodings are possible through the Unicode method "encode()" and the built- in function "unicode()". Tuples The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a 'singleton') can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses. Mutable sequences Mutable sequences can be changed after they are created. The subscription and slicing notations can be used as the target of assignment and "del" (delete) statements. There are currently two intrinsic mutable sequence types: Lists The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.) Byte Arrays A bytearray object is a mutable array. They are created by the built-in "bytearray()" constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface and functionality as immutable bytes objects. The extension module "array" provides an additional example of a mutable sequence type. Set types These represent unordered, finite sets of unique, immutable objects. As such, they cannot be indexed by any subscript. However, they can be iterated over, and the built-in function "len()" returns the number of items in a set. Common uses for sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. For set elements, the same immutability rules apply as for dictionary keys. Note that numeric types obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0"), only one of them can be contained in a set. There are currently two intrinsic set types: Sets These represent a mutable set. They are created by the built-in "set()" constructor and can be modified afterwards by several methods, such as "add()". Frozen sets These represent an immutable set. They are created by the built-in "frozenset()" constructor. As a frozenset is immutable and *hashable*, it can be used again as an element of another set, or as a dictionary key. Mappings These represent finite sets of objects indexed by arbitrary index sets. The subscript notation "a[k]" selects the item indexed by "k" from the mapping "a"; this can be used in expressions and as the target of assignments or "del" statements. The built-in function "len()" returns the number of items in a mapping. There is currently a single intrinsic mapping type: Dictionaries These represent finite sets of objects indexed by nearly arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key's hash value to remain constant. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. Dictionaries are mutable; they can be created by the "{...}" notation (see section Dictionary displays). The extension modules "dbm", "gdbm", and "bsddb" provide additional examples of mapping types. Callable types These are the types to which the function call operation (see section Calls) can be applied: User-defined functions A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function's formal parameter list. Special attributes: +-------------------------+---------------------------------+-------------+ | Attribute | Meaning | | +=========================+=================================+=============+ | "__doc__" "func_doc" | The function's documentation | Writable | | | string, or "None" if | | | | unavailable. | | +-------------------------+---------------------------------+-------------+ | "__name__" "func_name" | The function's name | Writable | +-------------------------+---------------------------------+-------------+ | "__module__" | The name of the module the | Writable | | | function was defined in, or | | | | "None" if unavailable. | | +-------------------------+---------------------------------+-------------+ | "__defaults__" | A tuple containing default | Writable | | "func_defaults" | argument values for those | | | | arguments that have defaults, | | | | or "None" if no arguments have | | | | a default value. | | +-------------------------+---------------------------------+-------------+ | "__code__" "func_code" | The code object representing | Writable | | | the compiled function body. | | +-------------------------+---------------------------------+-------------+ | "__globals__" | A reference to the dictionary | Read-only | | "func_globals" | that holds the function's | | | | global variables --- the global | | | | namespace of the module in | | | | which the function was defined. | | +-------------------------+---------------------------------+-------------+ | "__dict__" "func_dict" | The namespace supporting | Writable | | | arbitrary function attributes. | | +-------------------------+---------------------------------+-------------+ | "__closure__" | "None" or a tuple of cells that | Read-only | | "func_closure" | contain bindings for the | | | | function's free variables. | | +-------------------------+---------------------------------+-------------+ Most of the attributes labelled "Writable" check the type of the assigned value. Changed in version 2.4: "func_name" is now writable. Changed in version 2.6: The double-underscore attributes "__closure__", "__code__", "__defaults__", and "__globals__" were introduced as aliases for the corresponding "func_*" attributes for forwards compatibility with Python 3. Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. *Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.* Additional information about a function's definition can be retrieved from its code object; see the description of internal types below. User-defined methods A user-defined method object combines a class, a class instance (or "None") and any callable object (normally a user-defined function). Special read-only attributes: "im_self" is the class instance object, "im_func" is the function object; "im_class" is the class of "im_self" for bound methods or the class that asked for the method for unbound methods; "__doc__" is the method's documentation (same as "im_func.__doc__"); "__name__" is the method name (same as "im_func.__name__"); "__module__" is the name of the module the method was defined in, or "None" if unavailable. Changed in version 2.2: "im_self" used to refer to the class that defined the method. Changed in version 2.6: For Python 3 forward-compatibility, "im_func" is also available as "__func__", and "im_self" as "__self__". Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object. User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object, an unbound user-defined method object, or a class method object. When the attribute is a user-defined method object, a new method object is only created if the class from which it is being retrieved is the same as, or a derived class of, the class stored in the original method object; otherwise, the original method object is used as it is. When a user-defined method object is created by retrieving a user-defined function object from a class, its "im_self" attribute is "None" and the method object is said to be unbound. When one is created by retrieving a user-defined function object from a class via one of its instances, its "im_self" attribute is the instance, and the method object is said to be bound. In either case, the new method's "im_class" attribute is the class from which the retrieval takes place, and its "im_func" attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the "im_func" attribute of the new instance is not the original method object but its "im_func" attribute. When a user-defined method object is created by retrieving a class method object from a class or instance, its "im_self" attribute is the class itself, and its "im_func" attribute is the function object underlying the class method. When an unbound user-defined method object is called, the underlying function ("im_func") is called, with the restriction that the first argument must be an instance of the proper class ("im_class") or of a derived class thereof. When a bound user-defined method object is called, the underlying function ("im_func") is called, inserting the class instance ("im_self") in front of the argument list. For instance, when "C" is a class which contains a definition for a function "f()", and "x" is an instance of "C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)". When a user-defined method object is derived from a class method object, the "class instance" stored in "im_self" will actually be the class itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent to calling "f(C,1)" where "f" is the underlying function. Note that the transformation from function object to (unbound or bound) method object happens each time the attribute is retrieved from the class or instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this *only* happens when the function is an attribute of the class. Generator functions A function or method which uses the "yield" statement (see section The yield statement) is called a *generator function*. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator's "next()" method will cause the function to execute until it provides a value using the "yield" statement. When the function executes a "return" statement or falls off the end, a "StopIteration" exception is raised and the iterator will have reached the end of the set of values to be returned. Built-in functions A built-in function object is a wrapper around a C function. Examples of built-in functions are "len()" and "math.sin()" ("math" is a standard built-in module). The number and type of the arguments are determined by the C function. Special read- only attributes: "__doc__" is the function's documentation string, or "None" if unavailable; "__name__" is the function's name; "__self__" is set to "None" (but see the next item); "__module__" is the name of the module the function was defined in or "None" if unavailable. Built-in methods This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is "alist.append()", assuming *alist* is a list object. In this case, the special read-only attribute "__self__" is set to the object denoted by *alist*. Class Types Class types, or "new-style classes," are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override "__new__()". The arguments of the call are passed to "__new__()" and, in the typical case, to "__init__()" to initialize the new instance. Classic Classes Class objects are described below. When a class object is called, a new class instance (also described below) is created and returned. This implies a call to the class's "__init__()" method if it has one. Any arguments are passed on to the "__init__()" method. If there is no "__init__()" method, the class must be called without arguments. Class instances Class instances are described below. Class instances are callable only when the class has a "__call__()" method; "x(arguments)" is a shorthand for "x.__call__(arguments)". Modules Modules are imported by the "import" statement (see section The import statement). A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the func_globals attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object does not contain the code object used to initialize the module (since it isn't needed once the initialization is done). Attribute assignment updates the module's namespace dictionary, e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1". Special read-only attribute: "__dict__" is the module's namespace as a dictionary object. **CPython implementation detail:** Because of the way CPython clears module dictionaries, the module dictionary will be cleared when the module falls out of scope even if the dictionary still has live references. To avoid this, copy the dictionary or keep the module around while using its dictionary directly. Predefined (writable) attributes: "__name__" is the module's name; "__doc__" is the module's documentation string, or "None" if unavailable; "__file__" is the pathname of the file from which the module was loaded, if it was loaded from a file. The "__file__" attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file. Classes Both class types (new-style classes) and class objects (old- style/classic classes) are typically created by class definitions (see section Class definitions). A class has a namespace implemented by a dictionary object. Class attribute references are translated to lookups in this dictionary, e.g., "C.x" is translated to "C.__dict__["x"]" (although for new-style classes in particular there are a number of hooks which allow for other means of locating attributes). When the attribute name is not found there, the attribute search continues in the base classes. For old-style classes, the search is depth-first, left-to-right in the order of occurrence in the base class list. New-style classes use the more complex C3 method resolution order which behaves correctly even in the presence of 'diamond' inheritance structures where there are multiple inheritance paths leading back to a common ancestor. Additional details on the C3 MRO used by new-style classes can be found in the documentation accompanying the 2.3 release at https://www.python.org/download/releases/2.3/mro/. When a class attribute reference (for class "C", say) would yield a user-defined function object or an unbound user-defined method object whose associated class is either "C" or one of its base classes, it is transformed into an unbound user-defined method object whose "im_class" attribute is "C". When it would yield a class method object, it is transformed into a bound user-defined method object whose "im_self" attribute is "C". When it would yield a static method object, it is transformed into the object wrapped by the static method object. See section Implementing Descriptors for another way in which attributes retrieved from a class may differ from those actually contained in its "__dict__" (note that only new-style classes support descriptors). Class attribute assignments update the class's dictionary, never the dictionary of a base class. A class object can be called (see above) to yield a class instance (see below). Special attributes: "__name__" is the class name; "__module__" is the module name in which the class was defined; "__dict__" is the dictionary containing the class's namespace; "__bases__" is a tuple (possibly empty or a singleton) containing the base classes, in the order of their occurrence in the base class list; "__doc__" is the class's documentation string, or "None" if undefined. Class instances A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance's class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object or an unbound user-defined method object whose associated class is the class (call it "C") of the instance for which the attribute reference was initiated or one of its bases, it is transformed into a bound user-defined method object whose "im_class" attribute is "C" and whose "im_self" attribute is the instance. Static method and class method objects are also transformed, as if they had been retrieved from class "C"; see above under "Classes". See section Implementing Descriptors for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class's "__dict__". If no class attribute is found, and the object's class has a "__getattr__()" method, that is called to satisfy the lookup. Attribute assignments and deletions update the instance's dictionary, never a class's dictionary. If the class has a "__setattr__()" or "__delattr__()" method, this is called instead of updating the instance dictionary directly. Class instances can pretend to be numbers, sequences, or mappings if they have methods with certain special names. See section Special method names. Special attributes: "__dict__" is the attribute dictionary; "__class__" is the instance's class. Files A file object represents an open file. File objects are created by the "open()" built-in function, and also by "os.popen()", "os.fdopen()", and the "makefile()" method of socket objects (and perhaps by other functions or methods provided by extension modules). The objects "sys.stdin", "sys.stdout" and "sys.stderr" are initialized to file objects corresponding to the interpreter's standard input, output and error streams. See File Objects for complete documentation of file objects. Internal types A few types used internally by the interpreter are exposed to the user. Their definitions may change with future versions of the interpreter, but they are mentioned here for completeness. Code objects Code objects represent *byte-compiled* executable Python code, or *bytecode*. The difference between a code object and a function object is that the function object contains an explicit reference to the function's globals (the module in which it was defined), while a code object contains no context; also the default argument values are stored in the function object, not in the code object (because they represent values calculated at run-time). Unlike function objects, code objects are immutable and contain no references (directly or indirectly) to mutable objects. Special read-only attributes: "co_name" gives the function name; "co_argcount" is the number of positional arguments (including arguments with default values); "co_nlocals" is the number of local variables used by the function (including arguments); "co_varnames" is a tuple containing the names of the local variables (starting with the argument names); "co_cellvars" is a tuple containing the names of local variables that are referenced by nested functions; "co_freevars" is a tuple containing the names of free variables; "co_code" is a string representing the sequence of bytecode instructions; "co_consts" is a tuple containing the literals used by the bytecode; "co_names" is a tuple containing the names used by the bytecode; "co_filename" is the filename from which the code was compiled; "co_firstlineno" is the first line number of the function; "co_lnotab" is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); "co_stacksize" is the required stack size (including local variables); "co_flags" is an integer encoding a number of flags for the interpreter. The following flag bits are defined for "co_flags": bit "0x04" is set if the function uses the "*arguments" syntax to accept an arbitrary number of positional arguments; bit "0x08" is set if the function uses the "**keywords" syntax to accept arbitrary keyword arguments; bit "0x20" is set if the function is a generator. Future feature declarations ("from __future__ import division") also use bits in "co_flags" to indicate whether a code object was compiled with a particular feature enabled: bit "0x2000" is set if the function was compiled with future division enabled; bits "0x10" and "0x1000" were used in earlier versions of Python. Other bits in "co_flags" are reserved for internal use. If a code object represents a function, the first item in "co_consts" is the documentation string of the function, or "None" if undefined. Frame objects Frame objects represent execution frames. They may occur in traceback objects (see below). Special read-only attributes: "f_back" is to the previous stack frame (towards the caller), or "None" if this is the bottom stack frame; "f_code" is the code object being executed in this frame; "f_locals" is the dictionary used to look up local variables; "f_globals" is used for global variables; "f_builtins" is used for built-in (intrinsic) names; "f_restricted" is a flag indicating whether the function is executing in restricted execution mode; "f_lasti" gives the precise instruction (this is an index into the bytecode string of the code object). Special writable attributes: "f_trace", if not "None", is a function called at the start of each source code line (this is used by the debugger); "f_exc_type", "f_exc_value", "f_exc_traceback" represent the last exception raised in the parent frame provided another exception was ever raised in the current frame (in all other cases they are "None"); "f_lineno" is the current line number of the frame --- writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno. Traceback objects Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as "sys.exc_traceback", and also as the third item of the tuple returned by "sys.exc_info()". The latter is the preferred interface, since it works correctly when the program is using multiple threads. When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as "sys.last_traceback". Special read-only attributes: "tb_next" is the next level in the stack trace (towards the frame where the exception occurred), or "None" if there is no next level; "tb_frame" points to the execution frame of the current level; "tb_lineno" gives the line number where the exception occurred; "tb_lasti" indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a "try" statement with no matching except clause or with a finally clause. Slice objects Slice objects are used to represent slices when *extended slice syntax* is used. This is a slice using two colons, or multiple slices or ellipses separated by commas, e.g., "a[i:j:step]", "a[i:j, k:l]", or "a[..., i:j]". They are also created by the built-in "slice()" function. Special read-only attributes: "start" is the lower bound; "stop" is the upper bound; "step" is the step value; each is "None" if omitted. These attributes can have any type. Slice objects support one method: slice.indices(self, length) This method takes a single integer argument *length* and computes information about the extended slice that the slice object would describe if applied to a sequence of *length* items. It returns a tuple of three integers; respectively these are the *start* and *stop* indices and the *step* or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices. New in version 2.3. Static method objects Static method objects provide a way of defeating the transformation of function objects to method objects described above. A static method object is a wrapper around any other object, usually a user-defined method object. When a static method object is retrieved from a class or a class instance, the object actually returned is the wrapped object, which is not subject to any further transformation. Static method objects are not themselves callable, although the objects they wrap usually are. Static method objects are created by the built-in "staticmethod()" constructor. Class method objects A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under "User-defined methods". Class method objects are created by the built-in "classmethod()" constructor. ttypess Functions ********* Function objects are created by function definitions. The only operation on a function object is to call it: "func(argument-list)". There are really two flavors of function objects: built-in functions and user-defined functions. Both support the same operation (to call the function), but the implementation is different, hence the different object types. See Function definitions for more information. ttypesfunctionss/ Mapping Types --- "dict" ************************ A *mapping* object maps *hashable* values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the *dictionary*. (For other containers see the built in "list", "set", and "tuple" classes, and the "collections" module.) A dictionary's keys are *almost* arbitrary values. Values that are not *hashable*, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as "1" and "1.0") then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) Dictionaries can be created by placing a comma-separated list of "key: value" pairs within braces, for example: "{'jack': 4098, 'sjoerd': 4127}" or "{4098: 'jack', 4127: 'sjoerd'}", or by the "dict" constructor. class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an *iterable* object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary. If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument. To illustrate, the following examples all return a dictionary equal to "{"one": 1, "two": 2, "three": 3}": >>> a = dict(one=1, two=2, three=3) >>> b = {'one': 1, 'two': 2, 'three': 3} >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) >>> e = dict({'three': 3, 'one': 1, 'two': 2}) >>> a == b == c == d == e True Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used. New in version 2.2. Changed in version 2.3: Support for building a dictionary from keyword arguments added. These are the operations that dictionaries support (and therefore, custom mapping types should support too): len(d) Return the number of items in the dictionary *d*. d[key] Return the item of *d* with key *key*. Raises a "KeyError" if *key* is not in the map. If a subclass of dict defines a method "__missing__()" and *key* is not present, the "d[key]" operation calls that method with the key *key* as argument. The "d[key]" operation then returns or raises whatever is returned or raised by the "__missing__(key)" call. No other operations or methods invoke "__missing__()". If "__missing__()" is not defined, "KeyError" is raised. "__missing__()" must be a method; it cannot be an instance variable: >>> class Counter(dict): ... def __missing__(self, key): ... return 0 >>> c = Counter() >>> c['red'] 0 >>> c['red'] += 1 >>> c['red'] 1 The example above shows part of the implementation of "collections.Counter". A different "__missing__" method is used by "collections.defaultdict". New in version 2.5: Recognition of __missing__ methods of dict subclasses. d[key] = value Set "d[key]" to *value*. del d[key] Remove "d[key]" from *d*. Raises a "KeyError" if *key* is not in the map. key in d Return "True" if *d* has a key *key*, else "False". New in version 2.2. key not in d Equivalent to "not key in d". New in version 2.2. iter(d) Return an iterator over the keys of the dictionary. This is a shortcut for "iterkeys()". clear() Remove all items from the dictionary. copy() Return a shallow copy of the dictionary. fromkeys(seq[, value]) Create a new dictionary with keys from *seq* and values set to *value*. "fromkeys()" is a class method that returns a new dictionary. *value* defaults to "None". New in version 2.3. get(key[, default]) Return the value for *key* if *key* is in the dictionary, else *default*. If *default* is not given, it defaults to "None", so that this method never raises a "KeyError". has_key(key) Test for the presence of *key* in the dictionary. "has_key()" is deprecated in favor of "key in d". items() Return a copy of the dictionary's list of "(key, value)" pairs. **CPython implementation detail:** Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If "items()", "keys()", "values()", "iteritems()", "iterkeys()", and "itervalues()" are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". The same relationship holds for the "iterkeys()" and "itervalues()" methods: "pairs = zip(d.itervalues(), d.iterkeys())" provides the same value for "pairs". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.iteritems()]". iteritems() Return an iterator over the dictionary's "(key, value)" pairs. See the note for "dict.items()". Using "iteritems()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. iterkeys() Return an iterator over the dictionary's keys. See the note for "dict.items()". Using "iterkeys()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. itervalues() Return an iterator over the dictionary's values. See the note for "dict.items()". Using "itervalues()" while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. New in version 2.2. keys() Return a copy of the dictionary's list of keys. See the note for "dict.items()". pop(key[, default]) If *key* is in the dictionary, remove it and return its value, else return *default*. If *default* is not given and *key* is not in the dictionary, a "KeyError" is raised. New in version 2.3. popitem() Remove and return an arbitrary "(key, value)" pair from the dictionary. "popitem()" is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling "popitem()" raises a "KeyError". setdefault(key[, default]) If *key* is in the dictionary, return its value. If not, insert *key* with a value of *default* and return *default*. *default* defaults to "None". update([other]) Update the dictionary with the key/value pairs from *other*, overwriting existing keys. Return "None". "update()" accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: "d.update(red=1, blue=2)". Changed in version 2.4: Allowed the argument to be an iterable of key/value pairs and allowed keyword arguments. values() Return a copy of the dictionary's list of values. See the note for "dict.items()". viewitems() Return a new view of the dictionary's items ("(key, value)" pairs). See below for documentation of view objects. New in version 2.7. viewkeys() Return a new view of the dictionary's keys. See below for documentation of view objects. New in version 2.7. viewvalues() Return a new view of the dictionary's values. See below for documentation of view objects. New in version 2.7. Dictionaries compare equal if and only if they have the same "(key, value)" pairs. Dictionary view objects ======================= The objects returned by "dict.viewkeys()", "dict.viewvalues()" and "dict.viewitems()" are *view objects*. They provide a dynamic view on the dictionary's entries, which means that when the dictionary changes, the view reflects these changes. Dictionary views can be iterated over to yield their respective data, and support membership tests: len(dictview) Return the number of entries in the dictionary. iter(dictview) Return an iterator over the keys, values or items (represented as tuples of "(key, value)") in the dictionary. Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of "(value, key)" pairs using "zip()": "pairs = zip(d.values(), d.keys())". Another way to create the same list is "pairs = [(v, k) for (k, v) in d.items()]". Iterating views while adding or deleting entries in the dictionary may raise a "RuntimeError" or fail to iterate over all entries. x in dictview Return "True" if *x* is in the underlying dictionary's keys, values or items (in the latter case, *x* should be a "(key, value)" tuple). Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that (key, value) pairs are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries are generally not unique.) Then these set operations are available ("other" refers either to another view or a set): dictview & other Return the intersection of the dictview and the other object as a new set. dictview | other Return the union of the dictview and the other object as a new set. dictview - other Return the difference between the dictview and the other object (all elements in *dictview* that aren't in *other*) as a new set. dictview ^ other Return the symmetric difference (all elements either in *dictview* or *other*, but not in both) of the dictview and the other object as a new set. An example of dictionary view usage: >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.viewkeys() >>> values = dishes.viewvalues() >>> # iteration >>> n = 0 >>> for val in values: ... n += val >>> print(n) 504 >>> # keys and values are iterated over in the same order >>> list(keys) ['eggs', 'bacon', 'sausage', 'spam'] >>> list(values) [2, 1, 1, 500] >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> del dishes['sausage'] >>> list(keys) ['spam', 'bacon'] >>> # set operations >>> keys & {'eggs', 'bacon', 'salad'} {'bacon'} t typesmappingsz Methods ******* Methods are functions that are called using the attribute notation. There are two flavors: built-in methods (such as "append()" on lists) and class instance methods. Built-in methods are described with the types that support them. The implementation adds two special read-only attributes to class instance methods: "m.im_self" is the object on which the method operates, and "m.im_func" is the function implementing the method. Calling "m(arg-1, arg-2, ..., arg-n)" is completely equivalent to calling "m.im_func(m.im_self, arg-1, arg-2, ..., arg-n)". Class instance methods are either *bound* or *unbound*, referring to whether the method was accessed through an instance or a class, respectively. When a method is unbound, its "im_self" attribute will be "None" and if called, an explicit "self" object must be passed as the first argument. In this case, "self" must be an instance of the unbound method's class (or a subclass of that class), otherwise a "TypeError" is raised. Like function objects, methods objects support getting arbitrary attributes. However, since method attributes are actually stored on the underlying function object ("meth.im_func"), setting method attributes on either bound or unbound methods is disallowed. Attempting to set an attribute on a method results in an "AttributeError" being raised. In order to set a method attribute, you need to explicitly set it on the underlying function object: >>> class C: ... def method(self): ... pass ... >>> c = C() >>> c.method.whoami = 'my name is method' # can't set on the method Traceback (most recent call last): File "", line 1, in AttributeError: 'instancemethod' object has no attribute 'whoami' >>> c.method.im_func.whoami = 'my name is method' >>> c.method.whoami 'my name is method' See The standard type hierarchy for more information. t typesmethodss Modules ******* The only special operation on a module is attribute access: "m.name", where *m* is a module and *name* accesses a name defined in *m*'s symbol table. Module attributes can be assigned to. (Note that the "import" statement is not, strictly speaking, an operation on a module object; "import foo" does not require a module object named *foo* to exist, rather it requires an (external) *definition* for a module named *foo* somewhere.) A special attribute of every module is "__dict__". This is the dictionary containing the module's symbol table. Modifying this dictionary will actually change the module's symbol table, but direct assignment to the "__dict__" attribute is not possible (you can write "m.__dict__['a'] = 1", which defines "m.a" to be "1", but you can't write "m.__dict__ = {}"). Modifying "__dict__" directly is not recommended. Modules built into the interpreter are written like this: "". If loaded from a file, they are written as "". t typesmodulessy Sequence Types --- "str", "unicode", "list", "tuple", "bytearray", "buffer", "xrange" ************************************************************************************* There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects. For other containers see the built in "dict" and "set" classes, and the "collections" module. String literals are written in single or double quotes: "'xyzzy'", ""frobozz"". See String literals for more about string literals. Unicode strings are much like strings, but are specified in the syntax using a preceding "'u'" character: "u'abc'", "u"def"". In addition to the functionality described here, there are also string-specific methods described in the String Methods section. Lists are constructed with square brackets, separating items with commas: "[a, b, c]". Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as "a, b, c" or "()". A single item tuple must have a trailing comma, such as "(d,)". Bytearray objects are created with the built-in function "bytearray()". Buffer objects are not directly supported by Python syntax, but can be created by calling the built-in function "buffer()". They don't support concatenation or repetition. Objects of type xrange are similar to buffers in that there is no specific syntax to create them, but they are created using the "xrange()" function. They don't support slicing, concatenation or repetition, and using "in", "not in", "min()" or "max()" on them is inefficient. Most sequence types support the following operations. The "in" and "not in" operations have the same priorities as the comparison operations. The "+" and "*" operations have the same priority as the corresponding numeric operations. [3] Additional methods are provided for Mutable Sequence Types. This table lists the sequence operations sorted in ascending priority. In the table, *s* and *t* are sequences of the same type; *n*, *i* and *j* are integers: +--------------------+----------------------------------+------------+ | Operation | Result | Notes | +====================+==================================+============+ | "x in s" | "True" if an item of *s* is | (1) | | | equal to *x*, else "False" | | +--------------------+----------------------------------+------------+ | "x not in s" | "False" if an item of *s* is | (1) | | | equal to *x*, else "True" | | +--------------------+----------------------------------+------------+ | "s + t" | the concatenation of *s* and *t* | (6) | +--------------------+----------------------------------+------------+ | "s * n, n * s" | equivalent to adding *s* to | (2) | | | itself *n* times | | +--------------------+----------------------------------+------------+ | "s[i]" | *i*th item of *s*, origin 0 | (3) | +--------------------+----------------------------------+------------+ | "s[i:j]" | slice of *s* from *i* to *j* | (3)(4) | +--------------------+----------------------------------+------------+ | "s[i:j:k]" | slice of *s* from *i* to *j* | (3)(5) | | | with step *k* | | +--------------------+----------------------------------+------------+ | "len(s)" | length of *s* | | +--------------------+----------------------------------+------------+ | "min(s)" | smallest item of *s* | | +--------------------+----------------------------------+------------+ | "max(s)" | largest item of *s* | | +--------------------+----------------------------------+------------+ | "s.index(x)" | index of the first occurrence of | | | | *x* in *s* | | +--------------------+----------------------------------+------------+ | "s.count(x)" | total number of occurrences of | | | | *x* in *s* | | +--------------------+----------------------------------+------------+ Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.) Notes: 1. When *s* is a string or Unicode string object the "in" and "not in" operations act like a substring test. In Python versions before 2.3, *x* had to be a string of length 1. In Python 2.3 and beyond, *x* may be a string of any length. 2. Values of *n* less than "0" are treated as "0" (which yields an empty sequence of the same type as *s*). Note that items in the sequence *s* are not copied; they are referenced multiple times. This often haunts new Python programmers; consider: >>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]] What has happened is that "[[]]" is a one-element list containing an empty list, so all three elements of "[[]] * 3" are references to this single empty list. Modifying any of the elements of "lists" modifies this single list. You can create a list of different lists this way: >>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]] Further explanation is available in the FAQ entry How do I create a multidimensional list?. 3. If *i* or *j* is negative, the index is relative to the end of sequence *s*: "len(s) + i" or "len(s) + j" is substituted. But note that "-0" is still "0". 4. The slice of *s* from *i* to *j* is defined as the sequence of items with index *k* such that "i <= k < j". If *i* or *j* is greater than "len(s)", use "len(s)". If *i* is omitted or "None", use "0". If *j* is omitted or "None", use "len(s)". If *i* is greater than or equal to *j*, the slice is empty. 5. The slice of *s* from *i* to *j* with step *k* is defined as the sequence of items with index "x = i + n*k" such that "0 <= n < (j-i)/k". In other words, the indices are "i", "i+k", "i+2*k", "i+3*k" and so on, stopping when *j* is reached (but never including *j*). When *k* is positive, *i* and *j* are reduced to "len(s)" if they are greater. When *k* is negative, *i* and *j* are reduced to "len(s) - 1" if they are greater. If *i* or *j* are omitted or "None", they become "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero. If *k* is "None", it is treated like "1". 6. **CPython implementation detail:** If *s* and *t* are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form "s = s + t" or "s += t". When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use the "str.join()" method which assures consistent linear concatenation performance across versions and implementations. Changed in version 2.4: Formerly, string concatenation never occurred in-place. String Methods ============== Below are listed the string methods which both 8-bit strings and Unicode objects support. Some of them are also available on "bytearray" objects. In addition, Python's strings support the sequence type methods described in the Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange section. To output formatted strings use template strings or the "%" operator described in the String Formatting Operations section. Also, see the "re" module for string functions based on regular expressions. str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased. For 8-bit strings, this method is locale-dependent. str.center(width[, fillchar]) Return centered in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). Changed in version 2.4: Support for the *fillchar* argument. str.count(sub[, start[, end]]) Return the number of non-overlapping occurrences of substring *sub* in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. str.decode([encoding[, errors]]) Decodes the string using the codec registered for *encoding*. *encoding* defaults to the default string encoding. *errors* may be given to set a different error handling scheme. The default is "'strict'", meaning that encoding errors raise "UnicodeError". Other possible values are "'ignore'", "'replace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. New in version 2.2. Changed in version 2.3: Support for other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.encode([encoding[, errors]]) Return an encoded version of the string. Default encoding is the current default string encoding. *errors* may be given to set a different error handling scheme. The default for *errors* is "'strict'", meaning that encoding errors raise a "UnicodeError". Other possible values are "'ignore'", "'replace'", "'xmlcharrefreplace'", "'backslashreplace'" and any other name registered via "codecs.register_error()", see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings. New in version 2.0. Changed in version 2.3: Support for "'xmlcharrefreplace'" and "'backslashreplace'" and other error handling schemes added. Changed in version 2.7: Support for keyword arguments added. str.endswith(suffix[, start[, end]]) Return "True" if the string ends with the specified *suffix*, otherwise return "False". *suffix* can also be a tuple of suffixes to look for. With optional *start*, test beginning at that position. With optional *end*, stop comparing at that position. Changed in version 2.5: Accept tuples as *suffix*. str.expandtabs([tabsize]) Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. Tab positions occur every *tabsize* characters (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand the string, the current column is set to zero and the string is examined character by character. If the character is a tab ("\t"), one or more space characters are inserted in the result until the current column is equal to the next tab position. (The tab character itself is not copied.) If the character is a newline ("\n") or return ("\r"), it is copied and the current column is reset to zero. Any other character is copied unchanged and the current column is incremented by one regardless of how the character is represented when printed. >>> '01\t012\t0123\t01234'.expandtabs() '01 012 0123 01234' >>> '01\t012\t0123\t01234'.expandtabs(4) '01 012 0123 01234' str.find(sub[, start[, end]]) Return the lowest index in the string where substring *sub* is found within the slice "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" if *sub* is not found. Note: The "find()" method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the "in" operator: >>> 'Py' in 'Python' True str.format(*args, **kwargs) Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces "{}". Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. >>> "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' See Format String Syntax for a description of the various formatting options that can be specified in format strings. This method of string formatting is the new standard in Python 3, and should be preferred to the "%" formatting described in String Formatting Operations in new code. New in version 2.6. str.index(sub[, start[, end]]) Like "find()", but raise "ValueError" when the substring is not found. str.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.islower() Return true if all cased characters [4] in the string are lowercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. For 8-bit strings, this method is locale-dependent. str.istitle() Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. For 8-bit strings, this method is locale-dependent. str.isupper() Return true if all cased characters [4] in the string are uppercase and there is at least one cased character, false otherwise. For 8-bit strings, this method is locale-dependent. str.join(iterable) Return a string which is the concatenation of the strings in *iterable*. A "TypeError" will be raised if there are any non- string values in *iterable*, including "bytes" objects. The separator between elements is the string providing this method. str.ljust(width[, fillchar]) Return the string left justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.lower() Return a copy of the string with all the cased characters [4] converted to lowercase. For 8-bit strings, this method is locale-dependent. str.lstrip([chars]) Return a copy of the string with leading characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix; rather, all combinations of its values are stripped: >>> ' spacious '.lstrip() 'spacious ' >>> 'www.example.com'.lstrip('cmowz.') 'example.com' Changed in version 2.2.2: Support for the *chars* argument. str.partition(sep) Split the string at the first occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. New in version 2.5. str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced. str.rfind(sub[, start[, end]]) Return the highest index in the string where substring *sub* is found, such that *sub* is contained within "s[start:end]". Optional arguments *start* and *end* are interpreted as in slice notation. Return "-1" on failure. str.rindex(sub[, start[, end]]) Like "rfind()" but raises "ValueError" when the substring *sub* is not found. str.rjust(width[, fillchar]) Return the string right justified in a string of length *width*. Padding is done using the specified *fillchar* (default is a space). The original string is returned if *width* is less than or equal to "len(s)". Changed in version 2.4: Support for the *fillchar* argument. str.rpartition(sep) Split the string at the last occurrence of *sep*, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. New in version 2.5. str.rsplit([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* ones. If *sep* is not specified or "None", any whitespace string is a separator. Except for splitting from the right, "rsplit()" behaves like "split()" which is described in detail below. New in version 2.4. str.rstrip([chars]) Return a copy of the string with trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a suffix; rather, all combinations of its values are stripped: >>> ' spacious '.rstrip() ' spacious' >>> 'mississippi'.rstrip('ipz') 'mississ' Changed in version 2.2.2: Support for the *chars* argument. str.split([sep[, maxsplit]]) Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most "maxsplit+1" elements). If *maxsplit* is not specified or "-1", then there is no limit on the number of splits (all possible splits are made). If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')" returns "['1', '', '2']"). The *sep* argument may consist of multiple characters (for example, "'1<>2<>3'.split('<>')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". If *sep* is not specified or is "None", a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a "None" separator returns "[]". For example, "' 1 2 3 '.split()" returns "['1', '2', '3']", and "' 1 2 3 '.split(None, 1)" returns "['1', '2 3 ']". str.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. This method uses the *universal newlines* approach to splitting lines. Line breaks are not included in the resulting list unless *keepends* is given and true. Python recognizes ""\r"", ""\n"", and ""\r\n"" as line boundaries for 8-bit strings. For example: >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike "split()" when a delimiter string *sep* is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line: >>> "".splitlines() [] >>> "One line\n".splitlines() ['One line'] For comparison, "split('\n')" gives: >>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', ''] unicode.splitlines([keepends]) Return a list of the lines in the string, like "str.splitlines()". However, the Unicode method splits on the following line boundaries, which are a superset of the *universal newlines* recognized for 8-bit strings. +-------------------------+-------------------------------+ | Representation | Description | +=========================+===============================+ | "\n" | Line Feed | +-------------------------+-------------------------------+ | "\r" | Carriage Return | +-------------------------+-------------------------------+ | "\r\n" | Carriage Return + Line Feed | +-------------------------+-------------------------------+ | "\v" or "\x0b" | Line Tabulation | +-------------------------+-------------------------------+ | "\f" or "\x0c" | Form Feed | +-------------------------+-------------------------------+ | "\x1c" | File Separator | +-------------------------+-------------------------------+ | "\x1d" | Group Separator | +-------------------------+-------------------------------+ | "\x1e" | Record Separator | +-------------------------+-------------------------------+ | "\x85" | Next Line (C1 Control Code) | +-------------------------+-------------------------------+ | "\u2028" | Line Separator | +-------------------------+-------------------------------+ | "\u2029" | Paragraph Separator | +-------------------------+-------------------------------+ Changed in version 2.7: "\v" and "\f" added to list of line boundaries. str.startswith(prefix[, start[, end]]) Return "True" if string starts with the *prefix*, otherwise return "False". *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position. Changed in version 2.5: Accept tuples as *prefix*. str.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The *chars* argument is a string specifying the set of characters to be removed. If omitted or "None", the *chars* argument defaults to removing whitespace. The *chars* argument is not a prefix or suffix; rather, all combinations of its values are stripped: >>> ' spacious '.strip() 'spacious' >>> 'www.example.com'.strip('cmowz.') 'example' Changed in version 2.2.2: Support for the *chars* argument. str.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. For 8-bit strings, this method is locale-dependent. str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result: >>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk" A workaround for apostrophes can be constructed using regular expressions: >>> import re >>> def titlecase(s): ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", ... lambda mo: mo.group(0)[0].upper() + ... mo.group(0)[1:].lower(), ... s) ... >>> titlecase("they're bill's friends.") "They're Bill's Friends." For 8-bit strings, this method is locale-dependent. str.translate(table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument *deletechars* are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. You can use the "maketrans()" helper function in the "string" module to create a translation table. For string objects, set the *table* argument to "None" for translations that only delete characters: >>> 'read this short text'.translate(None, 'aeiou') 'rd ths shrt txt' New in version 2.6: Support for a "None" *table* argument. For Unicode objects, the "translate()" method does not accept the optional *deletechars* argument. Instead, it returns a copy of the *s* where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or "None". Unmapped characters are left untouched. Characters mapped to "None" are deleted. Note, a more flexible approach is to create a custom character mapping codec using the "codecs" module (see "encodings.cp1251" for an example). str.upper() Return a copy of the string with all the cased characters [4] converted to uppercase. Note that "str.upper().isupper()" might be "False" if "s" contains uncased characters or if the Unicode category of the resulting character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase). For 8-bit strings, this method is locale-dependent. str.zfill(width) Return the numeric string left filled with zeros in a string of length *width*. A sign prefix is handled correctly. The original string is returned if *width* is less than or equal to "len(s)". New in version 2.2.2. The following methods are present only on unicode objects: unicode.isnumeric() Return "True" if there are only numeric characters in S, "False" otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. unicode.isdecimal() Return "True" if there are only decimal characters in S, "False" otherwise. Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. String Formatting Operations ============================ String and Unicode objects have one unique built-in operation: the "%" operator (modulo). This is also known as the string *formatting* or *interpolation* operator. Given "format % values" (where *format* is a string or Unicode object), "%" conversion specifications in *format* are replaced with zero or more elements of *values*. The effect is similar to the using "sprintf()" in the C language. If *format* is a Unicode object, or if any of the objects being converted using the "%s" conversion are Unicode objects, the result will also be a Unicode object. If *format* requires a single argument, *values* may be a single non- tuple object. [5] Otherwise, *values* must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary). A conversion specifier contains two or more characters and has the following components, which must occur in this order: 1. The "'%'" character, which marks the start of the specifier. 2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, "(somename)"). 3. Conversion flags (optional), which affect the result of some conversion types. 4. Minimum field width (optional). If specified as an "'*'" (asterisk), the actual width is read from the next element of the tuple in *values*, and the object to convert comes after the minimum field width and optional precision. 5. Precision (optional), given as a "'.'" (dot) followed by the precision. If specified as "'*'" (an asterisk), the actual width is read from the next element of the tuple in *values*, and the value to convert comes after the precision. 6. Length modifier (optional). 7. Conversion type. When the right argument is a dictionary (or other mapping type), then the formats in the string *must* include a parenthesised mapping key into that dictionary inserted immediately after the "'%'" character. The mapping key selects the value to be formatted from the mapping. For example: >>> print '%(language)s has %(number)03d quote types.' % \ ... {"language": "Python", "number": 2} Python has 002 quote types. In this case no "*" specifiers may occur in a format (since they require a sequential parameter list). The conversion flag characters are: +-----------+-----------------------------------------------------------------------+ | Flag | Meaning | +===========+=======================================================================+ | "'#'" | The value conversion will use the "alternate form" (where defined | | | below). | +-----------+-----------------------------------------------------------------------+ | "'0'" | The conversion will be zero padded for numeric values. | +-----------+-----------------------------------------------------------------------+ | "'-'" | The converted value is left adjusted (overrides the "'0'" conversion | | | if both are given). | +-----------+-----------------------------------------------------------------------+ | "' '" | (a space) A blank should be left before a positive number (or empty | | | string) produced by a signed conversion. | +-----------+-----------------------------------------------------------------------+ | "'+'" | A sign character ("'+'" or "'-'") will precede the conversion | | | (overrides a "space" flag). | +-----------+-----------------------------------------------------------------------+ A length modifier ("h", "l", or "L") may be present, but is ignored as it is not necessary for Python -- so e.g. "%ld" is identical to "%d". The conversion types are: +--------------+-------------------------------------------------------+---------+ | Conversion | Meaning | Notes | +==============+=======================================================+=========+ | "'d'" | Signed integer decimal. | | +--------------+-------------------------------------------------------+---------+ | "'i'" | Signed integer decimal. | | +--------------+-------------------------------------------------------+---------+ | "'o'" | Signed octal value. | (1) | +--------------+-------------------------------------------------------+---------+ | "'u'" | Obsolete type -- it is identical to "'d'". | (7) | +--------------+-------------------------------------------------------+---------+ | "'x'" | Signed hexadecimal (lowercase). | (2) | +--------------+-------------------------------------------------------+---------+ | "'X'" | Signed hexadecimal (uppercase). | (2) | +--------------+-------------------------------------------------------+---------+ | "'e'" | Floating point exponential format (lowercase). | (3) | +--------------+-------------------------------------------------------+---------+ | "'E'" | Floating point exponential format (uppercase). | (3) | +--------------+-------------------------------------------------------+---------+ | "'f'" | Floating point decimal format. | (3) | +--------------+-------------------------------------------------------+---------+ | "'F'" | Floating point decimal format. | (3) | +--------------+-------------------------------------------------------+---------+ | "'g'" | Floating point format. Uses lowercase exponential | (4) | | | format if exponent is less than -4 or not less than | | | | precision, decimal format otherwise. | | +--------------+-------------------------------------------------------+---------+ | "'G'" | Floating point format. Uses uppercase exponential | (4) | | | format if exponent is less than -4 or not less than | | | | precision, decimal format otherwise. | | +--------------+-------------------------------------------------------+---------+ | "'c'" | Single character (accepts integer or single character | | | | string). | | +--------------+-------------------------------------------------------+---------+ | "'r'" | String (converts any Python object using repr()). | (5) | +--------------+-------------------------------------------------------+---------+ | "'s'" | String (converts any Python object using "str()"). | (6) | +--------------+-------------------------------------------------------+---------+ | "'%'" | No argument is converted, results in a "'%'" | | | | character in the result. | | +--------------+-------------------------------------------------------+---------+ Notes: 1. The alternate form causes a leading zero ("'0'") to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero. 2. The alternate form causes a leading "'0x'" or "'0X'" (depending on whether the "'x'" or "'X'" format was used) to be inserted before the first digit. 3. The alternate form causes the result to always contain a decimal point, even if no digits follow it. The precision determines the number of digits after the decimal point and defaults to 6. 4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6. 5. The "%r" conversion was added in Python 2.0. The precision determines the maximal number of characters used. 6. If the object or format provided is a "unicode" string, the resulting string will also be "unicode". The precision determines the maximal number of characters used. 7. See **PEP 237**. Since Python strings have an explicit length, "%s" conversions do not assume that "'\0'" is the end of the string. Changed in version 2.7: "%f" conversions for numbers whose absolute value is over 1e50 are no longer replaced by "%g" conversions. Additional string operations are defined in standard modules "string" and "re". XRange Type =========== The "xrange" type is an immutable sequence which is commonly used for looping. The advantage of the "xrange" type is that an "xrange" object will always take the same amount of memory, no matter the size of the range it represents. There are no consistent performance advantages. XRange objects have very little behavior: they only support indexing, iteration, and the "len()" function. Mutable Sequence Types ====================== List and "bytearray" objects support additional operations that allow in-place modification of the object. Other mutable sequence types (when added to the language) should also support these operations. Strings and tuples are immutable sequence types: such objects cannot be modified once created. The following operations are defined on mutable sequence types (where *x* is an arbitrary object): +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | same as "s[len(s):len(s)] = [x]" | (2) | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | for the most part the same as | (3) | | | "s[len(s):len(s)] = t" | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (11) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.count(x)" | return number of *i*'s for which | | | | "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.index(x[, i[, j]])" | return smallest *k* such that | (4) | | | "s[k] == x" and "i <= k < j" | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | same as "s[i:i] = [x]" | (5) | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | same as "x = s[i]; del s[i]; | (6) | | | return x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | same as "del s[s.index(x)]" | (4) | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (7) | | | place | | +--------------------------------+----------------------------------+-----------------------+ | "s.sort([cmp[, key[, | sort the items of *s* in place | (7)(8)(9)(10) | | reverse]]])" | | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4. 3. *t* can be any iterable object. 4. Raises "ValueError" when *x* is not found in *s*. When a negative index is passed as the second or third parameter to the "index()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, "index()" didn't have arguments for specifying start and stop positions. 5. When a negative index is passed as the first parameter to the "insert()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, all negative indices were truncated to zero. 6. The "pop()" method's optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 7. The "sort()" and "reverse()" methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. 8. The "sort()" method takes optional arguments for controlling the comparisons. *cmp* specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: "cmp=lambda x,y: cmp(x.lower(), y.lower())". The default value is "None". *key* specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower". The default value is "None". *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. In general, the *key* and *reverse* conversion processes are much faster than specifying an equivalent *cmp* function. This is because *cmp* is called multiple times for each list element while *key* and *reverse* touch each element only once. Use "functools.cmp_to_key()" to convert an old-style *cmp* function to a *key* function. Changed in version 2.3: Support for "None" as an equivalent to omitting *cmp* was added. Changed in version 2.4: Support for *key* and *reverse* was added. 9. Starting with Python 2.3, the "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). 10. **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python 2.3 and newer makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. 11. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange. ttypesseqsI Mutable Sequence Types ********************** List and "bytearray" objects support additional operations that allow in-place modification of the object. Other mutable sequence types (when added to the language) should also support these operations. Strings and tuples are immutable sequence types: such objects cannot be modified once created. The following operations are defined on mutable sequence types (where *x* is an arbitrary object): +--------------------------------+----------------------------------+-----------------------+ | Operation | Result | Notes | +================================+==================================+=======================+ | "s[i] = x" | item *i* of *s* is replaced by | | | | *x* | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j] = t" | slice of *s* from *i* to *j* is | | | | replaced by the contents of the | | | | iterable *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j]" | same as "s[i:j] = []" | | +--------------------------------+----------------------------------+-----------------------+ | "s[i:j:k] = t" | the elements of "s[i:j:k]" are | (1) | | | replaced by those of *t* | | +--------------------------------+----------------------------------+-----------------------+ | "del s[i:j:k]" | removes the elements of | | | | "s[i:j:k]" from the list | | +--------------------------------+----------------------------------+-----------------------+ | "s.append(x)" | same as "s[len(s):len(s)] = [x]" | (2) | +--------------------------------+----------------------------------+-----------------------+ | "s.extend(t)" or "s += t" | for the most part the same as | (3) | | | "s[len(s):len(s)] = t" | | +--------------------------------+----------------------------------+-----------------------+ | "s *= n" | updates *s* with its contents | (11) | | | repeated *n* times | | +--------------------------------+----------------------------------+-----------------------+ | "s.count(x)" | return number of *i*'s for which | | | | "s[i] == x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.index(x[, i[, j]])" | return smallest *k* such that | (4) | | | "s[k] == x" and "i <= k < j" | | +--------------------------------+----------------------------------+-----------------------+ | "s.insert(i, x)" | same as "s[i:i] = [x]" | (5) | +--------------------------------+----------------------------------+-----------------------+ | "s.pop([i])" | same as "x = s[i]; del s[i]; | (6) | | | return x" | | +--------------------------------+----------------------------------+-----------------------+ | "s.remove(x)" | same as "del s[s.index(x)]" | (4) | +--------------------------------+----------------------------------+-----------------------+ | "s.reverse()" | reverses the items of *s* in | (7) | | | place | | +--------------------------------+----------------------------------+-----------------------+ | "s.sort([cmp[, key[, | sort the items of *s* in place | (7)(8)(9)(10) | | reverse]]])" | | | +--------------------------------+----------------------------------+-----------------------+ Notes: 1. *t* must have the same length as the slice it is replacing. 2. The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4. 3. *t* can be any iterable object. 4. Raises "ValueError" when *x* is not found in *s*. When a negative index is passed as the second or third parameter to the "index()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, "index()" didn't have arguments for specifying start and stop positions. 5. When a negative index is passed as the first parameter to the "insert()" method, the list length is added, as for slice indices. If it is still negative, it is truncated to zero, as for slice indices. Changed in version 2.3: Previously, all negative indices were truncated to zero. 6. The "pop()" method's optional argument *i* defaults to "-1", so that by default the last item is removed and returned. 7. The "sort()" and "reverse()" methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. 8. The "sort()" method takes optional arguments for controlling the comparisons. *cmp* specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: "cmp=lambda x,y: cmp(x.lower(), y.lower())". The default value is "None". *key* specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower". The default value is "None". *reverse* is a boolean value. If set to "True", then the list elements are sorted as if each comparison were reversed. In general, the *key* and *reverse* conversion processes are much faster than specifying an equivalent *cmp* function. This is because *cmp* is called multiple times for each list element while *key* and *reverse* touch each element only once. Use "functools.cmp_to_key()" to convert an old-style *cmp* function to a *key* function. Changed in version 2.3: Support for "None" as an equivalent to omitting *cmp* was added. Changed in version 2.4: Support for *key* and *reverse* was added. 9. Starting with Python 2.3, the "sort()" method is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal --- this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade). 10. **CPython implementation detail:** While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python 2.3 and newer makes the list appear empty for the duration, and raises "ValueError" if it can detect that the list has been mutated during a sort. 11. The value *n* is an integer, or an object implementing "__index__()". Zero and negative values of *n* clear the sequence. Items in the sequence are not copied; they are referenced multiple times, as explained for "s * n" under Sequence Types --- str, unicode, list, tuple, bytearray, buffer, xrange. stypesseq-mutables Unary arithmetic and bitwise operations *************************************** All unary arithmetic and bitwise operations have the same priority: u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr The unary "-" (minus) operator yields the negation of its numeric argument. The unary "+" (plus) operator yields its numeric argument unchanged. The unary "~" (invert) operator yields the bitwise inversion of its plain or long integer argument. The bitwise inversion of "x" is defined as "-(x+1)". It only applies to integral numbers. In all three cases, if the argument does not have the proper type, a "TypeError" exception is raised. tunarys The "while" statement ********************* The "while" statement is used for repeated execution as long as an expression is true: while_stmt ::= "while" expression ":" suite ["else" ":" suite] This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the "else" clause, if present, is executed and the loop terminates. A "break" statement executed in the first suite terminates the loop without executing the "else" clause's suite. A "continue" statement executed in the first suite skips the rest of the suite and goes back to testing the expression. twhiles The "with" statement ******************** New in version 2.5. The "with" statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common "try"..."except"..."finally" usage patterns to be encapsulated for convenient reuse. with_stmt ::= "with" with_item ("," with_item)* ":" suite with_item ::= expression ["as" target] The execution of the "with" statement with one "item" proceeds as follows: 1. The context expression (the expression given in the "with_item") is evaluated to obtain a context manager. 2. The context manager's "__exit__()" is loaded for later use. 3. The context manager's "__enter__()" method is invoked. 4. If a target was included in the "with" statement, the return value from "__enter__()" is assigned to it. Note: The "with" statement guarantees that if the "__enter__()" method returns without an error, then "__exit__()" will always be called. Thus, if an error occurs during the assignment to the target list, it will be treated the same as an error occurring within the suite would be. See step 6 below. 5. The suite is executed. 6. The context manager's "__exit__()" method is invoked. If an exception caused the suite to be exited, its type, value, and traceback are passed as arguments to "__exit__()". Otherwise, three "None" arguments are supplied. If the suite was exited due to an exception, and the return value from the "__exit__()" method was false, the exception is reraised. If the return value was true, the exception is suppressed, and execution continues with the statement following the "with" statement. If the suite was exited for any reason other than an exception, the return value from "__exit__()" is ignored, and execution proceeds at the normal location for the kind of exit that was taken. With more than one item, the context managers are processed as if multiple "with" statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Note: In Python 2.5, the "with" statement is only allowed when the "with_statement" feature has been enabled. It is always enabled in Python 2.6. Changed in version 2.7: Support for multiple context expressions. See also: **PEP 343** - The "with" statement The specification, background, and examples for the Python "with" statement. twiths The "yield" statement ********************* yield_stmt ::= yield_expression The "yield" statement is only used when defining a generator function, and is only used in the body of the generator function. Using a "yield" statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator's "next()" method repeatedly until it raises an exception. When a "yield" statement is executed, the state of the generator is frozen and the value of "expression_list" is returned to "next()"'s caller. By "frozen" we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time "next()" is invoked, the function can proceed exactly as if the "yield" statement were just another external call. As of Python version 2.5, the "yield" statement is now allowed in the "try" clause of a "try" ... "finally" construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator's "close()" method will be called, allowing any pending "finally" clauses to execute. For full details of "yield" semantics, refer to the Yield expressions section. Note: In Python 2.2, the "yield" statement was only allowed when the "generators" feature has been enabled. This "__future__" import statement was used to enable the feature: from __future__ import generators See also: **PEP 255** - Simple Generators The proposal for adding generators and the "yield" statement to Python. **PEP 342** - Coroutines via Enhanced Generators The proposal that, among other generator enhancements, proposed allowing "yield" to appear inside a "try" ... "finally" block. tyieldN(ttopics(((s)/usr/lib64/python2.7/pydoc_data/topics.pyts ')9U   2 KMA#%-UJK<,1cU%+&{*;;MmD&" B&\QPKa2]&т __init__.pycnu[ |fc@sdS(N((((s+/usr/lib64/python2.7/pydoc_data/__init__.pyttPKa2]&т __init__.pyonu[ |fc@sdS(N((((s+/usr/lib64/python2.7/pydoc_data/__init__.pyttPKr|0]d8m-- _pydoc.cssnu[PKr|0]!g__pycache__/topics.cpython-36.pycnu[PKr|0]g9~~):"__pycache__/__init__.cpython-36.opt-1.pycnu[PKr|0]g9~~)#__pycache__/__init__.cpython-36.opt-2.pycnu[PKr|0]'#__pycache__/topics.cpython-36.opt-1.pycnu[PKr|0]'@ __pycache__/topics.cpython-36.opt-2.pycnu[PKr|0]g9~~#]__pycache__/__init__.cpython-36.pycnu[PKr|0] k^__init__.pynu[PKr|0] DN N ^topics.pynu[PKe&1]tkk topics.pycnu[PKe&1]?$#__pycache__/__init__.cpython-312.pycnu[PKe&1]?*o#__pycache__/__init__.cpython-312.opt-1.pycnu[PKe&1]?*V#__pycache__/__init__.cpython-312.opt-2.pycnu[PKa2]tkk =#topics.pyonu[PKa2]&т I)__init__.pycnu[PKa2]&т )__init__.pyonu[PKʼn)