我意识到一个月过去了,但我遇到了类似的问题(即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