Python类与整数模拟 [英] Python Class with integer emulation

查看:136
本文介绍了Python类与整数模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出如下的例子:

$ p $ class Foo(object):
def __init __(self,value = 0):
self.value = value
$ b $ def __int __(self):
return self.value

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

$ $ $ $ $ $ $ $打印int(f)+5 #正在工作
print f + 5#TypeError:不支持的操作数类型为+:'Foo'和'int'

第一条语句 print int(f)+5 正在工作,因为有两个整数。第二个是失败的,因为我必须执行 __ add __ 来对我的类执行此操作。



实现整数行为,我必须实现所有的整型仿真方法。我怎么能解决这个问题我尝试从 int 继承,但是这次尝试没有成功。

更新如果你想使用 __ init __ 继承 int

$ $ $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ):
self.value = value
#做一些东西
$ b $ def __int __(self):
return int(self.value)

如果您再打电话:

 <$ c $你可以得到:
$ f $

  TypeError:'some_argument'是这个函数的一个无效关键字参数

使用Python 2.5和2.6测试

解决方案

您需要覆盖 __ new__ ,而不是 __ init __

  class Foo (int):
def __new __(cls,some_argument = None,value = 0):
i = int .__ new __(cls,value)
i._some_argument = some_argument
return
$ b $ def print_some_argument(self):
print self._some_argument

现在您的课程可按预期工作:

 >>> f = Foo(some_argument =我是一个自定义的int,value = 10)
>>> f
10
>>> f + 8
18
>>> f * 0.25
2.5
>>> f.print_some_argument()
我是一个自定义的int

有关覆盖 new 可以在 Unifying Python 2.2中的类型和类


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

解决方案

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.

这篇关于Python类与整数模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆