博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的运行时方法修补
阅读量:2518 次
发布时间:2019-05-11

本文共 2943 字,大约阅读时间需要 9 分钟。

Often, when programming, we may want to change some already set behavior. This can be accomplished by sub-classing whatever classes we have and overriding those methods we are not happy with.

通常,在编程时,我们可能想要更改一些已经设置的行为。 这可以通过对我们拥有的所有类进行子类化并覆盖我们不满意的那些方法来实现。

While this works with our own code, what happens if we want to change third party code? Of course we can edit it so that it instantiates one of our sub-classes, this however, can create another whole set of problems. What we need to do then, is to figure out a way to replace an object’s methods with our own.

尽管这适用于我们自己的代码,但是如果我们想更改第三方代码会怎样? 当然,我们可以对其进行编辑,以使其实例化我们的一个子类,但是,这可能会带来另一整套问题。 然后,我们需要做的是找出一种用我们自己的方法替换对象方法的方法。

补习班 (Patching classes)

Probably the easiest way of adding a new method to an object or replacing an existing one is by patching its class. Say we want to teach our Dog class from the previous example how to howl, we can easily do it by defining a new howl function and adding it to our class like so:

向对象添加新方法或替换现有方法的最简单方法可能是修补其类。 假设我们想从前面的示例中教我们的Dog类如何to叫,我们可以通过定义一个新的howl函数并将其添加到我们的类中来轻松地做到这一点,如下所示:

While this is extremely easy to do, there’s a couple of things we should be aware of. First of all, all instances of the modified class will be updated, meaning that not only new objects will have the new method definitions, but that all objects we created before patching our class will have them too (unless they haven’t overridden the method themselves). Second, the new or modified methods will be bound, meaning that the first argument (i.e. ‘self’) will be the object being called.

尽管这非常容易做到,但是我们需要注意几件事。 首先,将修改类的所有实例,这意味着不仅新对象将具有新的方法定义,而且在修补类之前我们创建的所有对象也将具有它们(除非它们没有重写方法)他们自己)。 其次,将绑定新的或修改的方法,这意味着第一个参数(即“ self”)将成为被调用的对象。

修补对象 (Patching objects)

Individual objects can also be patched without having to affect all other instances of its class. There is, however, a little gotcha when doing it. Lets look at the following example:

也可以修补单个对象,而不必影响其类的所有其他实例。 但是,这样做时有一点陷阱。 让我们看下面的例子:

Now, let’s try calling our newly defined method:

现在,让我们尝试调用我们新定义的方法:

border_collie.herd(sheep)TypeError: herd() takes exactly 2 arguments (1 given)border_collie.herd(sheep)TypeError: herd() takes exactly 2 arguments (1 given)

The problem with the previous code is that herd is not a bound method, just take a look at the following code:

先前代码的问题在于, herd不是绑定方法,只需看下面的代码即可:

This means that the object being called is not passed as the first argument to the function, causing the error we previously saw. We can of course pass the instance ourselves, but that wouldn’t work when replacing methods. The correct way of patching an object is by using the MethodType function in the like so:

这意味着被调用的对象不会作为第一个参数传递给函数,从而导致我们先前看到的错误。 我们当然可以自己传递实例,但是在替换方法时不起作用。 修补对象的正确方法是使用的MethodType函数,如下所示:

print border_collie.herd
>print border_collie.herd
>

As we can see the method is now bound and we can safely call it.

如我们所见,该方法现已绑定,可以安全地调用它了。

结论 (Conclusion)

翻译自:

转载地址:http://ajqwd.baihongyu.com/

你可能感兴趣的文章
Ubuntu server搭建Java web服务器
查看>>
java读取xml配置文件和properties配置文件
查看>>
HDU 4300 Contest 1
查看>>
POJ 3311
查看>>
浅谈模块化
查看>>
14个免费访客行为分析工具
查看>>
(转)arguments.callee移除AS3匿名函数的侦听
查看>>
onNewIntent调用时机
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
高性能HTTP加速器Varnish(概念篇)
查看>>
Linux 如何写makefile文件
查看>>
PHPExcel 使用心得
查看>>
洛谷 P3374 【模板】树状数组 1(单点加,区间和)
查看>>
verilog 代码编写小记
查看>>
PyQT的安装和配置
查看>>
从 docker 到 runC
查看>>
守护进程
查看>>
php数组
查看>>
PHP curl登录 跳过验证码
查看>>
.NET json格式 使用Newtonsoft.Json.JsonConvert类 附读取文件方法
查看>>