关于Python的函数(Method)与⽅法(Function)先上结论:实例化类和实例化对象
1. 函数(function)是Python中⼀个可调⽤对象(callable), ⽅法(method)是⼀种特殊的函数。
2. ⼀个可调⽤对象是⽅法和函数,和这个对象⽆关,仅和这个对象是否与类或实例绑定有关(bound method)。
3. 实例⽅法,在类中未和类绑定,是函数;在实例中,此实例⽅法与实例绑定,即变成⽅法。
4. 静态⽅法没有和任何类或实例绑定,所以静态⽅法是个函数。
5. 装饰器不会改变被装饰函数或⽅法的类型。
6. 类实现__call__⽅法,其实例也不会变成⽅法或函数,依旧是类的实例。
7. 使⽤callalble() 只能判断对象是否可调⽤,不能判断是不是函数或⽅法。
8. 判断对象是函数或⽅法应该使⽤type(obj)。
下⾯,使⽤⼀些例⼦,对上述结论进⾏检测、验证。
测试的例⼦中,我们创建⼀个装饰器、⼀个函数及⼀个类,这个类包含:实例⽅法、类⽅法、静态⽅法及被装饰器装饰的⽅法。完整代码:
def test_decorator(func):
"""
装饰器,测试使⽤,⽆功能
:param func:
:return:
"""
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def the_function():
"""
函数
:return:
"""
pass
class TheClass:
def__call__(self, *args, **kwargs):
return self
@classmethod
def class_method(cls):
"""
类⽅法
:return:
"""
pass
def instance_method(self):
"""
实例⽅法
"""
return self
@staticmethod
def static_method():
"""
静态⽅法
:return:
"""
pass
@test_decorator
def decorated_func(self):
pass
先对类⽅法和实例⽅法的类型进⾏检测(注释部分为输出结果,下同)。
从运⾏结果上看,类⽅法和实例⽅法都是⽅法(method)。
同时,通过直接打印类⽅法和实例⽅法,可以得知它们都是绑定⽅法(bound method)。
print('class_method type {type} '.format(type=type(TheClass.class_method)))
# class_method type <class_ 'method'>
print('instance_method type {type} '.format(type=type(the_class.instance_method)))
# instance_method type <class_ 'method'>
print(TheClass.class_method)
# <bound method TheClass.class_method of <class '__main__.TheClass'>>
print(the_class.instance_method)
# <bound method TheClass.instance_method of <__main__.TheClass object at 0x00000275DEB3FC50>>
如果仅通过上述运⾏结果,就得出类⽅法和实例⽅法都是⽅法,那么就错了。
再看下⾯的代码,同⼀个对象instance_method,之前还是⽅法(method),现在已经变成函数(function)。
print('instance_method type {type} '.format(type=type(TheClass.instance_method)))
# instance_method type <class 'function'>
print(TheClass.instance_method)
# <function TheClass.instance_method at 0x00000275DEB3D840>
第⼆段代码,和第⼀段代码的不同之处:第⼀段代码是通过实例,去访问实例⽅法;⽽第⼆段代码,是通过类去访问实例⽅法。
同⼀个可调⽤对象,仅仅是访问的⽅式不同,就能从⽅法变为函数。
因为,在类中的实例⽅法,并没有和类建⽴绑定关系,所以它是⽅法。当类进⾏实例化时,会将实例⽅法,绑定到类创建出的实例上,此时实例⽅法与实例形成绑定关系,从函数变为⽅法。
所以,可以得到开头的第2、3条结论:
⼀个可调⽤对象是⽅法和函数,和这个对象⽆关,仅和这个对象是否与类或实例绑定有关(bound method)。
实例⽅法,在类中未和类绑定,是函数;在实例中,此实例⽅法与实例绑定,即变成⽅法。
接着对静态⽅法进⾏检测,有了之前的结论,就很容易理解为什么静态⽅法是函数⽽不是⽅法:因为它不会和类或实例进⾏绑定。
print('static_method type {type} '.format(type=type(the_class.static_method)))
# static_method type <class_ 'function_'>
print('static_method type {type} '.format(type=type(TheClass.static_method)))
# static_method type <class 'function'>
print(TheClass.static_method, the_class.static_method, sep='\n')
# <function TheClass.static_method at 0x0000024BC5EAD950>
# <function TheClass.static_method at 0x0000024BC5EAD950>
⽽对于⼀个函数,因为不会和任何类或实例绑定(除⾮使⽤MethodType将函数绑定到某个实例上),必然不是⽅法。
print('the_function type {type} '.format(type=type(the_function)))
# the_function type <class_ 'function_'>
对于装饰器,本⾝也不会改变被装饰对象的类型
# 装饰器本⾝也是个函数
print('test_decorator type {type} '.format(type=type(test_decorator)))
# test_decorator type <class_ 'function_'>
# 将装饰器装饰器到实例⽅法上
# 检查被装饰的⽅法的类型
print('decorated_func type {type} '.format(type=type(the_class.decorated_func)))
# decorated_func type <class_ 'method'>
# 从测试结果得知,装饰器不会影响被装饰⽅法或函数的类型
如果⼀个类,实现__call__⽅法,那么其实例会变为可调⽤对象,但这个这个实例依旧不是函数或⽅法
# 如果类实现__call__⽅法
# 执⾏结果True 其实例变为可调⽤对象
print('class_instance callable {callable} '.format(callable=callable(the_class)))
# 实例的类型依旧是这个类,⽽不会变成函数或⽅法
print('class_instance type {type} '.format(type=type(the_class)))
# class_instance type <class_ '__main__.TheClass'>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。