具有整数模拟的 Python 类

发布于 2024-08-09 03:26:43 字数 1028 浏览 3 评论 0原文

给出以下示例:

class Foo(object):
    def __init__(self, value=0):
        self.value=value

    def __int__(self):
        return self.value

我想要一个类 Foo,它充当整数(或浮点数)。所以我想做以下事情:

f=Foo(3)
print int(f)+5 # is working
print f+5 # TypeError: unsupported operand type(s) for +: 'Foo' and 'int'

第一个语句 print int(f)+5 有效,因为有两个整数。第二个失败了,因为我必须实现 __add__ 来对我的类执行此操作。

因此,为了实现整数行为,我必须实现所有整数模拟方法。我该如何解决这个问题。我尝试继承int,但这次尝试没有成功。

更新

如果您想使用 __init__,则从 int 继承会失败:

class Foo(int):
    def __init__(self, some_argument=None, value=0):
        self.value=value
        # do some stuff

    def __int__(self):
        return int(self.value)

如果您随后调用:

f=Foo(some_argument=3)

您将得到:

TypeError: 'some_argument' is an invalid keyword argument for this function

使用 Python 2.5 和 2.6 进行测试

Given is the following example:

class Foo(object):
    def __init__(self, value=0):
        self.value=value

    def __int__(self):
        return self.value

I want to have a class Foo, which acts as an integer (or float). So I want to do the following things:

f=Foo(3)
print int(f)+5 # is working
print f+5 # TypeError: unsupported operand type(s) for +: 'Foo' and 'int'

The first statement print int(f)+5 is working, cause there are two integers. The second one is failing, because I have to implement __add__ to do this operation with my class.

So to implement the integer behaviour, I have to implement all the integer emulating methods. How could I get around this. I tried to inherit from int, but this attempt was not successful.

Update

Inheriting from int fails, if you want to use a __init__:

class Foo(int):
    def __init__(self, some_argument=None, value=0):
        self.value=value
        # do some stuff

    def __int__(self):
        return int(self.value)

If you then call:

f=Foo(some_argument=3)

you get:

TypeError: 'some_argument' is an invalid keyword argument for this function

Tested with Python 2.5 and 2.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是年少的欢喜 2024-08-16 03:26:43

在 Python 2.4+ 中,继承 int 有效:

class MyInt(int):pass
f=MyInt(3)
assert f + 5 == 8

In Python 2.4+ inheriting from int works:

class MyInt(int):pass
f=MyInt(3)
assert f + 5 == 8
能怎样 2024-08-16 03:26:43

您需要重写 __new__,而不是 __init__

class Foo(int):
    def __new__(cls, some_argument=None, value=0):
        i = int.__new__(cls, value)
        i._some_argument = some_argument
        return i

    def print_some_argument(self):
        print self._some_argument

现在您的类按预期工作:

>>> f = Foo(some_argument="I am a customized int", value=10)
>>> f
10
>>> f + 8
18
>>> f * 0.25
2.5
>>> f.print_some_argument()
I am a customized int

有关重写 new 的更多信息可以在 统一 Python 2.2 中的类型和类

You need to override __new__, not __init__:

class Foo(int):
    def __new__(cls, some_argument=None, value=0):
        i = int.__new__(cls, value)
        i._some_argument = some_argument
        return i

    def print_some_argument(self):
        print self._some_argument

Now your class work as expected:

>>> f = Foo(some_argument="I am a customized int", value=10)
>>> f
10
>>> f + 8
18
>>> f * 0.25
2.5
>>> f.print_some_argument()
I am a customized int

More information about overriding new can be found in Unifying types and classes in Python 2.2.

篱下浅笙歌 2024-08-16 03:26:43

尝试使用最新版本的 python。您的代码适用于 2.6.1。

Try to use an up-to-date version of python. Your code works in 2.6.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文