Python2.7 以及 Python 3.5的实例方法,类方法,静态方法之间的区别及调用关系
2017-12-05 20:50
1031 查看
今天很好奇Python2.7 和Python 3.5 的实例方法、类方法、静态方法之间的
区别与联系。所以就做了两个小实验来测验一下
Python3.5及以上
类的定义
class Test(): def instanceMethod(self): # 实例方法,必须传入自身实例(self)作为参数 print("I am the instancemethold,because I have the parmeter self") @classmethod def classMethod(cls): # 类方法,传入类实例 print("I am the classMethod,because I have the @ modifier ,and my paramter has cls") @staticmethod def staticMethod(): # 静态方法 print("I am the staticMethod,because I have the @ modifier ") def normalMethod(): # 普通方法 print("I am a noemal method Because I have nothing")
测验结果:
一、实例对各种方法的调用:
t=Test() t.instanceMethod() t.classMethod() t.staticMethod() t.normalMethod() # 输出 I am the instancemethold,because I have the parmeter self I am the classMethod,because I have the "@ modifier" ,and my paramter has cls I am the staticMethod,because I have the @ modifier Traceback (most recent call last): File "D:/Python/test.py", line 22, in < 4000 ;module> t.normalMethod() TypeError: normalMethod() takes 0 positional arguments but 1 was given
二、类对各种方法的调用:
t=Test() Test.instanceMethod(t) Test.classMethod() Test.staticMethod() Test.normalMethod() # 输出 I am the instancemethold,because I have the parmeter self I am the classMethod,because I have the "@ modifier" ,and my paramter has cls I am the staticMethod,because I have the @ modifier I am a normal method Because I have nothing
Python2.7中:
类的定义:
class Test(): def instanceMethod(self): # 实例方法,必须传入自身实例(self)作为参数 print("I am the instancemethold,because I have the parmeter self") @classmethod def classMethod(cls): # 类方法,传入类实例 print("I am the classMethod,because I have the @ modifier ,and my paramter has cls") @staticmethod def staticMethod(): # 静态方法 print(" am the staticMethod,because I have the @ modifier ") def normalMethod(): # 普通方法 print("I am a noemal method Because I have nothing")
一、实例对各种方法的调用:
========================= RESTART: D:\Python\test.py ========================= >>> t=Test() >>> t.instanceMethod <bound method Test.instanceMethod of <__main__.Test instance at 0x02BCC738>> >>> t.instanceMethod() # 实例可以调用实例方法,self为默认参数,不用传 I am the instancemethold,because I have the parmeter self >>> t.classMethod <bound method classobj.classMethod of <class __main__.Test at 0x004FB378>> >>> t.classMethod() # 实例可以调用类方法 I am the classMethod,because I have the @ modifier ,and my paramter has cls >>> t.staticMethod <function staticMethod at 0x02C09D30> >>> t.staticMethod() # 实例可以调用静态方法 am the staticMethod,because I have the @ modifier >>> t.normalMethod <bound method Test.normalMethod of <__main__.Test instance at 0x02BCC738>> >>> t.normalMethod() # 实例不可以调用普通方法 Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> t.normalMethod() TypeError: normalMethod() takes no arguments (1 given) >>>
二、类对各种方法的调用:
========================= RESTART: D:\Python\test.py ========================= >>> Test.instanceMethod() Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> Test.instanceMethod() TypeError: unbound method instanceMethod() must be called with Test instance as first argument (got nothing instead) >>> Test.instanceMethod(t) # 类调用实例方法需传入实例对象作为参数 I am the instancemethold,because I have the parmeter self >>> Test.classMethod() # 类调用类方法 I am the classMethod,because I have the @ modifier ,and my paramter has cls >>> Test.staticMethod <function staticMethod at 0x02C09D30> >>> Test.staticMethod() # 类调用静态方法 am the staticMethod,because I have the @ modifier >>> Test.normalMethod <unbound method Test.normalMethod> >>> Test.normalMethod() #类不能调用普通方法 Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> Test.normalMethod() TypeError: unbound method normalMethod() must be called with Test instance as first argument (got nothing instead) >>>
总结 :
以上实验说明,实例可以访问实例方法、类方法、静态方法 ,普通方法不能访问;类可以访问实例方法(必须传入类的实例作为参数)、类方法、静态方法,普通方法不能访问。实例可以访问实例方法也可以访问类方法,类可以访问类方法也可以访问实例方法,但是类方法在访问实例方法的时候必须传入类的实例作为参数,类可以理解为实例的一种。类本身可以访问函数,但是实例确不可以。
但是在Python2.7中类不能调用普通函数,在Python3.5h中类方法可以调用普通函数,具体体现在
Test.normalMethod()的使用当中。
相关文章推荐
- Python——class的基础知识(Python的实例方法,类方法,静态方法之间的区别及调用关系)
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- Python的实例方法,类方法,静态方法之间的区别及调用关系
- 【Python】实例方法、类方法、静态方法之间的区别
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- 【Python】实例方法、类方法、静态方法之间区别(转)
- 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法
- Python类中实例方法、类方法及静态方法的调用区别
- python 中的实例方法,静态方法以及类方法
- 关于python的类方法、实例方法和静态方法区别
- python中类方法、类实例方法、静态方法的使用与区别
- python中类方法、类实例方法、静态方法的使用与区别
- java线程基础巩固---同步代码块以及同步方法之间的区别和关系
- python中实例方法、类方法、静态方法的区别
- Python中类、实例、方法之间的关系
- Python中类、实例和类方法、静态方法、实例方法的关系