建立使用者定義的方法物件
獲取類的屬性(可能通過該類的例項),如果該屬性是使用者定義的函式物件,未繫結的使用者定義的方法物件或類方法物件,則可以建立使用者定義的方法物件。
class A(object):
# func: A user-defined function object
#
# Note that func is a function object when it's defined,
# and an unbound method object when it's retrieved.
def func(self):
pass
# classMethod: A class method
@classmethod
def classMethod(self):
pass
class B(object):
# unboundMeth: A unbound user-defined method object
#
# Parent.func is an unbound user-defined method object here,
# because it's retrieved.
unboundMeth = A.func
a = A()
b = B()
print A.func
# output: <unbound method A.func>
print a.func
# output: <bound method A.func of <__main__.A object at 0x10e9ab910>>
print B.unboundMeth
# output: <unbound method A.func>
print b.unboundMeth
# output: <unbound method A.func>
print A.classMethod
# output: <bound method type.classMethod of <class '__main__.A'>>
print a.classMethod
# output: <bound method type.classMethod of <class '__main__.A'>>
當屬性是使用者定義的方法物件時,只有在從中檢索它的類與儲存在原始方法物件中的類相同或派生類時,才會建立新的方法物件; 否則,原始方法物件按原樣使用。
# Parent: The class stored in the original method object
class Parent(object):
# func: The underlying function of original method object
def func(self):
pass
func2 = func
# Child: A derived class of Parent
class Child(Parent):
func = Parent.func
# AnotherClass: A different class, neither subclasses nor subclassed
class AnotherClass(object):
func = Parent.func
print Parent.func is Parent.func # False, new object created
print Parent.func2 is Parent.func2 # False, new object created
print Child.func is Child.func # False, new object created
print AnotherClass.func is AnotherClass.func # True, original object used