我有這個類:OverflowError:Python的詮釋太大,轉換爲C長
class MetricInt(int):
"""Int wrapper that adds only during the observation window."""
def __new__(cls, _, initial):
return int.__new__(cls, initial)
def __init__(self, sim, initial):
int.__init__(initial)
self.sim = sim
def __add__(self, val):
if self.sim.in_observe_window():
self = MetricInt(self.sim, super(MetricInt, self).__add__(int(val)))
return self
這基本上覆蓋__add__
方法,以只對另外如果self.sim.in_observe_window()
回報True
。
但是,如果初始值太大,我有OverflowError: Python int too large to convert to C long
。
什麼是正確的方法來做我想做的事情,並處理大數字?
我有Python 2.7的問題,但我需要我的代碼與Python2和Python3兼容。 –