Python中的子类float类型无法在__init __()中捕获异常 [英] Sub-classing float type in Python, fails to catch exception in __init__()
问题描述
在Python 2.5中,我需要使用修改后的 __ str __()
方法的浮点数。另外,我需要知道构造函数何时失败。
On Python 2.5 I need to use float numbers with a modified __str__()
method. Also I need to know when the constructor fails.
为什么我无法捕获从 float .__ init __()
?
查询我的派生浮点对象的数值的最好方法是什么?在我的代码中,我使用 float(self)
。
What is the best way to consult the numeric value of my derived float object? In my code I'm using float(self)
.
class My_Number(float):
def __init__(self, float_string):
try:
super(My_Number, self).__init__(float_string)
except (TypeError, ValueError):
raise My_Error(float_string)
def __str__(self):
if int(float(self)) == float(self):
return str(int(float(self)))
else:
return str(round(float(self), 2))
>>> n = My_Number('0.54353')
>>> print n
0.54
>>> n = My_Number('5.0')
>>> print n
5
>>> n = My_Number('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): foo
推荐答案
float
是不可变的,因此其 __ init __
,初始化程序基本上是无操作 - 没有任何实质可以在那里发生,因为 self
对象不能被更改(如果它实际上是 float
而不是一个子类 - 但当然 float
自己的 __ init __
必须在这个假设下运行; )
float
is immutable, therefore its __init__
, the initializer, is basically a no-op -- nothing substantial can happen there, because the self
object cannot be altered (if it's actually an instance of float
rather than of a subclass -- but of course float
's own __init__
must operate on that assumption;-).
因此,所有的操作都发生在 __ new __
,构造函数就像其他不可变类型,如 int
, str
, tuple
,等等。这是一个常见的错误,认为 __ init __
是一个构造函数:它不是,它需要一个已经构造的对象作为其第一个参数, self
,并且初始化它(如果可行,即如果 self
是可变的 - - ) - 构造本身发生在 __ new __
。
Therefore, all the action happens in __new__
, the constructor proper, just like for other immutable types like int
, str
, tuple
, and so on. It's a common mistake to believe that __init__
is a constructor: it's not, it takes an already-constructed object as its first argument, self
, and "initializes" it (if feasible, i.e., if that self
is mutable!-) -- the construction itself happens in __new__
.
所以,你的 float
子类应该开始:
So, your float
subclass should start:
class My_Number(float):
def __new__(cls, float_string):
try: return float.__new__(cls, float_string)
except (TypeError, ValueError): raise My_Error(float_string)
,您可以删除不需要的 __ init __
。现在:
and you can remove the __init__
, which is not needed. Now:
>>> n = My_Number('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __new__
NameError: global name 'My_Error' is not defined
(当然,如果 有一个 My_Error
定义的异常类,它会更好; )。
(of course, it would work even better if you did have a My_Error
exception class defined;-).
这篇关于Python中的子类float类型无法在__init __()中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!