自定义dict类的ipython选项卡完成

Joh*_*ohn 11 python

我一直在我的代码中使用以下内容:

class Structure(dict,object):
""" A 'fancy' dictionary that provides 'MatLab' structure-like
referencing. 

"""
def __getattr__(self, attr):
    # Fake a __getstate__ method that returns None
    if attr == "__getstate__":
        return lambda: None
    return self[attr]

def __setattr__(self, attr, value):
    self[attr] = value

def set_with_dict(self, D):
    """ set attributes with a dict """
    for k in D.keys():
        self.__setattr__(k, D[k])
Run Code Online (Sandbox Code Playgroud)

总而言之,它适用于我的目的,但我注意到,只有方法选项卡完成才能用于继承自Structure的另一个自定义类中的方法,而不是属性.我也做了这个测试,我发现结果有点奇怪:

In [2]: d = Structure()
In [3]: d.this = 'that'
In [4]: d.this
Out[4]: 'that'
In [5]: d.th<tab>
NOTHING HAPPENS

In [6]: class T():
   ...:     pass
   ...: 

In [7]: t = T()
In [8]: t.this = 'cheese'
In [9]: t.th<tab>
COMPLETES TO t.this
Out[9]: 'cheese'
Run Code Online (Sandbox Code Playgroud)

我需要将哪些内容添加到我的类中以使标签完成为属性工作?