type python3_Python3 types.MethodType()

阅读:

217

当我们定义一个class 的时候可以给这个类定义实例属性,包括实例方法,实例方法的特点在于 当实例方法被调用的时候Python对象会自动作为self参数传入到方法的内部。

如果我们在模块中定义一个方法,而不是在类中定义一个方法,那么此时这个方法并不是对象的实例方法,但是我们可以将这个放法作为属性赋值给对象的 某一个属性。

class Person(object):

def __init___(self,newName, newAge):

self.name=newName

self.age=newAge

def run(self):

print" this is run method ",self

person=Person()

#将模块属性run赋值给对象属性

person.run =run

person .run("paramter")

但是在调用run方法 的时候person对象不会作为self参数传递到run方法内部。原因是run属性对应的方法并不是person对象的属性。

如果我们想让一个模块方法在调用的时候自动传入被调用对象作为self参数,那么可以使用types.MethodType() 方法实现。

因为方法也是一个属性,所以,它也可以动态地添加到实例上,只是需要用 types.MethodType() 把一个函数变为方法。

anotherRef=types.MethodType(methodName,instance,cls) 其中第一个参数为一个方法名称; 第二个参数是一个实例对象,表示将 method 绑定到instance对象上,当method被调用的时候自动传入instance对象作为self参数;第三个参数是实例对象所属的类;方法的返回值是一个method对象,我们可以使用anotherRef引用调用方法,也就是anotherRef(),当anotherRef方法执行的时候实际上就是在执行methodName对应的方法,此时instance会作为methodName的默认参数传入到methodName中。

import types

def fn_get_grade(self):

if self.score >= 80:

return 'A'

if self.score >= 60:

return 'B'

return 'C'

class Person(object):

def __init__(self, name, score):

self.name = name

self.score = score

下面我们将fn_get_grade()方法添加到实例上:

>>> p1 = Person('Bob', 90)

>>> p1.get_grade = types.MethodType(fn_get_grade, p1, Person)

>>>print p1.get_grade()

A

>>> p2 = Person('Alice', 65)

>>>print p2.get_grade()

# ERROR: AttributeError: 'Person' object has no attribute 'get_grade'

因p2实例没有绑定get_grade方法,所以出现错误。

Python3 types.MethodType()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值