3.Data model———从python官方文档上摘取

3.1 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.

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:
id(x) is the memory address where x is stored.

The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.

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:

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.

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.)

3.2. 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.), although such additions will often be provided via the standard library instead.

1 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.

2 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.

For example, fractions.Fraction uses:

def _operator_fallbacks(monomorphic_operator, fallback_operator):
    def forward(a, b):
        if isinstance(b, (int, Fraction)):
            return monomorphic_operator(a, b)
        elif isinstance(b, float):
            return fallback_operator(float(a), b)
        elif isinstance(b, complex):
            return fallback_operator(complex(a), b)
        else:
            return NotImplemented
    forward.__name__ = '__' + fallback_operator.__name__ + '__'
    forward.__doc__ = monomorphic_operator.__doc__

    def reverse(b, a):
        if isinstance(a, Rational):
            # Includes ints.
            return monomorphic_operator(a, b)
        elif isinstance(a, numbers.Real):
            return fallback_operator(float(a), float(b))
        elif isinstance(a, numbers.Complex):
            return fallback_operator(complex(a), complex(b))
        else:
            return NotImplemented
    reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
    reverse.__doc__ = monomorphic_operator.__doc__

    return forward, reverse

def _add(a, b):
    """a + b"""
    return Fraction(a.numerator * b.denominator +
                    b.numerator * a.denominator,
                    a.denominator * b.denominator)

__add__, __radd__ = _operator_fallbacks(_add, operator.add)

3 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.

4 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:Integers (int),Booleans (bool)
numbers.Real (float)
numbers.Complex (complex)

5 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.
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.

  • 5.1 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.)
    • 5.1.1 Strings
      All the code points in the range U+0000 - U+10FFFF can be represented in a string.
      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.
    • 5.1.2 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.
    • 5.1.3 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.
  • 5.2 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.
    • 5.2.1 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.)
    • 5.2.2 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.
      This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.
      namedtuple() factory function for creating tuple subclasses with named fields
      deque list-like container with fast appends and pops on either end
      ChainMap dict-like class for creating a single view of multiple mappings
      Counter dict subclass for counting hashable objects
      OrderedDict dict subclass that remembers the order entries were added
      defaultdict dict subclass that calls a factory function to supply missing values
      UserDict wrapper around dictionary objects for easier dict subclassing
      UserList wrapper around list objects for easier list subclassing
      UserString wrapper around string objects for easier string subclassing

6 Set types
These represent unordered, finite sets of unique, immutable objects.

  • 6.1 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().
  • 6.2 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.

7 Mappings

  • Dictionaries
    These represent finite sets of objects indexed by nearly arbitrary values.
    Dictionaries are mutable; they can be created by the {...}

8 Callable types
These are the types to which the function call operation (see section Calls) can be applied:

  • 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

  • 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).

  • 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 able
doc The function’s documentation string, or None if unavailable; not inherited by subclasses. Writable
name The function’s name. Writable
qualname The function’s qualified name. New in version 3.3. Writable
module The name of the module the function was defined in, or None if unavailable. Writable
defaults A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value. Writable
code The code object representing the compiled function body. Writable
globals A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined. Read-only
dict The namespace supporting arbitrary function attributes. Writable
closure None or a tuple of cells that contain bindings for the function’s free variables. See below for information on the cell_contents attribute. Read-only
annotations A dict containing annotations of parameters. The keys of the dict are the parameter names, and 'return' for the return annotation, if provided. Writable
kwdefaults A dict containing defaults for keyword-only parameters. Writable
  • 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.

  • 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.

  • 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.

  • 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.

9 Modules
Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the import statement, 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).
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. \

10 Custom classes
Custom class types are typically created by class definitions
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.
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.
11 Class instances
A class instance is created by calling a class object (see above)
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;
If no class attribute is found, and the object’s class has a __getattr__() method, that is called to satisfy the lookup.
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.
12 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.
13 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.

  • Frame objects
    Frame objects represent execution frames. They may occur in traceback objects (see below), and are also passed to registered trace functions.
    Frame objects represent execution frames. They may occur in traceback objects (see below), and are also passed to registered trace functions.

    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 for various events during code execution (this is used by the debugger). Normally an event is triggered for each new source line - this can be disabled by setting f_trace_lines to False.

    Implementations may allow per-opcode events to be requested by setting f_trace_opcodes to True. Note that this may lead to undefined interpreter behaviour if exceptions raised by the trace function escape to the function being traced.

    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.

  • Traceback objects
    Traceback objects represent a stack trace of an exception.
    A traceback object is implicitly created when an exception occurs, and may also be explicitly created by calling types.TracebackType.
    For implicitly created tracebacks, It is accessible as the third item of the tuple returned by sys.exc_info(), and as the traceback attribute of the caught exception.
    For explicitly created tracebacks, it is up to the creator of the traceback to determine how the tb_next attributes should be linked to form a full stack trace.

  • 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.indices(self, length)
    It returns a tuple of three integers; respectively these are the start and stop indices and the step or stride length of the slice.

  • Static method objects
    Static method objects are created by the built-in staticmethod() constructor.

  • Class method objects
    Class method objects are created by the built-in classmethod() constructor.

3.3 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]

3.3.1Basic customization

  • object.__new__(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 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__().

    __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[, ...])
    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.
    It is possible (though not recommended!) for the __del__() method to postpone destruction of the instance by creating a new reference to it.

    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.

  • object.__repr__(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.
    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.
    Changed in version 3.7: object.__format__(x, '') is now equivalent to str(x) rather than format(str(self), '').

  • 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.

    A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments.

  • 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.

    def __hash__(self):
    return hash((self.name, self.nick, self.color))
    

    Note: hash() truncates the value returned from an object’s custom __hash__() method to the size of a Py_ssize_t. This is typically 8 bytes on 64-bit builds and 4 bytes on 32-bit builds.
    An easy way to do this is with python -c "import sys; print(sys.hash_info.width)"

    If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash\__.

  • object.__bool__(self)

3.3.2. Customizing attribute access

  • object.__getattr__(self, name)
    Called when the default attribute access fails with an AttributeError
    Note that if the attribute is found through the normal mechanism, __getattr__() is not called.

  • 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.

  • object.__setattr__(self, name, value)
    Called when an attribute assignment is attempted.

  • object.__delattr__(self, name)
    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.

3.3.2.1. 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}...')
        super().__setattr__(attr, value)

sys.modules[__name__].__class__ = VerboseModule

3.3.2.2. Implementing Descriptors

  • 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.

3.3.2.3. 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 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__).

3.3.2.4. __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. Attribute lookup speed can be significantly improved as well.

  • Notes on using __slots__

    1. When inheriting from a class without __slots__, the __dict__ and __weakref__ attribute of the instances will always be accessible.
    1. Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition
    1. Without a __weakref__ variable for each instance, classes defining __slots__ do not support weak references to its instances.
    1. __slots__ are implemented at the class level by creating descriptors (Implementing Descriptors) for each variable name.
    1. 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.
    1. 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.

3.3.3. 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
    

    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).

  • 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:

    1. MRO entries are resolved;
    2. the appropriate metaclass is determined;
    3. the class namespace is prepared;
    4. the class body is executed;
    5. the class object is created.
  • Resolving MRO entries
    If a base that appears in class definition is not an instance of type, then an __mro_entries__ method is searched on it. If found, it is called with the original bases tuple. This method must return a tuple of classes that will be used instead of this base. The tuple may be empty, in such case the original base is ignored.

  • Determining the appropriate metaclass
    The appropriate metaclass for a class definition is determined as follows:

    1. if no bases and no explicit metaclass are given, then type() is used;
    2. if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass;
    3. 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.
    f 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.

  • Executing the class body
    The class body is executed (approximately) as exec(body, globals(), namespace).

  • 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__).

    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;
    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.

  • 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.

3.3.4. 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.

3.3.5. Emulating generic types

classmethod object.__class_getitem__(cls, key)
Return an object representing the specialization of a generic class by type arguments found in key.

This method is looked up on the class object itself, and when defined in the class body, this method is implicitly a class method. Note, this mechanism is primarily reserved for use with static type hints, other usage is discouraged.

3.3.6.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, ...).

3.3.7. Emulating container types

The following methods can be defined to implement container objects.
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
Mutable sequences should provide methods append(), count(), index(), extend(), insert(), pop(), remove(), reverse() and sort(), like Python standard list objects.
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()
  • object.__length_hint__(self)
    Called to implement operator.length_hint()
    Should return an estimated length for the object
  • object.__getitem__(self, key)
    Called to implement evaluation of self[key]. For sequence types,
    the accepted keys should be integers and slice objects
  • object.__setitem__(self, key, value)
  • object.__delitem__(self, key)
  • 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)
  • 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
  • object.__contains__(self, item)
    Called to implement membership test operators

3.3.8. Emulating numeric types

The following methods can be defined to emulate numeric objects.

  • 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(), **, <<, >>, &, ^, |).
x + y, where x is an instance of a class that has an __add__() method, x.__add__(y)

If one of those methods does not support the operation with the supplied arguments, it should return NotImplemented.

3.3.9. 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.

  • 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.

3.3.10. Special method lookup

>>> 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

3.4. Coroutines

3.4.1. Awaitable Objects

An awaitable object generally implements an __await__() method. Coroutine objects returned from async def functions are awaitable.

Note: The generator iterator objects returned from generators decorated with types.coroutine() or asyncio.coroutine() are also awaitable, but they do not implement __await__().

  • object.__await__(self)
    Must return an iterator. Should be used to implement awaitable objects. For instance, asyncio.Future implements this method to be compatible with the await expression.

3.4.2. Coroutine Objects

Coroutine objects are awaitable objects. A coroutine’s execution can be controlled by calling __await__() and iterating over the result. When the coroutine has finished executing and returns, the iterator raises StopIteration, and the exception’s value attribute holds the return value. If the coroutine raises an exception, it is propagated by the iterator. Coroutines should not directly raise unhandled StopIteration exceptions.

  • coroutine.send(value)
  • coroutine.throw(type[, value[, traceback]])
  • coroutine.close()

Coroutine objects are automatically closed using the above process when they are about to be destroyed.

3.4.3. Asynchronous Iterators

An asynchronous iterator can call asynchronous code in its __anext__ method.

  • object.__aiter__(self)
    Must return an asynchronous iterator object.
  • object.__anext__(self)
    Must return an awaitable resulting in a next value of the iterator. Should raise a StopAsyncIteration error when the iteration is over.

    class Reader:
       async def readline(self):
           ...
       def __aiter__(self):
           return self
    
       async def __anext__(self):
           val = await self.readline()
           if val == b'':
               raise StopAsyncIteration
           return val
    

3.4.4. Asynchronous Context Managers

  • object.__aenter\_(self)
    This method is semantically similar to the __enter__(), with only difference that it must return an awaitable.
  • object.__aexit__(self, exc_type, exc_value, traceback)
    This method is semantically similar to the __exit__(), with only difference that it must return an awaitable.

    class AsyncContextManager:
    async def __aenter__(self):
        await log('entering context')
    
    async def __aexit__(self, exc_type, exc, tb):
        await log('exiting context')
    
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容