解析Python类中的方法定义
来源:互联网 发布:网络打字员招聘 编辑:程序博客网 时间:2024/06/05 20:40
最近在学习类过程中,绑定方法这个概念没有理解透彻,所以在网上找了很多相关博客、文章研究到底是怎么一回事。因为有的文章所陈述与我在python3.5版本实际实验中有些出入,所以经过实践后总结出以下结论。
对于Python类中,方法的定义方式,我们可以归纳有4种类型:
1、不带self、cls参数且不加装饰器(staticmethod、classmethod)
2、正常的方法定义,带self参数
3、类方法:加装饰器(classmethod)
4、静态方法:加装饰器(staticmethod)
下面对每一种定义的方式进行分析:
1、不带self、cls参数且不加装饰器(staticmethod、classmethod)
定义代码如下:
class Student(object): def func(name): print('my name is {}'.format(name))
我们通过用类和实例分别调用该方法:
print(Student.func)Student.func('Tom')print(Student().func)Student().func('Tom')
类调用output:
<function Student.func at 0x0000000000D7D268>my name is Tom[Finished in 0.1s]
实例调用output:
<bound method Student.func of <__main__.Student object at 0x0000000000D444A8>>Traceback (most recent call last): File "I:\MyProject\awesome-python3-webapp\www\for_test.py", line 11, in <module> Student().func('Tom')TypeError: func() takes 1 positional argument but 2 were given[Finished in 0.2s with exit code 1]
从上面的输出我们可以看出,此种定义方法,如果用类来调用该方法,那么这个方法就只是一个函数,不会像绑定方法一样会自动传值。传参只需按照正常的函数传参即可;如果用实例来调用该方法,那么这个方法就是绑定(bound)的方法,既然是绑定方法,那么就会自动把Student()
自身作为第一个参数传到方法中去,所以Student().func('Tom')
会传实例Student()
以及'Tom'
两个参数到此方法,但是由于此方法定义时只定义了一个参数,所以就会有上述的报错。
2、正常的方法定义,带self参数
定义代码如下:
class Student(object): def func(self, name): print('my name is {}'.format(name))
类调用该方法:
print(Student.func)Student.func('Tom')
output:
<function Student.func at 0x000000000070D268>Traceback (most recent call last): File "I:\MyProject\awesome-python3-webapp\www\for_test.py", line 8, in <module> Student.func('Tom')TypeError: func() missing 1 required positional argument: 'name'[Finished in 0.2s with exit code 1]
用类调用该方法,跟第一种情况一样,依然是一个函数。因为此函数有两参数(self
和name
),由于实参'Tom'
是传到了形参self
,所以输出中提示调用func
方法少了name
参数。正确的调用方式为Student.func(Student(), 'Tom')
。
用实例调用该方法:
print(Student().func)Student().func('Tom')
output:
<bound method Student.func of <__main__.Student object at 0x00000000006E44A8>>my name is Tom[Finished in 0.2s]
可以看出实例调用该方法,得到是绑定的方法,Student()
和Tom
参数,分别传给形参self
和name
。
其实第一种和第二种定义的方法是一样的,而他们的区别只是有没有self
这个参数。只要是实例调用这个方法,都是绑定的方法。都会自动将实例自身作为第一个参数传递进去。self
这个参数,是大家约定俗成的一种参数命名,命名成a
或者b
都是可以的,只不过这样命名会降低代码的可读性。下面要说的类方法中的cls
参数也是这个道理。
3、类方法:加装饰器(classmethod)
定义代码如下:
class Student(object): @classmethod def func(cls, name): print('my name is {} from {}'.format(name, cls.__name__))
用类和实例分别调用该方法:
print(Student.func)Student.func('Tom')print(Student().func)Student().func('Tom')
output:
<bound method Student.func of <class '__main__.Student'>>my name is Tom from Student<bound method Student.func of <class '__main__.Student'>>my name is Tom from Student[Finished in 0.2s]
可以看出,此种定义方式,用类或者实例调用,都是绑定的方法。用类调用,会将类自身作为第一个参数传递到方法中。用实例调用,会将实例所属的类作为第一个参数传递到方法中。
4、静态方法:加装饰器(staticmethod)
定义代码如下:
class Student(object): @staticmethod def func(name): print('my name is {}'.format(name))
用类和实例分别调用该方法:
print(Student.func)Student.func('Tom')print(Student().func)Student().func('Tom')
output:
<function Student.func at 0x0000000000D6D268>my name is Tom<function Student.func at 0x0000000000D6D268>my name is Tom[Finished in 0.2s]
从上面的输出代码中,可以看出,使用装饰器staticmethod装饰的函数,只是一个普通函数,没有绑定方法的自动传值功能,传参只需按照正常的函数传参即可。
综合上面分析,我们可以总结如下:
1、凡是类中的方法、函数,如果没有加装饰器,当通过类来调用的时候,得到的是普通的函数,当通过实例来调用的时候,得到的是绑定方法;
2、加装饰器classmethod的方法,无论是通过类或者实例来调用,得到的都是绑定方法,python会自动将类本身或者实例所以的类作为第一个参数传递进去;
3、加装饰器staticmethod的方法,通过类或者实例调用,得到的都是i普通函数。
- 解析Python类中的方法定义
- Python 定义类中的方法
- Python类定义解析
- Python定义类方法
- python学习:类中的方法的定义和调用
- Python 3.6 定义类中的方法和属性
- python类中定义方法
- python中定义类方法
- python中定义类方法
- Java类中的方法定义
- python类中的方法
- Python的方法定义
- python方法参数定义
- python定义静态方法
- pascal中的定义方法
- 定义Java中的方法
- 定义 Java 中的方法
- Java中的方法定义
- MySQL与Oracle的区别
- JS THIS 深入浅出 JavaScript 中的 this && 理解javascript函数调用和 this ** Javascript 普通函数和构造函数的区别
- iOS-动态库与静态库
- neo4j java 简单调用生成图
- dubbo整合springboot入门案例
- 解析Python类中的方法定义
- HTML全文转化为PDF技术选型研究与流行方法汇总
- build-essential的作用
- Java IO 复制文本文件_2
- 设计模式--单利模式
- <C++ Primer_5th>习题_1.3
- iOS-Demo实例大全
- MFC中 给按钮添加图片的方法
- iOS Runtime 几种基本用法简记