`
huangyiiiiii
  • 浏览: 118730 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

python types and objects

阅读更多
在探寻 metaclass 的过程中,对以前熟知的 OO 又有了全新的认识。我开始感觉到,只有在动态语言的世界,才有可能对这些概念有如此通透的理解。

在静态语言中,type 和 instance 之间永远隔着编译期和运行期之间的鸿沟。而在动态语言中,简单的 object 的概念统一世界,type 和 instance 突然变得如此的一致(都是object),其间并没有本质的区别。我们只有按照逻辑对其进行区分。

虽然由于现实世界问题概念本身的局限性,和 python 提供的各种方便的“语法糖”,使得普通的 python 程序和静态语言程序似乎区别不大。但其实当你深入 python 的基础,你会发现表象的下面其实隐藏着动态语言与生俱来的优雅和强大的根基!

这一切,都始于 python2.2 那一场翻天覆地的变革

Everything is an object

而所有 objects 之间存在着两种关系:is-a-kind-of 即类型-子类型,is-an-instance-of 即类型-实例。对应这两种关系,也就存在着两种产生新对象的方式:继承和实例化,前者可通过 class 语句:

class Temp(object):
a = 1
产生;
后者通过“调用”语句:

obj = Temp()
产生。

通过这两条规则,python中的对象可以向这两条正交的方向延伸,它们的起点就是内置对象:type 和 object 。

type 是所有对象(包括它自己!)的类型,object 是除了它自己以外所有类型(包括type!)的基类。
理论上说,从这两个起点出发我们可以朝着两个方向无限延伸,不过一般 python 代码中都只出现两层 类型-实例 关系,也就是:
metaclass - class - instance
一般 metaclass 是 type 的子类,class 是 object 的子类,instance 不存在继承关系。

参考:

Python Types and Objects 要是你对这个主题有兴趣,那一定要看这篇了,基本上你可以把本文当作是这篇文章的一个摘要。
分享到:
评论

相关推荐

    Python类型和对象.pdf

    Python 类型和对象 本文将详细介绍 Python 中的类型和对象,探讨新式对象(new-style)和用户定义的类及实例之间的关系,以及它们与内置类型的关系。 基本概念 在 Python 中,每个对象都是一个实体的体现,可以...

    Python Data Structures and Algorithms [2017]

    Python objects, types and expressions Python data types and structures Principles of data structure design Lists and pointer structures Stacks and Queues Trees Hashing and symbol tables Graphs and ...

    Perl To Python Migration

    The first section of Perl to Python Programming will show you the basic layout and approach of the two languages whilst the second section moves on to the specific differences of using Python data ...

    python3.6.5参考手册 chm

    Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API ...

    Core PYTHON Programming, 2nd Edition (epub 格式, Python 2.5)

    Using practical code examples, Chun introduces all the fundamentals of Python programming: syntax, objects and memory management, data types, operators, files and I/O, functions, generators, error ...

    python reference and libaray

    接下来,文档中提到基础数据类型(Fundamental Data Types),涵盖了Python中常用的数据类型,例如整型、浮点型、字符串、列表、元组、字典和集合等。这些数据类型构成了编程的基础,学习它们是掌握Python的先决条件...

    Pro Python电子版

    * Disguise your code as different types of objects in Python. * Inspect just about any object in Python. * Prepare your code for international audiences. * Ensure code quality with rigorous testing. ...

    quick python book 第三版

    About the Book ...Data types as objects Packages Using Python libraries PART 4 - WORKING WITH DATA Basic file wrangling Processing data files Data over the network Saving data Exploring data

    Learning Python 5th Edition(Python学习手册,第5版,英文)

    - Create and process objects with Python statements, and learn Python’s general syntax model - Use functions to avoid code redundancy and package code for reuse - Organize statements, functions, and ...

    Python 3.7.3

    Python 3.7.3 is the third maintenance ...The insertion-order preservation nature of dict objects is now an official part of the Python language spec. Notable performance improvements in many areas.

    Get Programming - Learn to code with Python.epub

    Lesson 5 - Object types and statements of code 46 Lesson 6 - Capstone project: your first Python program-convert hours to minutes UNIT 2 - STRINGS, TUPLES, AND INTERACTING WITH THE USER Lesson 7 - ...

    Scientific Computing with Python 3

    The essential building blocks of computational mathematics, linear algebra, and related Python objects Plot in Python using matplotlib to create high quality figures and graphics to draw and visualize...

    Rapid GUI Programming with Python and Qt

    Variables and Objects 27 Numbers and Strings 30 Collections 44 Built-in Functions 52 Summary 56 Exercises 57 Chapter 2. Control Structures 60 Conditional Branching 61 Looping 64 Functions 70...

    Python library

    The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but ...

Global site tag (gtag.js) - Google Analytics