xxleyi / learning_list

聚集自己的学习笔记
11 stars 3 forks source link

Python (编程)进阶,阅读 The Python Language Reference 之 Data Model #110

Open xxleyi opened 5 years ago

xxleyi commented 5 years ago
  1. Objects, values and types
  2. The standard type hierarchy
  3. Special method names
  4. Coroutines

这一章系统介绍 Python 的世界观。在 Python 中,一切皆是对象。对象是 data,一切中也包含 code,所以 code is data 也是 Python 的世界观之一。既然一切都是 data,那么 data 以一个什么样的形式组织起来就是至为关键的一部分。

其一,Python 将 data 封装为 object,每一个 object 都有自己的 value 和 type。同时在这一切之上的之上,源头之上的源头,有专指的 object 和 type,前者 object 是一切对象的始祖,后者 type 是一切类型的始祖。类型也是一种对象,所以 type 是 object 的实例。ojbect 作为一个对象,也必须有类型,其类型恰好是 type。同时,type 也是其自身的实例。所以,type 是万物的始祖,是 Python 这门变戏法之所以存在的根基,既是元类型,也是元类型的元实例。

所以有这样的逻辑存在:

# all of these return True
isinstance(object, type)
isinstance(type, object)
isinstance(type, type)

其二,Python 中有自己的一套类型层次,值得一览,至少有需要时可以来查阅。

其三,Data Model 的核心是一系列魔术方法:静态方法,实例方法。利用这些魔术方法,可以非常方便的定制某个算术或者其它形如取值,赋值,取属性,设属性等等操作的真正行为,从而随心所欲的创造或者进一步定制符合自己心意的 ADT。

其四,coroutine 也就是协程,在通常的理解中,可能会觉得它是一个过程,但它依然是对象,是 data,是一种特定的 data model。就像 iterator,generator 一样,是一种 ADT。

总之,通过概览 Data Model,可以完整的一览 Python 针对 ADT 的一系列支持以及提供的一系列成品和工具。既可进一步深入细节,又可退一步俯瞰全貌,如此才可进阶。

xxleyi commented 5 years ago

Objects, values and types

Objects are Python’s abstraction for data. Every object has an identity, a type and a value.

Immutability is not strictly the same as having an unchangeable value, it is more subtle.

Objects are never explicitly destroyed; The ‘try…finally’ statement and the ‘with’ statement provide convenient ways to release “external” resources.

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.

xxleyi commented 5 years ago

The standard type hierarchy

* None
* NotImplemented
* Ellipsis
* numbers.Number
* Python distinguishes between integers, floating point numbers, and complex numbers:
    * numbers.Integral
        * Integers (int)
        * Booleans (bool)
    * numbers.Real (float)
    * numbers.Complex (complex)
* Sequences are distinguished according to their mutability:
    * The following types are immutable sequences:
        * Strings
        * Tuples
        * Bytes
    * There are currently two intrinsic mutable sequence types:
        * Lists
        * Byte Arrays
    * There are currently two intrinsic set types:
        * Sets
        * Frozen sets
    * There is currently a single intrinsic mapping type:
        * Dictionaries
        * The extension modules dbm.ndbm and dbm.gnu provide additional examples of mapping types, as does the collections module.
* Callable types
    * User-defined functions
        * Special attributes:
            * __doc__
            * __name__
            * __qualname__
            * __module__
            * __defaults__
            * __code__
            * __globals__
            * __dict__
            * __closure__
            * __annotations__
            * __kwdefaults__
    * Instance methods
    * Generator functions
    * Coroutine functions
    * Asynchronous generator functions
    * Built-in functions
    * Built-in methods
    * Classes
    * Class Instances
* Modules
* Custom classes
    * Special attributes: __dict__ is the attribute dictionary; __class__ is the instance’s class.
* Class instances
    * Special attributes: __dict__ is the attribute dictionary; __class__ is the instance’s class.
* I/O objects (also known as file objects)
* Internal types
    * Code 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_freevarsis 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.
    * Frame objects
        * 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.
        * 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
    * Slice objects
        * Slice objects are used to represent slices for __getitem__() methods. They are also created by the built-in slice() function.
* 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

几个有意思的点:

xxleyi commented 5 years ago

Special method names

这一部分内容最多最全,不是一时可以消化吸收,但在有需要时不断来此回顾,而不是直接上网搜索答案将是一个更好的学习方式。

每次深入一点点,扩展一点点,逐渐融会贯通。


重温之后发现一个大料:Python (编程)进阶,Descriptor in Python

xxleyi commented 5 years ago

Coroutines

这一部分内容不多,但具有启发性,明确的再一次告诉我们什么叫一切皆对象。

xxleyi commented 5 years ago

阅读这一章节带来的新东西: