-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreference_sharing_solved.py
More file actions
21 lines (14 loc) · 919 Bytes
/
Copy pathreference_sharing_solved.py
File metadata and controls
21 lines (14 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Stødte på et problem hvor at Variable Ingredients_needed = self.recipes
# ville peje på det samme objekt i pc's hukommelse, og derfor gjorde at anden drone produceret ikke krævede nogle ingredients. "reference sharing/ aliasing".
a = [1, 2, 3] # `a` points to a list in memory
b = a # `b` now points to the same list as `a`
b.append(4) # Modify the list through `b`
print(a)
#----------------------------------------------------------------
x = [1, 2, 3] # `x` points to a list in memory
y = x[:] # `y` points to a new list that is a copy of `x`
#y = x.copy() # Create a shallow copy of the dictionary
y.append(4) # Modify the list through `y`
print(x) # `x` remains unchanged
#det er en måde at løse problemet med reference sharing på
#en anden måde er at hente informationen fra dictionariet eller listen dynamisk. Ved fx at hver gang man henter den så henter man daten fra en fil.