
Python静态函数和普通方法的区别
下面通过一个例子,讲解Python静态函数、普通方法、类方法的区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # -*-coding:utf-8-*-
# 普通方法,类方法,静态方法的区别
__metaclass__ = type
class Tst:
name = 'tst'
data = 'this is data'
# 普通方法
def normalMethod(self, name):
print self.data, name
# 类方法,可以访问类属性
@classmethod
def classMethod(cls, name):
print cls.data, name
# 静态方法,不可以访问类属性
@staticmethod
def staticMethod(name):
print name
|
三种方法都可以通过实例来调用,但是静态方法和类方法无法访问实例属性,所以更改了tst.data仅对普通方法起了作用
1 2 3 4 5 6 7 8 9 10 | tst = Tst()
tst.data = 'this is new'
tst.normalMethod( 'name' )
tst.staticMethod( 'name' )
tst.classMethod( 'name' )
#结果
this is new name
name
this is data name
|
区别
普通方法不能通过类名调用,但是静态方法和类方法是可以的
1 2 3 4 | # error普通方法必须通过实例调用#
Tst.normalMethod( 'name' )Tst.classMethod( 'name' )
Tst.staticMethod( 'name' )#结果this is data name
name
|
总结:
1、普通方法,可以通过self访问实例属性
1 | def normalMethod(self,data)
|
2、类方法,可以通过cls访问类属性
1 2 | @classmethod
def classMethod(cls,data)
|
3、静态方法,不可以访问,通过传值的方式
1 2 | @staticmethod
def staticMethod(data)
|
更多技术请关注Python视频教程。