我想要一類函數中定義一個類的實例運營商,像這樣:重載運營商
class MyClass(object):
@property
def _arithmetic_threshold(self):
return self.threshold # this will be defined somewhere
@_arithmetic_threshold.setter
def _arithmetic_threshold(self,value):
self.threshold = value
self._define_arithmetic_attributes()
def _define_arithmetic_attributes(self):
"""
Wrapper to define all arithmetic attributes (in order to allow threshold changes)
"""
self.__add__ = self._operation_wrapper(np.add,threshold=self._arithmetic_threshold)
self.__sub__ = self._operation_wrapper(np.subtract,threshold=self._arithmetic_threshold)
self.__mul__ = self._operation_wrapper(np.multiply,threshold=self._arithmetic_threshold)
self.__div__ = self._operation_wrapper(np.divide,threshold=self._arithmetic_threshold)
然而,這似乎並沒有工作 - 我覺得我失去了一些東西約運營商-
,+
等如何調用這些功能。即:
class MyClass2(object):
def __add__(self,other,threshold=None):
if threshold is not None:
return self+other
else:
# do something here involving checking a threshold....
pass
在MyClass2中,__add__
的行爲將有所不同。任何人都可以解釋它們是如何不同的,以及如何使MyClass中的操作符方法的行爲類似於MyClass2?
編輯:只是爲了澄清爲什麼我試圖做到這一點,這裏的_operation_wrapper
。這個類是一個「頻譜」對象,它有一個X軸和一個Y軸。目標是允許在Y軸上進行算術運算,但前提是X軸匹配。然而,它們可以匹配像素大小的1/5,這是可以接受的,所以我想要做更多純粹的「精確」匹配。
def _operation_wrapper(operation):
"""
Perform an operation (addition, subtraction, mutiplication, division, etc.)
after checking for shape matching
"""
def ofunc(self, other):
if np.isscalar(other):
newspec = self.copy()
newspec.data = operation(newspec.data, other)
return newspec
else: # purely for readability
if self._arithmetic_threshold == 'exact':
xarrcheck = all(self.xarr == other.xarr)
else:
if self._arithmetic_threshold_units is None:
# not sure this should ever be allowed
xarrcheck = all((self.xarr-other.xarr) < self._arithmetic_threshold)
else:
xarrcheck = all((self.xarr.as_unit(self._arithmetic_threshold_units)-other.xarr.as_unit(self._arithmetic_threshold_units)) < self._arithmetic_threshold)
if self.shape == other.shape and xarrcheck:
newspec = self.copy()
newspec.data = operation(newspec.data, other.data)
return newspec
elif self.shape != other.shape:
raise ValueError("Shape mismatch in data")
elif not xarrcheck:
raise ValueError("X-axes do not match.")
return ofunc
對於運營商首要的先進(涼爽)示例,請參閱[這裏]( http://stackoverflow.com/a/7844038/566644)對我在SO上提出的問題進行了精彩的回答。 – 2012-02-25 19:02:02