美文网首页Python
types.MethodType()

types.MethodType()

作者: yeathMe | 来源:发表于2018-01-05 14:19 被阅读0次

当我们定义一个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参数,那么可以使用typesMethodType() 方法实现

-----------------

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

anotherRef=types.MethodType(methodName,instance) 其中第一个参数为一个方法名称, 第二个参数是一个实例对象,表示将 method 绑定到instance对象上,是的当method被调用的时候自动传入instance对象作为self参数。方法的返回值是一个method对象anotherRef,我们可以使用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方法,所以出现错误。

相关文章

  • types.MethodType()

    当我们定义一个class 的时候可以给这个类定义实例属性,包括实例方法,实例方法的特点在于 当实例方法被调用的时候...

  • python对象绑定属相方法、生成器

    1、python动态添加属性方法的方法, import types types.MethodType(属性方法,i...

网友评论

    本文标题:types.MethodType()

    本文链接:https://www.haomeiwen.com/subject/djxsnxtx.html