今天很好奇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 <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()的使用当中。