想要写一个 classmethod 和一个同名的普通方法,使得直接通过类名调用时调用的是 classmethod,通过实例则调用普通方法。
本来通过参数个数来区别,本来至少需要一个参数,所以对其中一个增加作为区分的参数
https://gist.github.com/wenLiangcan/8764887
class test:
def __init__(self):
self.a = 1
@classmethod
def foo(cls, p, flag):
pass
def foo(self, p):
print(self.a)
可是 test.foo(P, f) 却会报错,显示调用了只有两个参数的 foo
如果用两个 flag 显然又太笨拙了,请问有什么优雅的写法么?
本来通过参数个数来区别,本来至少需要一个参数,所以对其中一个增加作为区分的参数
https://gist.github.com/wenLiangcan/8764887
class test:
def __init__(self):
self.a = 1
@classmethod
def foo(cls, p, flag):
pass
def foo(self, p):
print(self.a)
可是 test.foo(P, f) 却会报错,显示调用了只有两个参数的 foo
如果用两个 flag 显然又太笨拙了,请问有什么优雅的写法么?