Changyu Lee

Dev Log 02.18.24 : Python Object Reference

Published at
2024/02/18
Last edited time
2024/02/17 21:16
Created
2024/02/17 21:14
Section
Dev Log
Status
Done
Series
Tags
Basic
AI summary
Keywords
Python
Language

Goal

element update (memory strength += 1)
for i in docs: dict_doc = json.loads(i.page_content) dict_doc['memory_strength'] += 1 i.page_content = json.dumps(dict_doc, indent=4, ensure_ascii=False) #type(docs) is List(Document) if i.metadata['tag'] == "StoryMemory": for j in self.story_mem: if i.metadata['id'] == j.metadata['id']: #j 요소 삭제 self.story_mem.remove(j) self.story_mem.append(i.copy()) break elif i.metadata['tag'] == "CharacterRelation": for j in self.characters_relation_mem: if i.metadata['id'] == j.metadata['id']: self.story_mem.remove(j) self.story_mem.append(i.copy()) break elif i.metadata['tag'] == "CharacterStatus": for j in self.characters_status_mem: if i.metadata['id'] == j.metadata['id']: self.story_mem.remove(j) self.story_mem.append(i.copy()) break
Python
복사
Working Codes

Explaination

The primary goal of the provided code is to iterate through a list of documents (docs), increase the memory_strength value of each document by one, and then update specific documents within certain lists based on their tags. The tags considered in this code are "StoryMemory", "CharacterRelation", "CharacterStatus". Depending on the tag, the document is updated in the corresponding list.
The approach used in the code:
It first increases the memory_strength value of each document and updates the document's content.
For documents with specific tags, it finds and removes the old version from the relevant list and adds a new version of the document.
The reason for using self.story_mem.remove(j) and self.story_mem.append(i.copy()) is to opt for a method where a new copy of the object is created and added to the list, rather than directly manipulating the reference of the object. This approach has several benefits:
1.
Maintaining Immutability: By creating a new copy of the object rather than modifying it directly, you can maintain the immutability of the original object. This is particularly useful in complex systems or in cases where there is a lot of interaction between different components, as it helps prevent unintended side effects.
2.
Clarity in State Management: Adding a new object helps clearly indicate changes in the system's state. That is, it becomes easier to track what has changed, and when.
3.
Preventing Errors Due to Reference: Directly modifying an object could affect other variables or objects that reference it. Using a new copy prevents these kinds of side effects due to shared references.
The reason for not using j = i.copy() is because, while this would update the j variable to refer to a new object, it does not affect the original list (self.story_mem, etc.) in any way. In other words, updating j to a new object leaves the original object in the list unchanged. Therefore, to actually update an object in the list, you need to explicitly remove the old object and add the modified new object to the list. This ensures that the list reflects the most current state of its elements.