让我们以一个Foo类开始:

class Foo(object):

def __init__(self, x, y=0):

self.x = x

self.y = y

当你实例化它(即创建该类的一个新的实例)时发生了什么?

f = Foo(1, y=2)

对Foo的调用到底调用了什么函数或方法呢?大多数新手甚至许多有经验的Python开发者会立刻回答:调用了__init__方法。如果你停下来仔细想1秒,你会发现这远不是一个正确答案。

__init__并没有返回一个对象,但是调用Foo(1, y=2)确实返回了一个对象。而且,__init__预期一个self参数,但是当我们调用Foo(1, y=2)时这里并没有这个参数。这里会有更复杂的工作。在这篇文章中,让我们探究下在Python中实例化一个类时到底发生了什么。

构造顺序

在Python中实例化一个对象包含了几个阶段,但它的妙处在于它们自身是Pythonic(python之禅)的——理解这些步骤使得我们对Python整体有多一点的了解。Foo是一个类,但是Python中的类也是对象!类、函数、方法以及实例都是对象,并且无论何时你将一对括号放在它们的名字后面时,就会调用它们的__call__方法。所以Foo(1, y=2)是等价于Foo.__call__(1, y=2)的。__call__方法是定义在Foo的类中的。Foo的类是什么呢?

>>> Foo.__class__

所以Foo是类型type的一个对象并且调用__call__返回一个Foo类的对象。让我们看下type中的__call__方法是什么样的。这个方法相当的复杂,但是我们尝试尽量简化它。在下面我粘贴了CPython C和PyPy Python的实现。我发想从源码中寻找答案是很有趣的,但是你也可以直接看下面的简化版:

CPython

static PyObject *

type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)

{

PyObject *obj;

if (type->tp_new == NULL) {

PyErr_Format(PyExc_TypeError,

"cannot create '%.100s' instances",

type->tp_name);

return NULL;

}

obj = type->tp_new(type, args, kwds);

obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);

if (obj == NULL)

return NULL;

/* Ugly exception: when the call was type(something),

don't call tp_init on the result. */

if (type == &PyType_Type &&

PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&

(kwds == NULL ||

(PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))

return obj;

/* If the returned object is not an instance of type,

it won't be initialized. */

if (!PyType_IsSubtype(Py_TYPE(obj), type))

return obj;

type = Py_TYPE(obj);

if (type->tp_init != NULL) {

int res = type->tp_init(obj, args, kwds);

if (res < 0) {

assert(PyErr_Occurred());

Py_DECREF(obj);

obj = NULL;

}

else {

assert(!PyErr_Occurred());

}

}

return obj;

}

PyPy

def descr_call(self, space, __args__):

promote(self)

# invoke the __new__ of the type

if not we_are_jitted():

# note that the annotator will figure out that self.w_new_function

# can only be None if the newshortcut config option is not set

w_newfunc = self.w_new_function

else:

# for the JIT it is better to take the slow path because normal lookup

# is nicely optimized, but the self.w_new_function attribute is not

# known to the JIT

w_newfunc = None

if w_newfunc is None:

w_newtype, w_newdescr = self.lookup_where('__new__')

if w_newdescr is None: # see test_crash_mro_without_object_1

raise oefmt(space.w_TypeError, "cannot create '%N' instances",

self)

w_newfunc = space.get(w_newdescr, self)

if (space.config.objspace.std.newshortcut and

not we_are_jitted() and

isinstance(w_newtype, W_TypeObject)):

self.w_new_function = w_newfunc

w_newobject = space.call_obj_args(w_newfunc, self, __args__)

call_init = space.isinstance_w(w_newobject, self)

# maybe invoke the __init__ of the type

if (call_init and not (space.is_w(self, space.w_type) and

not __args__.keywords and len(__args__.arguments_w) == 1)):

w_descr = space.lookup(w_newobject, '__init__')

if w_descr is not None: # see test_crash_mro_without_object_2

w_result = space.get_and_call_args(w_descr, w_newobject,

__args__)

if not space.is_w(w_result, space.w_None):

raise oefmt(space.w_TypeError,

"__init__() should return None")

return w_newobject

如果我们忽略错误检查,那么对于常规类的实例化它大致等同如下:

def __call__(obj_type, *args, **kwargs):

obj = obj_type.__new__(*args, **kwargs)

if obj is not None and issubclass(obj, obj_type):

obj.__init__(*args, **kwargs)

return obj

__new__方法为对象分配了内存空间,构建它为一个“空"对象然后__init__方法被调用来初始化它。

总的来说:

Foo(*args, **kwargs)等价于Foo.__call__(*args, **kwargs)

既然Foo是一个type的实例,Foo.__call__(*args, **kwargs)实际调用的是type.__call__(Foo, *args, **kwargs)

type.__call__(Foo, *args, **kwargs)调用type.__new__(Foo, *args, **kwargs),然后返回一个对象。

obj随后通过调用obj.__init__(*args, **kwargs)被初始化。

obj被返回。

定制

现在我们将注意力转移到__new__方法上。本质上,它是负责实际对象的创建的方法。我们不会具体探究__new__方法的底层实现细节。它的要点是它会为对象分配空间并返回该对象。有趣的是,一旦你意识到__new__做了什么,你可以用它来定制有趣的实例创建方式。值得注意的是,尽管__new__是一个静态方法,但你不需要用@staticmethod来声明它——它是Python解释器的特例。

一个精彩的展现__new__方法的力量的例子就是用它来实现一个单例类:

class Singleton(object):

_instance = None

def __new__(cls, *args, **kwargs):

if cls._instance is None:

cls._instance = super().__new__(cls, *args, **kwargs)

return cls._instance

然后使用它:

>>> s1 = Singleton()

... s2 = Singleton()

... s1 is s2

True

注意在这个单例类的实现中,__init__方法会在每次我们调用Singleton()时被调用,所以要小心处理。

另外一个相似的例子是实现Borg design pattern:

class Borg(object):

_dict = None

def __new__(cls, *args, **kwargs):

obj = super().__new__(cls, *args, **kwargs)

if cls._dict is None:

cls._dict = obj.__dict__

else:

obj.__dict__ = cls._dict

return obj

然后:

>>> b1 = Borg()

... b2 = Borg()

... b1 is b2

False

>>> b1.x = 8

... b2.x

8

最后的提醒——上面的例子展示了__new__的力量,但是只是说明你可以使用它,而不是意味着你应该这么做:

__new__是Python中最容易被滥用的特性。它晦涩难懂,又缺陷丛生,并且几乎每个用例都被我发现有更好的解决方案(使用其它的Python工具)。但是,当你确实需要__new__时,它令人难以置信的强大并且值得去理解。

—— Arion Sprague, Python’s Hidden New

在python中,一个问题的最佳解决方案是用__new__的情况是罕见的。麻烦的是如果你手里有把锤子,任何问题看起来都会像是钉子了 —— 那么你可能会突然遇到很多__new__能解决的问题。但是我们应该更倾向于更好的设计而不是使用一个全新的工具。__new__并不总是更好的。

参考

补充

如果 Foo 定义了一个 __call__方法,Foo(*args, **kwargs) 并不等于Foo.__call__(*args, **kwargs):

>>> class Foo:

... def __call__(self):

... print('running __call__')

...

>>> Foo()

>>> Foo.__call__()

Traceback (most recent call last):

File "", line 1, in

TypeError: __call__() missing 1 required positional argument: 'self'

In this case, __call__ is used to call instances of the class :

>>> Foo()()

running __call__

>>>

理解python的类实例化_理解python的类实例化相关推荐

  1. python在线搭建教程_理解python web开发,轻松搭建web app!

    大家好,今天分享给大家的是理解python web开发,轻松搭建web app,希望大家学有所获! 因为 python代码的优雅美观且易于维护这一特点,越来越多的人选择使用 Python做web开发. ...

  2. python web什么意思_理解Python的Web开发

    因为python代码的优雅美观且易于维护这一特点,越来越多的人选择使用Python做Web开发.而Python的Web框架百花齐放,目前比较流行的框架有大包大揽的Django,小巧灵活的Flask.B ...

  3. python如何使用多态_在python 3中,如何将多态应用于类

    介绍 多态性是为不同的基础形式(例如,数据类型或类)利用同一接口的能力.这允许函数在不同时间使用不同类型的实体. 对于Python中的面向对象编程,这意味着可以用与属于特定类的特定对象相同的方式来使用 ...

  4. python中mod运算符_自定义 Python 类中的运算符和函数重载(上)

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 如果你对 Python 中的str对象使用过 + 或 * 运算符,你一定注意到了它的操作与 int 或 f ...

  5. python多个函数_请教:一个类中可以定义多个同名函数?

    python认为每个文件是一个module 每个函数的的作用域就是本module 但是module可以被import import进来的module相当于在本module内 谢谢,你说得对,昨晚太晚了 ...

  6. python写一个类方法_重写python脚本,在脚本的每个类中注入一个方法 - python

    假设我有一个python模块foo.py,其中包含: class Foo(object): def __init__(self): pass 接下来,我想解析此脚本,并在每个类中注入一个方法,然后将其 ...

  7. python出现的意义_[转]Python中下划线以及命名空间的意义

    Python 用下划线作为变量前缀和后缀指定特殊变量/方法. 主要存在四种情形 1. 1. object # public 2. __object__ # special, python system ...

  8. python大牛 关东升_《Python从小白到大牛》第4章 Python语法基础

    本章主要为大家介绍Python的一些语法,其中包括标识符.关键字.常量.变量.表达式.语句.注释.模块和包等内容. 标识符和关键字 任何一种计算机语言都离不开标识符和关键字,因此下面将详细介绍Pyth ...

  9. python结束进程树_【python爬虫】线程进程

    关注:程序运行速度---->主要是由cpu(大脑)来决定. 想要提高程序的运行速度----->提高cpu利用率. 提高cpu的利用率由两种途径: 1.让cpu不休息.cpu每时每刻都在处理 ...

最新文章

  1. ICRA 2021 | VINS 研讨会概要(附完整视频)
  2. oracle Database 10g后:闪回表
  3. Apriori算法实例
  4. Make Games with Python Pygame (2)
  5. [云炬创业基础笔记] 第四章测试6
  6. Spring boot 自定义banner
  7. js各种方法继承以及优缺点
  8. .NET 产品版权保护方案 (.NET源码加密保护)
  9. matlab 锐化降噪,matlab 图形锐化 滤波
  10. python表达式352的值为_表达式 3 and 5 的值为
  11. 女生做产品经理好吗_谁说女生不适合做产品经理?
  12. Tensorflow Estimator之DNNClassifier
  13. shell脚本-监控系统资源并通过短信报警
  14. tarjan 算法模板
  15. openwrt nas_真牛气,矿渣蜗牛星际也能玩软路由Openwrt和NAS虚拟一体机
  16. php经典实例博客管理,PHP经典项目案例-(一)博客管理系统2
  17. 24道互联网大厂最爱问智商题汇总|你能答对几道?(附带答案、详解)
  18. Django 之 Models(Models 模型 数据表关系)
  19. 亿级用户百TB级数据的 AIOps 技术实践之路(增强版)
  20. 自动钉木箱机器人_一种全自动木箱钉装机的制作方法

热门文章

  1. git status或者 git commit 中文文件显示乱码
  2. drools 7.11.0.Final使用
  3. canal 入门(2)
  4. XP系统计算机桌面图标不见,XP系统显示桌面图标消失的解决方法
  5. 23种设计模式(3)-原型模式
  6. 【vue.config.js配置configureWebpack的optimization splitChunks页面空白 - DCloud】
  7. leetcode题解-买卖股票的最佳时机
  8. error C2086: “int WINGDIAPI”: 重定义
  9. 2017-2018-1 20155327 《信息安全系统设计基础》第7周学习总结
  10. JAXB 遇到的问题