【问题标题】:Python - Class instances share class dictionary variable? [duplicate]Python - 类实例共享类字典变量? [复制]
【发布时间】:2018-11-11 07:41:24
【问题描述】:
class Student():

    NAME = ''
    DICT = {}

    def __init__(self, name):
        self.NAME = name
        self.DICT['name'] = name

    def change_DICT(self, change):
        self.DICT['name'] = change


student_one = Student('Leo')
student_two = Student('Liz')

print ('Student one NAME: ' + student_one.NAME)
print ('Student two NAME: ' + student_two.NAME)
print ('---------------------------------')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))
print ('---------------------------------')
student_one.change_DICT('Tom')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))


>> Student one NAME: Leo
>> Student two NAME: Liz
>> ---------------------------------
>> Student one DICT: {'name': 'Liz'}
>> Student two DICT: {'name': 'Liz'}
>> ---------------------------------
>> Student one DICT: {'name': 'Tom'}
>> Student two DICT: {'name': 'Tom'}

我已经玩了几个小时了,但仍然无法理解它。更改Student 类的一个实例的DICT 如何同步更改所有其他实例的DICT

这是怎么发生的?如果我真的需要使用dict,解决这个问题的最佳方法是什么?

【问题讨论】:

  • 除了我的回答:我确实觉得很奇怪self.DICT 仍然可以针对该类变量,所以我不确定为什么会发生这种情况。也许其他人可以启发我们。
  • @BramVanroy self.DICT 不针对该类变量,但 self.DICT['name'] 可以。即使使用预定义的DICT,您的代码也能正常工作。谢谢你的帮助!我会将此标记为重复,因为我在@Cyber​​ Tailor 建议的帖子中找到了我需要的内容。

标签: python-3.x class dictionary instance instance-variables


【解决方案1】:

您可以通过更改DICTNAME 等类属性的初始化位置来实现所需的输出。 我给你举个例子。

class Student():


    def __init__(self, name):
        self.DICT = {}
        self.NAME = name
        self.DICT['name'] = name

    def change_DICT(self, change):
        self.DICT['name'] = change

student_one = Student('Leo')
student_two = Student('Liz')

print ('Student one NAME: ' + student_one.NAME)
print ('Student two NAME: ' + student_two.NAME)
print ('---------------------------------')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))
print ('---------------------------------')
student_one.change_DICT('Tom')
print ('Student one DICT: ' + str(student_one.DICT))
print ('Student two DICT: ' + str(student_two.DICT))

这将给出如下输出:

Student one NAME: Leo
Student two NAME: Liz
---------------------------------
Student one DICT: {'name': 'Leo'}
Student two DICT: {'name': 'Liz'}
---------------------------------
Student one DICT: {'name': 'Tom'}
Student two DICT: {'name': 'Liz'}

在这里,您将检查字典值是否已更改。

【讨论】:

    【解决方案2】:

    您正在混合类变量和实例变量。当你在构造函数之外声明一个变量时(即上面__init__)它是一个类变量,意味着它是共享的。相反,你只需要实例变量,如下所示。

    class Student():
        def __init__(self, name):
            self.NAME = name
            self.DICT = {'name': name}
    
        def change_DICT(self, change):
            self.DICT['name'] = change
    
    
    student_one = Student('Leo')
    student_two = Student('Liz')
    
    print ('Student one NAME: ' + student_one.NAME)
    print ('Student two NAME: ' + student_two.NAME)
    print ('---------------------------------')
    print ('Student one DICT: ' + str(student_one.DICT))
    print ('Student two DICT: ' + str(student_two.DICT))
    print ('---------------------------------')
    student_one.change_DICT('Tom')
    print ('Student one DICT: ' + str(student_one.DICT))
    print ('Student two DICT: ' + str(student_two.DICT))
    

    【讨论】:

      猜你喜欢
      • 2021-07-04
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多