使用class中的关键字调用特定方法
Use keywords in class to call a specific method
假设一个Pythonclass有不同的方法,根据用户指定的不同,在主函数calculate()
.
中执行不同的方法
在下面的示例中,用户需要指定关键字参数 'methodOne'
或 'methodTwo'
。如果没有指定或指定了不正确的关键字,它应该默认为 'methodOne'
。
class someClass(object):
def __init__(self,method=None):
methodList = ['methodOne','methodTwo']
if method in methodList:
self.chosenMethod = method
else:
self.chosenMethod = self.methodOne
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
return self.chosenMethod()
以上显然不起作用,因为 method
是字符串而不是函数。如何根据我的关键字参数 method
select self.methedOne()
或 self.methedOne()
?原则上以下工作:
def __init__(self,method=None):
if method == 'methodOne':
self.chosenMethod = self.methodOne
elif method == 'methodTwo':
self.chosenMethod = self.methodTwo
else:
self.chosenMethod = self.methodOne
但是如果我有两种以上的方法,这就变得相当难看。有没有办法像我的原始代码那样执行此操作?
您可以使用 getattr
获取 class 对象的实际方法。
class someClass(object):
def __init__(self,method=None):
# store it with the object so we can access it later in calculate method
self.method = method
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
# get the actual method from the string here
# if no such method exists then use methodOne instead
return getattr(self, self.method, self.methodOne)()
> someClass('methodOne').calculate()
# 1
> someClass('methodTwo').calculate()
# 2
您可以使用 getattr()
来达到这个目的:
class someClass(object):
def __init__(self,method=None):
methodList = ['methodOne','methodTwo']
if method in methodList:
self.chosenMethod = method
else:
self.chosenMethod = self.methodOne
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
return getattr(self, self.chosenMethod)()
x = someClass(method='methodOne')
print x.calculate()
>>> 1
假设一个Pythonclass有不同的方法,根据用户指定的不同,在主函数calculate()
.
在下面的示例中,用户需要指定关键字参数 'methodOne'
或 'methodTwo'
。如果没有指定或指定了不正确的关键字,它应该默认为 'methodOne'
。
class someClass(object):
def __init__(self,method=None):
methodList = ['methodOne','methodTwo']
if method in methodList:
self.chosenMethod = method
else:
self.chosenMethod = self.methodOne
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
return self.chosenMethod()
以上显然不起作用,因为 method
是字符串而不是函数。如何根据我的关键字参数 method
select self.methedOne()
或 self.methedOne()
?原则上以下工作:
def __init__(self,method=None):
if method == 'methodOne':
self.chosenMethod = self.methodOne
elif method == 'methodTwo':
self.chosenMethod = self.methodTwo
else:
self.chosenMethod = self.methodOne
但是如果我有两种以上的方法,这就变得相当难看。有没有办法像我的原始代码那样执行此操作?
您可以使用 getattr
获取 class 对象的实际方法。
class someClass(object):
def __init__(self,method=None):
# store it with the object so we can access it later in calculate method
self.method = method
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
# get the actual method from the string here
# if no such method exists then use methodOne instead
return getattr(self, self.method, self.methodOne)()
> someClass('methodOne').calculate()
# 1
> someClass('methodTwo').calculate()
# 2
您可以使用 getattr()
来达到这个目的:
class someClass(object):
def __init__(self,method=None):
methodList = ['methodOne','methodTwo']
if method in methodList:
self.chosenMethod = method
else:
self.chosenMethod = self.methodOne
def methodOne(self):
return 1
def methodTwo(self):
return 2
def calculate(self):
return getattr(self, self.chosenMethod)()
x = someClass(method='methodOne')
print x.calculate()
>>> 1