【Python】实例方法、类方法、静态方法之间的区别
2018-01-08 09:40
691 查看
在python中,方法分为三类实例、类、静态方法。代码如下:
class Test(object):
def InstanceFun(self):
print("InstanceFun");
print(self);
@classmethod
def ClassFun(cls):
print("ClassFun");
print(cls);
@staticmethod
def StaticFun():
print("StaticFun");
t = Test();
t.InstanceFun(); # 输出InstanceFun,打印对象内存地址“<__main__.Test object at 0x0293DCF0>”
Test.
4000
ClassFun(); # 输出ClassFun,打印类位置 <class '__main__.Test'>
Test.StaticFun(); # 输出StaticFun
t.StaticFun(); # 输出StaticFun
t.ClassFun(); # 输出ClassFun,打印类位置 <class '__main__.Test'>
Test.InstanceFun(); # 错误,TypeError: unbound method instanceFun() must be called with Test instance as first argument
Test.InstanceFun(t); # 输出InstanceFun,打印对象内存地址“<__main__.Test object at 0x0293DCF0>”
t.ClassFun(Test); # 错误 classFun() takes exactly 1 argument (2 given)
可以看到,在python中,两种方法的主要区别在于参数。实例方法隐含的参数为类实例self,而类方法隐含的参数为类本身cls。
静态方法无隐含参数,主要为了类实例也可以直接调用静态方法。
所以逻辑上类方法应当只被类调用,实例方法实例调用,静态方法两者都能调用。主要区别在于参数传递上的区别,实例方法悄悄传递的是self引用作为参数,而类方法悄悄传递的是cls引用作为参数。
python实现了一定的灵活性使得类方法和静态方法,都能够被实例和类二者调用。
实例能调用所有方法,类只能调用类和静态方法。
class Test(object):
def InstanceFun(self):
print("InstanceFun");
print(self);
@classmethod
def ClassFun(cls):
print("ClassFun");
print(cls);
@staticmethod
def StaticFun():
print("StaticFun");
t = Test();
t.InstanceFun(); # 输出InstanceFun,打印对象内存地址“<__main__.Test object at 0x0293DCF0>”
Test.
4000
ClassFun(); # 输出ClassFun,打印类位置 <class '__main__.Test'>
Test.StaticFun(); # 输出StaticFun
t.StaticFun(); # 输出StaticFun
t.ClassFun(); # 输出ClassFun,打印类位置 <class '__main__.Test'>
Test.InstanceFun(); # 错误,TypeError: unbound method instanceFun() must be called with Test instance as first argument
Test.InstanceFun(t); # 输出InstanceFun,打印对象内存地址“<__main__.Test object at 0x0293DCF0>”
t.ClassFun(Test); # 错误 classFun() takes exactly 1 argument (2 given)
可以看到,在python中,两种方法的主要区别在于参数。实例方法隐含的参数为类实例self,而类方法隐含的参数为类本身cls。
静态方法无隐含参数,主要为了类实例也可以直接调用静态方法。
所以逻辑上类方法应当只被类调用,实例方法实例调用,静态方法两者都能调用。主要区别在于参数传递上的区别,实例方法悄悄传递的是self引用作为参数,而类方法悄悄传递的是cls引用作为参数。
python实现了一定的灵活性使得类方法和静态方法,都能够被实例和类二者调用。
实例能调用所有方法,类只能调用类和静态方法。
相关文章推荐
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python2.7 以及 Python 3.5的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python——class的基础知识(Python的实例方法,类方法,静态方法之间的区别及调用关系)
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- 【Python】实例方法、类方法、静态方法之间区别(转)
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- python中类方法、类实例方法、静态方法的使用与区别
- Python 实例方法、类方法、静态方法的区别与作用
- python 类实例方法,类方法和类静态方法的区别
- python中类方法、类实例方法、静态方法的使用与区别
- Python类中实例方法、类方法及静态方法的调用区别
- python中类方法、类实例方法、静态方法的使用与区别
- Python中实例方法,静态方法,类方法的区别详解
- python中类方法、类实例方法、静态方法的使用与区别
- Python的类方法,静态方法,实例方法的区别
- [Python] 类方法、实例方法和静态方法的定义方式与区别
- python中类方法、类实例方法、静态方法的使用与区别