object.__flags__的结果是什么意思?以及如何解读它?
In [55]: str.__flags__
Out[55]: 269227008
In [56]: list.__flags__
Out[56]: 34362368
In [57]: tuple.__flags__
Out[57]: 67916800
In [58]: object.__flags__
Out[58]: 791552
In [59]: ndarray.__flags__
Out[59]: 791552而且,最重要的是,为什么object.__flags__和ndarray.__flags__得到相同的结果?
发布于 2018-01-08 22:08:29
使用链接SO:在python类型中用于
很明显,__flags__是类的tpflags属性的整数表示。以二进制形式显示,您的示例如下:
In [165]: "{0:29b}".format(str.__flags__) # high bit (1UL << 28)
Out[165]: '10000000011000001010000000000'
In [166]: "{0:29b}".format(tuple.__flags__) # (1UL << 26)
Out[166]: ' 100000011000101010000000000'
In [167]: "{0:29b}".format(list.__flags__) # (1UL << 25)
Out[167]: ' 10000011000101010000000000'object没有这些高度专门化的比特:
In [168]: "{0:29b}".format(object.__flags__)
Out[168]: ' 11000001010000000000'
In [169]: "{0:29b}".format(np.ndarray.__flags__)
Out[169]: ' 11000001010000000000'
In [171]: "{0:29b}".format(1<<10 | 1<<12 | 1<<18 | 1<<19)
Out[171]: ' 11000001010000000000'np.ufunc没有1<<10位,Py_TPFLAGS_BASETYPE。
In [170]: "{0:29b}".format(np.ufunc.__flags__)
Out[170]: ' 11000001000000000000'我不能把它细分:
In [173]: class foo(np.ufunc):
...: pass
TypeError: type 'numpy.ufunc' is not an acceptable base type用户定义类的标志为
In [176]: class Foo:
...: pass
In [177]: "{0:29b}".format(Foo.__flags__)
Out[177]: ' 11000101011000000001'对于np.ndarray的子类(如np.matrix )也是如此。
我们可以在numpy代码中搜索tpflags定义
https://github.com/numpy/numpy/search?utf8=%E2%9C%93&q=tpflags&type=
在arrayobject.c中,标志被设置为Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE。在ufunc_object.c只有Py_TPFLAGS_DEFAULT。
总之,__flags__表明np.ndarray是一个普通的基类。numpy不尝试使用此属性设置任何特殊属性。
https://stackoverflow.com/questions/48143749
复制相似问题