我意識到一個月過去了,但我遇到了類似的問題(即pow(float('nan'), 1)
在某些Python實現中拋出異常,例如Jython 2.52b2),並且我發現上述答案並不是那麼簡單,我非常想找到的東西。
使用6502建議的MissingData類型似乎是要走的路,但我需要一個具體的例子。我試圖伊桑Furman的零式類卻發現,這不與任何算術運算工作,因爲它不強制數據類型(見下文),而且我也不喜歡,它明確指定這是重寫每一個算術函數。
從Ethan的例子開始,調整代碼我發現here,我到了下面的課。雖然這個類很受評論,但您可以看到它實際上只有少量的功能代碼。
其要點是: 1.使用裹脅()返回用於混合型的(例如無數據+浮動)算術操作的兩個無數據對象,並且兩個字符串用於基於字符串(例如CONCAT)操作。 2.使用GETATTR()返回一個可調用無數據()對象對於所有其他屬性/方法訪問 3.使用()調用來實現無數據的所有其他方法()對象:由返回NoData()對象
以下是其使用的一些示例。
>>> nd = NoData()
>>> nd + 5
NoData()
>>> pow(nd, 1)
NoData()
>>> math.pow(NoData(), 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: nb_float should return float object
>>> nd > 5
NoData()
>>> if nd > 5:
... print "Yes"
... else:
... print "No"
...
No
>>> "The answer is " + nd
'The answer is NoData()'
>>> "The answer is %f" % (nd)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float argument required, not instance
>>> "The answer is %s" % (nd)
'The answer is '
>>> nd.f = 5
>>> nd.f
NoData()
>>> nd.f()
NoData()
我注意到,使用戰俘與無數據()調用**操作,因此與無數據的工作,但使用math.pow不會因爲它首先嚐試無數據()對象轉換爲浮動。我很高興使用非數學pow - 希望6502等他們使用math.pow,當他們在上面的評論中有pow問題時。
其他問題,我想不出解決的辦法是與格式(%F)運算符...的無數據的任何方法被調用在這種情況下使用,如果你不操作只是失敗提供一個浮動。無論如何,這是班級本身。
class NoData():
"""NoData object - any interaction returns NoData()"""
def __str__(self):
#I want '' returned as it represents no data in my output (e.g. csv) files
return ''
def __unicode__(self):
return ''
def __repr__(self):
return 'NoData()'
def __coerce__(self, other_object):
if isinstance(other_object, str) or isinstance(other_object, unicode):
#Return string objects when coerced with another string object.
#This ensures that e.g. concatenation operations produce strings.
return repr(self), other_object
else:
#Otherwise return two NoData objects - these will then be passed to the appropriate
#operator method for NoData, which should then return a NoData object
return self, self
def __nonzero__(self):
#__nonzero__ is the operation that is called whenever, e.g. "if NoData:" occurs
#i.e. as all operations involving NoData return NoData, whenever a
#NoData object propagates to a test in branch statement.
return False
def __hash__(self):
#prevent NoData() from being used as a key for a dict or used in a set
raise TypeError("Unhashable type: " + self.repr())
def __setattr__(self, name, value):
#This is overridden to prevent any attributes from being created on NoData when e.g. "NoData().f = x" is called
return None
def __call__(self, *args, **kwargs):
#if a NoData object is called (i.e. used as a method), return a NoData object
return self
def __getattr__(self,name):
#For all other attribute accesses or method accesses, return a NoData object.
#Remember that the NoData object can be called (__call__), so if a method is called,
#a NoData object is first returned and then called. This works for operators,
#so e.g. NoData() + 5 will:
# - call NoData().__coerce__, which returns a (NoData, NoData) tuple
# - call __getattr__, which returns a NoData object
# - call the returned NoData object with args (self, NoData)
# - this call (i.e. __call__) returns a NoData object
#For attribute accesses NoData will be returned, and that's it.
#print name #(uncomment this line for debugging purposes i.e. to see that attribute was accessed/method was called)
return self
我的意思是Jython 2.5.2b2,而不是2.52b2 – jcdude 2012-05-03 12:43:12