Python中的对象树

2024-09-25 04:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到自动完成的字典或一些类似的对象,以便访问对象。以下是我目前的情况:

class Foo:
   def DoFoo(self):
      pass

list = {"x": Foo(), "y": Foo()}

现在我要做的是:

list.x.DoFoo()

我怎样才能用IPython自动完成来访问的对象填充一个列表?你知道吗

我需要像我看到的here那样的“类工厂”吗?你知道吗


Tags: 对象self列表字典herefoo工厂def
2条回答

您需要重写__getattr__

class Foo(object):

    attr_a = 'default_attr_a_value'
    attr_b = 'default_attr_b_value'

    def __init__(self, *args, **kwargs):
        if len(args) >= 2:
            self.attr_a = args[0]
            self.attr_b = args[1]
        if len(args) == 1:
            self.attr_a = args[0]

        # holds the "emulated" attributes
        self._properties = kwargs

    def __getattr__(self, name):
        if name in self._properties:
            return self._properties[name]
        else:
            raise AttributeError

    def __hasattr__(self, name):
        return name in self._properties

bar = Foo('modified_value_of_attr_a', **{'aaa':1,'bbb':2})

print bar.attr_a
print bar.attr_b
print bar.aaa

try:
    print bar.sss
except AttributeError:
    print 'Attribute error raised!'

list_attrs = ['attr_a', 'aaa', 'sss']
for a in list_attrs:
    if hasattr(bar, a):
        print 'bar, instance of Foo, has an attribute named "%s"' % a
    else:
        print 'bar, instance of Foo, doesn\'t have an attribute named "%s"' % a

More to read

自定义类的实例可以用作在其上设置的属性的容器。这可以简单地开始:

class MyContainer:
   pass

a = MyContainer()
a.x = Foo()
a.y = Foo()

a.x.DoFoo()

要获得作为构造函数的一部分传入所需属性的功能(使用与dict构造函数相同的语义),可以使它变得更复杂一些:

def MyContainer:
    def __init__(self, vals={}, **kwvals):
        self.__dict__.update(vals, **kwvals)

a = MyContainer({"x":Foo()}, y=Foo())   # there are multiple ways to pass the args

a.x.DoFoo()

如果在创建此类容器后不需要添加或删除值,可以改用标准库的collections.namedtuple类工厂。您向它传递一个类名和一系列属性名,它将返回一个新类。你知道吗

from collections import namedtuple

A = namedtuple("A", ["x", "y"])
a = A(Foo(), y=Foo())       # can use positional or keyword arguments

a.x.DoFoo()      # this still works!
a.x = "something else"      # but this will raise an error

相关问题 更多 >