类似C++中的:
class A {
static int var;
static int func(void);
}
在python中如何实现呢?
研究了下,静态函数可以这样:
class A():
# non-static method
def func1(self):
pass
# static method
@staticmethod
def func2():
pass
但静态变量就麻烦些了,需要用classmethod来间接实现,如:
class A():
last = -1
@classmethod
def last_argument(cls, n):
temp = cls.last
cls.last = n
return temp
A.last_argument(1)
A.last_argument(17)
此博文 有问题
静态成员变量 示例如下:
class Test:
Total=0
def __init__(self):
Test.Total = Test.Total +1
if __name__==”__main__”:
a=[]
for i in xrange(4):
a.append(Test())
print a
print a[0].Total
print a[1].Total
print a[2].Total
print Test.Total