Python Class with integer emulation

后端 未结 3 950
慢半拍i
慢半拍i 2020-12-18 06:39

Given is the following example:

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

    def __int__(self):
        return self.valu         


        
3条回答
  •  孤城傲影
    2020-12-18 07:02

    In Python 2.4+ inheriting from int works:

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

提交回复
热议问题