我无法理解x和y是如何相同的列表。我一直在尝试使用print语句和import code; code.interact(local=locals())
进行调试,以便进入各个方面,但我无法弄清楚究竟发生了什么: - (
from collections import namedtuple, OrderedDict
coordinates_2d=["x","y"]
def virtual_container(virtual_container, objects_type):
"""Used to create a virtual object given a the type of container and what it holds.
The object_type needs to only have normal values."""
if issubclass(virtual_container, list):
class my_virtual_container_class:
"""This singleton class represents the container"""
def __init__(self):
#Define the default values
__vals__=OrderedDict([(key,list()) for key in objects_type])
print(id(__vals__["x"]), id(__vals__["y"]))#ids are different: 12911896 12911968
#Then functions to access them
d={key: lambda self: self.__vals__[key] for key in objects_type}
d["__vals__"]=__vals__
#Construct a named tuple from this
self.attr=type('attr_cl',(), d)()
print(id(self.attr.x()), id(self.attr.y()))#ids are same: 32904544 32904544
#TODO: Define the operators __del__, setitem, getitem. Also append
return my_virtual_container_class()
#Nice method of handling coordinates
coordinates=virtual_container(list, coordinates_2d)
x=coordinates.attr.x()
y=coordinates.attr.y()
x.append(1)
y.append(2)
print(x, y)#Prints [1, 2] [1, 2]
答案 0 :(得分:7)
问题在于这一行:
d={key: lambda self: self.__vals__[key] for key in objects_type}
lambda使用变量key
的值,但是这个值在调用lambda时已经改变了 - 所以所有lambdas实际上都会使用相同的键值。
这可以通过一个小技巧解决:将密钥作为默认参数值传递给lambda:
... lambda self, key=key: self.__vals__[key] ...
这可以确保key
的值绑定到创建lambda时的值。
答案 1 :(得分:0)
我认为以下行应该是这样的(但遗憾的是我无法测试,因为我没有Python 3可用):
# Then functions to access them
d = dict((key, lambda self: self.__vals__[key]) for key in objects_type)