相关疑难解决方法(0)

Python空字典没有通过引用传递?

这个样本代码有点奇怪,但请耐心等待...

class Foo(object):
    def __init__(self, internal_dict = None):
        self._internal_dict = internal_dict or {}

        for attribute_name in self.__class__.__dict__.keys():
            attr = getattr(self.__class__, attribute_name)
            if isinstance(attr, str) and attribute_name.startswith("a"):
                # We are iterating over all string attributes of this class whos name begins with "a" 
                self._internal_dict[attribute_name] = {}
                setattr(self, attribute_name + '_nested_object', Foo(internal_dict=self._internal_dict[attribute_name]))

class FooChild(Foo):
    ax = "5"
    ay = "10"

fc = FooChild()

print fc.ax_nested_object._internal_dict # This prints {}

fc.ax_nested_object._internal_dict['123'] = 'abc'

print fc._internal_dict # This prints {'ay': {}, 'ax': {}}
Run Code Online (Sandbox Code Playgroud)

我本来期望我 …

python recursion

1
推荐指数
1
解决办法
76
查看次数

标签 统计

python ×1

recursion ×1