在字典中添加新条目将还原以前输入条目的属性值

2024-09-21 03:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了这样一个问题:在字典中添加一个带有新键的新条目将还原以前更改的属性的值。你知道吗

import list_territories as territories

class XmlGeneratorApp:
    def __init__(self, parent=None):    
        self.territory_count = 1

    def add_territories(self):
        self.territory_count += 1
        self.new_entry_name = str("territory%d" % self.territory_count)
        self.new_entry = territories.Territories
        territories.terr_dict[self.new_entry_name] = self.new_entry

    def set_product_price_hd_check_2_false(self, territory_number):
        self.territory_number = territory_number
        territories.terr_dict["territory2"].product_price_hd_checks = False

以下是您的领土列表:

terr_dict = {}

class Territories(object):
    def __init__(self,product_price_hd_checks):
        self.product_price_hd_checks = product_price_hd_checks

如果我调用set_product_price_hd_check_2_false"territory2"设置为False,然后调用add_territories,它会将属性terr_dict["territory2"].product_price_hd_checks的值重置为True。如果我调用add_territories来创建多个其他实例(territory3、territory4、territory5等),也是一样的。你知道吗

只有在我的字典里添加新词条时才会发生这种情况。你知道吗

我错过什么了吗?在字典中添加新条目会将值重置为init,这是已知的问题吗?你知道吗


Tags: selfnew字典initdefcountproductprice

热门问题