python动态给对象或者类添加方法
参考:http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object
In Python, there is a difference between functions and bound methods.
>>>def foo():...print"foo"...>>>class A:...def bar( self ):...print"bar"...>>> a = A()>>> foo
<function foo at 0x00A98D70>>>> a.bar
<bound method A.bar of <__main__.A instance at 0x00A9BC88>>>>>
Bound methods have been "bound" (how descriptive) to an instance, and that instance will be passed as the first argument whenever the method is called.
Callables that are attributes of a class (as opposed to an instance) are still unbound, though, so you can modify the class definition whenever you want:
>>>def fooFighters( self ):...print"fooFighters"...>>> A.fooFighters = fooFighters
>>> a2 = A()>>> a2.fooFighters
<bound method A.fooFighters of <__main__.A instance at 0x00A9BEB8>>>>> a2.fooFighters()
fooFighters
Previously defined instances are updated as well (as long as they haven't overridden the attribute themselves):
>>> a.fooFighters()
fooFighters
The problem comes when you want to attach a method to a single instance:
>>>def barFighters( self ):...print"barFighters"...>>> a.barFighters = barFighters
>>> a.barFighters()Traceback(most recent call last):File"<stdin>", line 1,in<module>TypeError: barFighters() takes exactly 1 argument (0 given)
The function is not automatically bound when it's attached directly to an instance:
>>> a.barFighters
<function barFighters at 0x00A98EF0>
To bind it, we can use the MethodType function in the types module:
>>>import types
>>> a.barFighters = types.MethodType( barFighters, a )>>> a.barFighters
<bound method ?.barFighters of <__main__.A instance at 0x00A9BC88>>>>> a.barFighters()
barFighters
This time other instances of the class have not been affected:
>>> a2.barFighters()Traceback(most recent call last):File"<stdin>", line 1,in<module>AttributeError: A instance has no attribute 'barFighters'
More information can be found by reading about descriptors and metaclass programming.
python动态给对象或者类添加方法的更多相关文章
- python动态获取对象的属性和方法 (转载)
首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...
- python动态获取对象的属性和方法 (转)
转自未知,纯个人笔记使用 首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): ...
- python动态获取对象的属性和方法
http://blog.csdn.net/kenkywu/article/details/6822220首先通过一个例子来看一下本文中可能用到的对象和相关概念.01 #coding: UTF- ...
- python 动态调用模块、类、方法(django项目)
需求:近一段时间基于django框架,开发各业务层监控代码,每个业务的监控逻辑不同,因此需要开发监控子模块,动态的导入调用. 项目名称:demo_django App:common_base.moni ...
- C#动态创建和动态使用程序集、类、方法、字段等
C#动态创建和动态使用程序集.类.方法.字段等 分类:技术交流 (3204) (3) 首先需要知道动态创建这些类型是使用的一些什么技术呢?其实只要相关动态加载程序集呀,类呀,都是使用反射,那么动 ...
- 运行过程中给类添加方法 types.MethodType
class Person(object): def __init__(self,name = None,age = None): self.name = name#类中拥有的属性 self.age = ...
- [19/10/14-星期一] Python中的对象和类
一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...
- C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)
C#中的扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...
- 调用其他python脚本文件里面的类和方法
问题描述: 自己编写了若干个Python脚本. 在testC.py里面需要调用testA.py和testB.py里面的若干类和方法.要怎么办? 需要都打包.安装,再去调用吗? 其实不必那么麻烦. 这里 ...
随机推荐
- 深入了解JavaScript中的Symbol的使用方法
这篇文章主要介绍了深入了解JavaScript中的Symbol的使用方法,本文针对ES6版本的JS进行讲解,需要的朋友可以参考下 Symbol 是什么? Symbols 不是图标,也不是指在代码中可以 ...
- Windows 10上强制Visual Studio 2017 以管理员身份运行
1. 打开VS的安装目录,找到devenv.exe,右键,选择“兼容性疑难解答”. 2. 选择“疑难解答程序” 3. 选择“该程序需要附加权限” 4. 确认用户帐户控制后,点击测试程序,不然这个对话框 ...
- flask学习(八):页面跳转和重定向
1. 用处:在用户访问一些需要登录的页面的时候,如果用户没有登录,那么让页面重定向到登录页面 2. 实例 运行效果: 用户已登录,进入发布问答页面 用户未登录,跳转到登录页面
- Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges
http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...
- 1-25-循环控制符break、continue和函数详解
大纲: 1-for循环补充 1-1-for循环实战---类C格式应用 2-break.continue循环控制符 2-1实战:帮助理解break.continue作用 3-函数详解 3-1.脚本文件中 ...
- C# ASP.NET 验证码
使用C# ASP.NET 获取 验证码的代码书写 一般都采用异步 点击 前台验证码图片 请求一次 : 前台图片代码: <img id="imgvalidatecode" sr ...
- 学习写domready
原视频参考http://www.imooc.com/learn/488 --博主个人尝试学习写的-- /** * Created by ty on 2016/1/3. */ //尝试自己写domrea ...
- mifi随身wifi选购
一款优秀的随身wifi不光要信号好,更要电量足 ,网速快.影响这个三个问题的主要因素就是cpu.so咱们从cpu的角度来分析下mifi 机器型号(cpu型号) TP 961 52000 (MDM962 ...
- MoreEffectiveC++Item35 条款25 将constructor和non-member functions虚化
1.virtual constructor 在语法上是不可将构造函数声明成虚函数,虚函数用于实现"因类型而异的行为",也就是根据指针或引用所绑定对象的动态类型而调用不同实体.现在所 ...
- 20165202 2017-2018-2 《Java程序设计》第8周学习总结
20165202 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 Ch12 进程与线程 线程是比进程更小的单位,一个进程在其执行过程中,可以产生多个线程 Ja ...