Python空dict未通过引用传递?
这个例子是代码有点奇怪,但请容忍我Python空dict未通过引用传递?,python,recursion,Python,Recursion,这个例子是代码有点奇怪,但请容忍我 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_n
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': {}}
我本来希望我的{'123'='abc'}
能够通过第二次打印,因为字典应该通过引用传递到递归的\uuuuu init\uuu
调用中。但是,如果我更改此行:
self._internal_dict[attribute_name] = {}
为此:
self._internal_dict[attribute_name] = {'test': 1}
然后我会打印以下内容:
{'test': 1}
{'ay': {'test': 1}, 'ax': {'test': 1, '123': 'abc'}}
为什么启动字典数据会导致它通过引用正确传递?这就是问题所在:
self._internal_dict = internal_dict or {}
空的dict是错误的,因此在后续递归调用中您将得到一个新的空dict。这就是为什么将dict初始化为非空(truthy)“修复”它
你想要:
self._internal_dict = {} if internal_dict is None else internal_dict
这就是问题所在:
self._internal_dict = internal_dict or {}
空的dict是错误的,因此在后续递归调用中您将得到一个新的空dict。这就是为什么将dict初始化为非空(truthy)“修复”它
你想要:
self._internal_dict = {} if internal_dict is None else internal_dict
你的类的问题是你在分配
self.\u internal\u dict
变量时所走的捷径
不幸的是,空字典的真值为false
如果将代码更改为:
class Foo(object):
def __init__(self, internal_dict = None):
if internal_dict is None:
internal_dict = {}
self._internal_dict = internal_dict
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': {}}
你的类的问题是你在分配
self.\u internal\u dict
变量时所走的捷径
不幸的是,空字典的真值为false
如果将代码更改为:
class Foo(object):
def __init__(self, internal_dict = None):
if internal_dict is None:
internal_dict = {}
self._internal_dict = internal_dict
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': {}}
这就是我在检查是否提供了可选参数时询问是否包含
是否为None
的确切原因。感谢您确认这确实发生了;-)这就是我在检查是否提供了可选参数时询问是否包含是否为None
的确切原因。感谢您确认这确实发生了;-)谢谢我以为我疯了。谢谢!我以为我疯了。