标签:的区别 def proc static title tty self sso method
# -*-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 = Tst()
tst.data = ‘this is new‘
tst.normalMethod(‘name‘)
tst.staticMethod(‘name‘)
tst.classMethod(‘name‘)
#结果
this is new name
name
this is data name
# error普通方法必须通过实例调用
# Tst.normalMethod(‘name‘)
Tst.classMethod(‘name‘)
Tst.staticMethod(‘name‘)
#结果
this is data name
name
def normalMethod(self,data)
@classmethod
def classMethod(cls,data)
@staticmethod
def staticMethod(data)
标签:的区别 def proc static title tty self sso method
原文地址:https://www.cnblogs.com/fmgao-technology/p/9172978.html