类似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)