class A:
def __init__(self,opt):
if not hasattr(self,opt):
raise SystemExit(1)
getattr(self,opt)()
def optionA(self):
return "A"
def optionB(self):
return "B"
现在,当我试着用它的时候
>> A('optionA')
<__main__.A instance at 0x7f87bccfca70>
我希望它返回的是“A”。所以我试着用
class A:
def __call__(self,opt):
if not hasattr(self,opt):
raise SystemExit(1)
getattr(self,opt)()
def optionA(self):
return "A"
def optionB(self):
return "B"
这很管用,但现在我不得不打这个丑陋的电话
A()("optionA")
init
方法不返回值,如果要使其工作,请执行此操作, 使用另一个isntance方法getdata
(在我的例子中):-你想用这个解决什么问题?您只是将类用作函数容器吗?你可以试试下面的方法;有点漂亮
相关问题 更多 >
编程相关推荐