English 中文(简体)
伸展的Python级不完全表现得像一英寸
原标题:Python class that extends int doesn t entirely behave like an int
  • 时间:2012-05-23 17:34:55
  •  标签:
  • python

当试图将一个字符串转换为我写给一个扩展 int 的班级时,我看到了一些怪异的行为。这里有一个简单的程序来证明我的问题:

class MyInt(int):
    pass

toInt =  123456789123456789123456789 

print "
Converting to int..."
print type(int(toInt))

print "
Converting to MyInt..."
print type(MyInt(toInt))

由于 MyInt 是空的, 我期望它的行为将完全像 int 。 相反, 这里显示我从以上程序获得的输出 :

Converting to int...
<type  long >

Converting to MyInt...
Traceback (most recent call last):
  File "int.py", line 9, in <module>
    print type(MyInt(toInt))
OverflowError: long int too large to convert to int

字符串无法转换为 < code> myInt ! 我写的 < code> MyInt 是如何导致其行为与其基类不同? 在这种情况下, 在 < code> MyInt 上似乎有某种最大值; 在 Python 扩展一个内嵌类时, 是否有其它隐含的属性被强制实施? 最后, 有没有办法更改 < code> MyInt , 以便它不再有这个最大值?

最佳回答

秘密在 方法下:

>>> class MyInt(int): pass
>>> MyInt.__new__ == int.__new__
True
>>> MyInt.__new__(MyInt,  123456789101234567890 )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
>>> MyInt.__new__(int,  123456789101234567890 )
123456789101234567890L

当您立即将一个类别( 在 < code_ init_ (self, *args) 之前) 第一件事发生于 < code_ new_ (cls, *args) 。 它的第一个参数是通过该类对象。 < code> new_ /code > 方法( 由 MyInt 继承), 只有在通过该类别为 < code> long 的情况下, 才会转换为 < code > long 。 我假设这是为了避免扰乱子类, 因为将 < code> Myint 转换为 < code > loong 将会删除您添加的所有特殊功能 。

如果您想要的整数大于 int 能够处理的整数, 您应该使用 long 作为基本类别。

问题回答

希望这次与口译员的会话能提供一些深入的见解,

>>> i = 1
>>> print type(i), i
<type  int > 1
>>> i = int((i << 31) - 1)
>>> print type(i), i
<type  int > 2147483647
>>> i += 1
>>> print type(i), i
<type  long > 2147483648
>>> 

您的班级没有继承此行为, 因为 Python 可能将 < code>int 对象作为特例处理 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签