如何在 python class 中有条件地别名方法
How to conditionally alias a method in a python class
假设一个 class myClass
有两个方法定义 _call1
和 _call2
,但是应该只使用其中一个,这取决于对象被调用时传递的参数实例化,并为所选方法提供特定别名 call
。如何编程?下面显示了一个最小示例。
此代码背后的原因是我想避免在单个 call
方法中使用带有两个可能的 return
的 if-else
语句的开销。另一种选择是基于 cond_param
创建两个不同的可能 classes,但我宁愿避免这种情况。
class myClass:
def __init__(self, cond_param):
self.cond_param = cond_param
# depending on the value of the conditional parameter `cond_param`,
# a method named `call` will be assigned to one of the methods
# defined below
if self.cond_param not in [1, 2]:
return ValueError
if self.cond_param == 1:
call = _call1
else
call = _call2
def _call1(self):
return "call 1!"
def _call2(self):
return "call 2!"
obj1 = myClass(1)
print(obj1.call()) # should print "call 1!"
obj2 = myClass(2)
print(obj2.call()) # should print "call 2!"
你可以用字典来避免 if-else。
您需要使用 self.
才能在 class 中的任何地方引用某些内容。此外,您应该使用 raise
来引发错误,而不是 return
,后者会 return 错误 class 本身。
修复您的代码
class myClass:
def __init__(self, cond_param):
self.cond_param = cond_param
# depending on the value of the conditional parameter `cond_param`,
# a method named `call` will be assigned to one of the methods
# defined below
self._calls = {1: self._call1, 2: self._call2}
self.call = self._calls.get(cond_param, self._callErr)
def _call1(self):
return "call 1!"
def _call2(self):
return "call 2!"
def _callErr(self):
raise ValueError
obj1 = myClass(1)
print(obj1.call()) # should print "call 1!"
obj2 = myClass(2)
print(obj2.call()) # should print "call 2!"
obj3 = myClass(3)
print(obj3.call()) # should raise ValueError
给予
call 1!
call 2!
Traceback (most recent call last):
...
ValueError
符合预期。
假设一个 class myClass
有两个方法定义 _call1
和 _call2
,但是应该只使用其中一个,这取决于对象被调用时传递的参数实例化,并为所选方法提供特定别名 call
。如何编程?下面显示了一个最小示例。
此代码背后的原因是我想避免在单个 call
方法中使用带有两个可能的 return
的 if-else
语句的开销。另一种选择是基于 cond_param
创建两个不同的可能 classes,但我宁愿避免这种情况。
class myClass:
def __init__(self, cond_param):
self.cond_param = cond_param
# depending on the value of the conditional parameter `cond_param`,
# a method named `call` will be assigned to one of the methods
# defined below
if self.cond_param not in [1, 2]:
return ValueError
if self.cond_param == 1:
call = _call1
else
call = _call2
def _call1(self):
return "call 1!"
def _call2(self):
return "call 2!"
obj1 = myClass(1)
print(obj1.call()) # should print "call 1!"
obj2 = myClass(2)
print(obj2.call()) # should print "call 2!"
你可以用字典来避免 if-else。
您需要使用 self.
才能在 class 中的任何地方引用某些内容。此外,您应该使用 raise
来引发错误,而不是 return
,后者会 return 错误 class 本身。
修复您的代码
class myClass:
def __init__(self, cond_param):
self.cond_param = cond_param
# depending on the value of the conditional parameter `cond_param`,
# a method named `call` will be assigned to one of the methods
# defined below
self._calls = {1: self._call1, 2: self._call2}
self.call = self._calls.get(cond_param, self._callErr)
def _call1(self):
return "call 1!"
def _call2(self):
return "call 2!"
def _callErr(self):
raise ValueError
obj1 = myClass(1)
print(obj1.call()) # should print "call 1!"
obj2 = myClass(2)
print(obj2.call()) # should print "call 2!"
obj3 = myClass(3)
print(obj3.call()) # should raise ValueError
给予
call 1!
call 2!
Traceback (most recent call last):
...
ValueError
符合预期。