DIR为什么不显示所有的Python对象属性? [英] Why dir doesn't show all Python object attributes?

查看:1312
本文介绍了DIR为什么不显示所有的Python对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么某些对象的方法/属性显示不出来,当我打电话 DIR 的对象?

Why do some object methods/attributes not show up when I call dir on the object?

例如:

from scipy import sparse
e = sparse.eye(2)
'H' in dir(e)

收益。但调用 e.H 工作得很好(返回另一个稀疏矩阵对象)。为什么会这样,我怎么能看到这些隐藏属性?

returns False. But calling e.H works just fine (returning another sparse matrix object). Why is this so, and how can I see these hidden attributes?

我使用Python 3.5.1。

I'm using Python 3.5.1.

推荐答案

EH EA 都是稀疏矩阵对象的伪属性。

e.H, e.A, e.T are pseudo-properties of the sparse matrix object.

形状是一个属性。在 sparse.base.py 我觉得

shape is a property. In sparse.base.py I find

shape = property(fget=get_shape, fset=set_shape)

,而字典仅包含:

In [121]: e.__dict__
Out[121]: 
{'_shape': (2, 2),
 'data': array([[ 1.,  1.]]),
 'format': 'dia',
 'maxprint': 50,
 'offsets': array([0])}

get_shape 可能访问 _shape set_shape 可能限制了我们的能力改变 _shape

get_shape probably accesses _shape. set_shape probably limits our ability to change _shape.

我不知道发现所有定义的属性这样一个对象的一种巧妙的方法。理想情况下,他们应该在文档中列出,但稀疏文档不理想。

I don't know of a neat way of finding all the defined properties for an object like this. Ideally they should be listed in the documentation, but sparse documentation is not ideal.

有一个 e.getH 方法,该方法返回相同的事情 EH ,所以我怀疑有一个一块code,很可能在某些超

There is an e.getH method, which returns the same thing as e.H, so I suspect there's a piece of code, probably in some superclass

H = property(fget=getH)

都能跟得上 - 在 sparse.base.py 我觉得 __ __ GETATTR 的重新定义,所以它是一个完全不同的机制(不是财产,而不是 __ __字典

Nope - in sparse.base.py I find a redefinition of __getattr__, so it's an entirely different mechanism (not property and not __dict__):

def __getattr__(self, attr):
    if attr == 'A':
        return self.toarray()
    elif attr == 'T':
        return self.transpose()
    elif attr == 'H':
        return self.getH()
    elif attr == 'real':
        return self._real()
    elif attr == 'imag':
        return self._imag()
    elif attr == 'size':
        return self.getnnz()
    else:
        raise AttributeError(attr + " not found")

IPython的我可以用取这个在[152]:电子.__ GETATTR __ ?? sparse.base ??

GETATTR 可用于获取所有这些特性和属性(和方法)

getattr can be used to fetch all these attributes and properties (and methods)

In [161]: getattr(e,'shape')
Out[161]: (2, 2)

In [162]: getattr(e,'_shape')
Out[162]: (2, 2)

In [163]: getattr(e,'H')
Out[163]: 
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Column format>

In [164]: getattr(e,'transpose')
Out[164]: 
<bound method dia_matrix.transpose of <2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements (1 diagonals) in DIAgonal format>>

随着检查模块,我可以得到成员的名单,例如

With the inspect module I can get a list of members, e.g.

[i[0] for i in inspect.getmembers(e)]

但是仍然没有列出在中定义这些特殊的人__ __ GETATTR 方法。

这篇关于DIR为什么不显示所有的Python对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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