我正在嘗試爲整數創建nan值。我正在考慮的設計如下。 我需要創建和類定義頭部isnan拉姆達功能,但它返回一個錯誤類頭中的lambda函數
import numpy as np
class Integer(object):
type = int
nan = -1
isnan = lambda val: val==-1
def __new__(cls, value):
return cls.type(value)
class Float(object):
type = float
isnan = lambda val: np.isnan(val)
def __new__(cls, value):
return cls.type(value)
但它返回一個錯誤
>> Integer.isnan(1)
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>>TypeError: unbound method <lambda>() must be called with Integer instance as first argument (got int instance instead)