Where is stored the int value of a int instance in Python?

前端 未结 1 1328
小鲜肉
小鲜肉 2021-01-20 07:26

The int type in python offers two attributes named numerator and real that have the same content as __int__().

A

相关标签:
1条回答
  • 2021-01-20 07:43

    The actual integer value is in ob_ival. In essence, int_int is simply taking the integer value from one int object and wrapping it in another object.

    Not sure why you can't see the properties. If I run this, they show up for me, in both 2.7 and 3.4 versions:

    x = 8
    dir(x)
    

    EDIT: Too hard to explain in a comment, so adding to the answer.

    You could easily subclass it like this:

    class foo(int):
        def __getitem__(self):
            return self + 1
    
    foo(8).__getitem__()
    

    You could also use super to explicitly access the int object this way.

    (You do realize that __getitem__ is for use with keyed objects [dict-like, say] and hence normally gets a second argument specifying the key, right? And int and float are not keyed.)

    0 讨论(0)
提交回复
热议问题