Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动将python对象添加到字典_Python_Class_Object_Dictionary - Fatal编程技术网

自动将python对象添加到字典

自动将python对象添加到字典,python,class,object,dictionary,Python,Class,Object,Dictionary,我有一个类,其中包含程序中其他地方使用的信息,并定义了许多实例。我想将所有这些添加到字典中,并将它们的name属性作为键(见下文),以便用户可以访问它们 因为我经常制作新的这样的对象,有没有办法以同样的方式自动将它们添加到字典中?当然也可以添加到一个列表中,然后我可以迭代添加到字典中 简化示例: class Example: def __init__(self, name, eg): self.name = name self.eg = eg a =

我有一个类,其中包含程序中其他地方使用的信息,并定义了许多实例。我想将所有这些添加到字典中,并将它们的name属性作为键(见下文),以便用户可以访问它们

因为我经常制作新的这样的对象,有没有办法以同样的方式自动将它们添加到字典中?当然也可以添加到一个列表中,然后我可以迭代添加到字典中

简化示例:

class Example:
    def __init__(self, name, eg):
        self.name = name 
        self.eg = eg

a = Example("a", 0)
b = Example("b", 1)
c = Example("c", 2)
# etc...

# Adding to this dictionary is what I'd like to automate when new objects are defined
examples = {a.name : a,
            b.name : b,
            c.name : c,
            # etc...
            }

# User choice
chosen_name = raw_input("Enter eg name: ")
chosen_example = examples[chosen_name]

# Do something with chosen_example . . . 

我熟悉python,但对类做得不多,所以我不确定有什么可能。具有类似结果的替代方法也很好,提前感谢

我过去做过的一件事是让对象将自己添加到类字典中:

class Example:
    objects = {}

    def __init__(self, name, eg):
        Example.objects[name] = self  # self.objects also works
        ...
...
Example.objects[chosen_name]

您可以将dict传递给您创建的所有对象,如下所示:

class Example:
    def __init__(self, name, eg, all_examples):
        self.name = name 
        self.eg = eg
        all_examples[name] = self

all_examples = {}
a = Example("a", 0, all_examples)
b = Example("b", 1, all_examples)
c = Example("c", 2, all_examples)

print(all_examples)  

下面的示例应该是您所需要的

\uuuu init\uuuuu
中,将对象保存到class variables=
Example.\u ALL\u EXAMPLES
,然后您可以通过
Example.\u ALL\u EXAMPLES
访问它。甚至还没有创建这个类的任何实例(它返回
{}

我认为我们应该避免在这里使用全局变量,所以使用类变量会更好

class Example:
    _ALL_EXAMPLES = {}
    def __init__(self, name, eg):
        self.name = name
        self.eg = eg
        Example._ALL_EXAMPLES[self.name] = self
print(Example._ALL_EXAMPLES)
a = Example("a", 0)
b = Example("b", 1)
c = Example("c", 2)
# etc...

print(Example._ALL_EXAMPLES)
输出:

{}
{'a': <__main__.Example object at 0x01556530>, 'b': <__main__.Example object at 0x015564D0>, 'c': <__main__.Example object at 0x01556A50>}
[Finished in 0.163s]
{}
{'a':,'b':,'c':}
[完成时间为0.163s]

只需在构造函数中添加
examples[self.name]=self
,如果不想硬编码类名:
type(self)。\u ALL\u examples[…]
@VPfB我认为只有当
\u ALL\u examples
是int/string/float等时,它才会起作用。我相信这种硬代码会给我们带来好处,我们可以得到一个默认值={},甚至没有创建任何实例。只有子类不同。Iit没有指定子类实例应该注册的位置。没关系,那只是一句话,你的回答很好。