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里面的若干类和方法.要怎么办? 需要都打包.安装,再去调用吗? 其实不必那么麻烦. 这里 ...
随机推荐
- 【Python】模块学习之使用paramiko连接Linux,远程执行命令,上传下载、文件
本文主要介绍paramiko远程执行linux命令,及在服务器上进行文件的上传.下载 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. ...
- python爬虫之下载京东页面图片
import requests from bs4 import BeautifulSoup import time import re t = 0 #用于给图片命名 for i in range(10 ...
- C++(二十五) — 类的封装、实现
1.类的封装.实现.对象的定义及使用 (1)类是一组对象的抽象化模型.类对象将数据及函数操作集合在一个实体中,只需要接口,而不需要知道具体的操作. 隐藏细节,模型化: 类内自由修改: 减少耦合,相当于 ...
- Python打包分发工具setuptools简介(转)
作为Python标准的打包及分发工具,setuptools可以说相当地简单易用.它会随着Python一起安装在你的机器上.你只需写一个简短的setup.py安装文件,就可以将你的Python应用打包. ...
- NOIP2018小反思
今天下午做了一道叫邮票 Stamps的题.敲代码的时候就发现,好像和去年D1T2货币系统有点像,原理都是一个完全背包DP.做完之后交上去发现有几个点RE了,于是马上把数组改大,AC. 我赶忙找到去年那 ...
- HDU-4714-贪心
Tree2cycle Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Tot ...
- UVA-10972 RevolC FaeLoN (边双连通+缩点)
题目大意:将n个点,m条边的无向图变成强连通图,最少需要加几条有向边. 题目分析:所谓强连通,就是无向图中任意两点可互达.找出所有的边连通分量,每一个边连通分量都是强连通的,那么缩点得到bcc图,只需 ...
- yii2手动添加图片处理插件Imagine
1.首先从官网下载yii2-imagine的拓展 下载地址:https://github.com/yiisoft/yii2-imagine 下载包名称:yii2-imagine-master 2.然后 ...
- log4j2 输入日志到flume
最近想将服务的运行日志收集起来,首先了解到flume技术栈 采用flume方案定了之后有两种方式实现 1: 在应用中,log4j2直接发送日志信息到flume , 2: 通过监控log4j2 产生的日 ...
- Java中String两种不同创建方式的区别及intern的用法
一, Java有两种创建字符串的方式, String str1 = "abc"; String str2 = new String("abc"); 用双引号创建 ...