Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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_Dictionary_Updates - Fatal编程技术网

将Python对象的历史更新为字典并保留第一项

将Python对象的历史更新为字典并保留第一项,python,class,dictionary,updates,Python,Class,Dictionary,Updates,我不熟悉课堂编程。我试图将对象的初始属性(即字典)保存在其历史记录中,然后用属性的更改更新历史记录。代码如下: import datetime import pytz class Property: """ Simple property class """ @staticmethod def _current_time(): utc_time = datetime.datetime.utcnow() return pytz.utc.

我不熟悉课堂编程。我试图将对象的初始属性(即字典)保存在其历史记录中,然后用属性的更改更新历史记录。代码如下:

import datetime
import pytz

class Property:
    """ Simple property class """ 

    @staticmethod
    def _current_time():
        utc_time = datetime.datetime.utcnow()
        return pytz.utc.localize(utc_time)

    def __init__(self, link, attributes):
        self._link = link
        self.__attributes = attributes
        self._history = [(Property._current_time(), attributes)]

    def update(self, new_attributes):
        def dictdiff(d1, d2):                                              
            return dict(set(d2.items()) - set(d1.items()))
        attr_change = dictdiff(self.__attributes, new_attributes) 
        self.__attributes.update(attr_change)
        self._history.append((Property._current_time(), attr_change))

    def show_attributes(self):
        return self.__attributes

    def show_history(self):
        # how to show changes in time?
        return self._history


prop = Property(1234, {"Price":3000, "Active":"Yes"})
prop.update({"Price":2500, "Active":"Yes"})
prop.update({"Active":"No"})
prop.show_history()
和输出:

Out[132]: 
[(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
  {'Price': 2500, 'Active': 'No'}),
 (datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
  {'Price': 2500}),
 (datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
  {'Active': 'No'})]
Out[132]:
[(datetime.datetime(2018,10,28,10,24,19712385,tzinfo=),
{'Price':2500,'Active':'No'}),
(datetime.datetime(2018,10,28,10,24,19712385,tzinfo=),
{'Price':2500}),
(datetime.datetime(2018,10,28,10,24,19712385,tzinfo=),
{'Active':'No'}]
历史上的第一项事实上应该是:

(datetime.datetime(2018, 10, 28, 10, 24, 19, 712385, tzinfo=<UTC>),
  {"Price":3000, "Active":"Yes"})
(datetime.datetime(2018,10,28,10,24,19712385,tzinfo=),
{“价格”:3000,“有效”:“是”})

我试过了,但没有成功。似乎init函数在更新属性时正在更新初始化的历史记录,同时,在历史记录中,首先我希望看到第一次初始化的属性。

问题在于,您正在使用以下代码修改历史记录中的字典:

self.__attributes.update(attr_change)
你基本上是这样做的:

>>> attributes = {}
>>> history = [attributes]
>>> attributes.update(foo=3)
>>> history
[{'foo': 3}]
通过在历史记录中存储词典副本,可以轻松解决此问题:

self._history = [(Property._current_time(), attributes.copy())]
另请参见。

您希望第一个历史记录条目是
属性的副本

self._history = [(Property._current_time(), dict(attributes))]

正如您现在的代码一样,第一个历史记录条目引用了当前的属性字典。

可能是错误的重复,但您的问题基本相同: