Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/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 dict作为类变量的问题_Python_Class_Dictionary_Variables - Fatal编程技术网

Python dict作为类变量的问题

Python dict作为类变量的问题,python,class,dictionary,variables,Python,Class,Dictionary,Variables,在Python中使用字典作为类变量时,我遇到了一些问题。我希望有一个dict类变量(模板),它被添加到每个实例(用户)中,然后每个实例都可以独立地更改它们的dict值。但是,根据我的经验,在一个实例中更改dict值也会更改模板类变量和所有其他实例的dict值。整型变量的情况并非如此,所以我很好奇为什么DICT作为类变量会表现出这种行为 为了举例说明,我在下面组装了一个示例代码,其中有两个用户(在初始化时)被分配了“他们自己的”字典。向用户添加值时,此字典由模板dict填充,以便用户可以彼此独立地

在Python中使用字典作为类变量时,我遇到了一些问题。我希望有一个dict类变量(模板),它被添加到每个实例(用户)中,然后每个实例都可以独立地更改它们的dict值。但是,根据我的经验,在一个实例中更改dict值也会更改模板类变量和所有其他实例的dict值。整型变量的情况并非如此,所以我很好奇为什么DICT作为类变量会表现出这种行为

为了举例说明,我在下面组装了一个示例代码,其中有两个用户(在初始化时)被分配了“他们自己的”字典。向用户添加值时,此字典由模板dict填充,以便用户可以彼此独立地修改值。或者,至少这是它应该举例说明的

class User:
    template = {"sad": 0, "happy": 0, "meloncoli": 0}

    def __init__(self):
        self.emo = {}
    
    def new_dict(self, user_input):
        # add the template to the current instance
        self.emo = self.template
        # if the input values (strings) are in the template, then add 1 instead of 0 as value.
        for i in range(len(list(user_input.values())[0])):
            if list(user_input.values())[0][i] in self.emo:
                self.emo[list(user_input.values())[0][i]] = 1
        

user1 = User()
user_input = {
    6: ["sad", "happy"],
    }
user1.new_dict(user_input)
print(user1.emo)


user2 = User()
user_input = {
    6: ["sad"],
    }
user2.new_dict(user_input)
print(user2.emo)

我在这里得到的输出是:

{'sad': 1, 'happy': 1, 'meloncoli': 0}
{'sad': 1, 'happy': 1, 'meloncoli': 0}
然而,我应该得到:

{'sad': 1, 'happy': 1, 'meloncoli': 0}
{'sad': 1, 'happy': 0, 'meloncoli': 0}
这表示在第一个实例中更改dict值会全局更改它们。有没有人有这方面的经验?我想一个解决方案是在init方法中添加模板dict,但我不确定在这些情况下什么是最佳实践


希望有人能给我一些启发。

我已经解决了下面的问题-不需要使用类变量

class User:

    def __init__(self):
        self.emo = {"sad": 0, "happy": 0, "meloncoli": 0}

    def new_dict(self, user_input):
        # if the input values (strings) are in the template, then add 1 instead of 0 as value.
        for i in range(len(list(user_input.values())[0])):
            if list(user_input.values())[0][i] in self.emo:
                self.emo[list(user_input.values())[0][i]] = 1
运行以下命令:

user1 = User()
user_input = {
    6: ["sad", "happy"],
    }
user1.new_dict(user_input)
print(user1.emo)


user2 = User()
user_input = {
    6: ["sad"],
    }
user2.new_dict(user_input)
print(user2.emo)
这将产生:

{'sad': 1, 'happy': 1, 'meloncoli': 0}
{'sad': 1, 'happy': 0, 'meloncoli': 0}

Python中的赋值从不复制任何内容
self.emo=self.template
只是为单个现有dict提供了第二个名称。
self.emo=self.template.copy()
将是一个解决方案。@jasonharper,该解决方案很好用。非常感谢。但我上面的例子适用于整数变量。你知道这是为什么吗?整型是不可变的——任何改变其值的操作都必须将一个全新的值赋给一个特定的变量名,断开该名称与共享原始值的其他名称之间的任何连接。只有可变值(如dict)可以就地修改;通过引用同一对象的任何其他名称都可以看到这样的更改。