python猴子补丁是什么_什么是Python的monkeypatch(猴子补丁)?

这个问题是有一次面试的时候问到的,当时确实第一次听说这个,单纯只是非常好奇,于是就去了解了一下。

在维基百科上找到了:我理解的就是Python的一种运行时的机制,简单来说就是这样一个效果。

>>> import math

>>> math.pi

3.141592653589793

>>> math.pi = 1

>>> math.pi

1

猴子补丁就是在运行过程中动态修改一个类的方法或变量等。monkey patching is used to:

Replace methods / classes / attributes / functions at runtime, e.g. to stub out a function during testing;

Modify/extend behaviour of a third-party product without maintaining a private copy of the source code;

Apply the result of a patch at runtime to the state in memory, instead of the source code on disk;

Distribute security or behavioural fixes that live alongside the original source code (an example of this would be distributing the fix as a plugin for the Ruby on Rails platform).

猴子补丁应用于:

在运行阶段替换方法/类别/属性/功能。

修改/扩展第三方产品的行为而不需要维护源代码的私有拷贝;

将运行时补丁的结果应用于内存中的状态,而不是磁盘上的源代码;

分发与原始源代码共存的安全性或行为修复程序(例如,将修复程序作为Ruby on Rails平台的插件分发)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值