关于Python的函数(Method)与方法(Function)
先上结论:
- 函数(function)是Python中一个可调用对象(callable), 方法(method)是一种特殊的函数。
- 一个可调用对象是方法和函数,和这个对象无关,仅和这个对象是否与类或实例绑定有关(bound method)。
- 实例方法,在类中未和类绑定,是函数;在实例中,此实例方法与实例绑定,即变成方法。
- 静态方法没有和任何类或实例绑定,所以静态方法是个函数。
- 装饰器不会改变被装饰函数或方法的类型。
- 类实现__call__方法,其实例也不会变成方法或函数,依旧是类的实例。
- 使用callalble() 只能判断对象是否可调用,不能判断是不是函数或方法。
- 判断对象是函数或方法应该使用type(obj)。
下面,使用一些例子,对上述结论进行检测、验证。
测试的例子中,我们创建一个装饰器、一个函数及一个类,这个类包含:实例方法、类方法、静态方法及被装饰器装饰的方法。
完整代码: https://github.com/blackmatrix7/python-learning/blob/master/function_/method_func.py
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'>
关于Python的函数(Method)与方法(Function)的更多相关文章
- python enumerate() 函数的使用方法
列表是最常用的Python数据类型,前段时间看书的时候,发现了enumerate() 函数非常实用,因为才知道下标可以这么容易的使用,总结一下. class enumerate(object): &q ...
- python spilt()函数的使用方法
Python中的split()函数的用法 Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后 ...
- python第十九天 关于方法,函数
1.先从简单的函数说起 from inspect import isfunction 导入判断是否是function def foo():pass 定义了一个函数 print(foo) <fun ...
- Python进阶----反射(四个方法),函数vs方法(模块types 与 instance()方法校验 ),双下方法的研究
Python进阶----反射(四个方法),函数vs方法(模块types 与 instance()方法校验 ),双下方法的研究 一丶反射 什么是反射: 反射的概念是由Smith在1982年首次提出的 ...
- python中函数和方法区别,以及如何给python类动态绑定方法和属性(涉及types.MethodType()和__slots__)
网上有很多同义但不同方式的说法,下面的这个说法比较让你容易理解和接受 与类和实例无绑定关系的function都属于函数(function): 与类和实例有绑定关系的function都属于方法(meth ...
- Python的程序结构[1] -> 方法/Method[2] -> 魔术方法 __init__ / __del__ / __new__
魔术方法 / Magic Method 魔法方法就是可以给你的类增加魔力的特殊方法(实质应称为特殊方法,魔术方法在JavaScript中有所体现,对象具有不透明特性,而且无法在自定义对象中模拟这些行为 ...
- python isinstance和issubclass,区分方法和函数,反射
一.isinstance和issubclass 1.isinstance class Animal: def eat(self): print('刚睡醒吃点儿东西') class Cat(Animal ...
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- 举例详解Python中的split()函数的使用方法
这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:sp ...
随机推荐
- 对datatable进行简单的操作
筛选出datatable中c_level=1的数据 dataRow[] rows = dt.Select("c_level=0"); 克隆表dt的结构到表dt,并将dt的数据复制到 ...
- hdu5304 Eastest Magical Day Seep Group's Summer 状压dp+生成树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5304 16个点的无向图,问能生成多少个n条边的连通图.(即多一条边的树) 先n^3 * 2^n 枚举全部的 ...
- com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user
1.错误描写叙述 553 Mail from must equal authorized user com.sun.mail.smtp.SMTPSendFailedException: 553 Mai ...
- hibernate学习笔记之中的一个(JDBC回想-ORM规范)
JDBC回想-ORM规范 JDBC操作步骤 注冊数据库驱动 Class.forName("JDBCDriverClass") 数据库 驱动程序类 来源 Access sun.jdb ...
- as 与 is
在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS:在游标(CURSOR)中只能用IS.
- inline-block解决
一.现象描述 真正意义上的inline-block水平呈现的元素间,换行显示或空格分隔的情况下会有间距 二.方法之移除空格 元素间留白间距出现的原因就是标签段之间的空格,因此,去掉HTML中的空格,自 ...
- 一个简单的java网络通信例子
先建立2个项目,分别是请求端和响应端,端口改成不一样的就行,比如请求端是8080,响应端是8081 废话不多说,直接上代码 请求端的Controller层 @GetMapping("/req ...
- VirtualBox 安装 Ubuntu 14.04 无法调节分辨率问题
基础环境 宿主系统:Windows 10 虚拟机系统:Ubuntu 14.04-32bit.Ubuntu 14.04-64bit VirtualBox:5.2.0 r118431 (Qt5.6.2) ...
- mybatis 整合spring之mapperLocations配置的问题
今天尝试spring整合mybatis时遇到这么一个问题,就是在配置sqlSessionFactory时是否要配置mapperLocations的问题. <bean id="sessi ...
- eclipse中JPA插件的安装与使用
说明 上周实验室学习了数据库设计相关的内容,其中涉及到将数据库实体化的问题,JPA是一个很好的实现工具,便开始着手于JPA的学习.因为JPA涉及到的知识还是挺多的,需要学习许多新的知识,所以对于JPA ...