Skip to content

Instantly share code, notes, and snippets.

@drj11
Created November 3, 2016 14:01
Show Gist options
  • Save drj11/287c9f565d387cbfb96f69265c6f41de to your computer and use it in GitHub Desktop.
Save drj11/287c9f565d387cbfb96f69265c6f41de to your computer and use it in GitHub Desktop.
structure sharing
a=[1]
b=['b']
c=['c']
b.append(a)
c.append(a)
print(b)
print(c)
b[1].append('xyz')
print(b)
print(c)
@sparkslabs
Copy link

>>> a=[1]
>>> b=['b']
>>> c=['c']
>>> b.append(a)
>>> c.append(a)
>>> print(b)
['b', [1]]
>>> print(c)
['c', [1]]
>>> b[1].append('xyz')
>>> print(b)
['b', [1, 'xyz']]
>>> print(c)
['c', [1, 'xyz']]

Numbers are line numbers above

(remember python values are all objects, so being more specific below)

  1. Create a list [1], and stick the label a onto that value. Below refers to this specific object as A. (Remember [1] is a representation, not the value)
  2. Create a list ['b'] and stick the label b onto that value. Below refers to this specific object as B
  3. Create a list ['c'] and stick the label c onto that value. Below refers to this specific object as C
  4. Find the value that "a" is stuck onto ( A ) and use it as an argument to a function call. Find the value that "b" refers to it ( B ), and find the value that the label "append" is stuck onto ( append func ). Treat that value ( append func ) as a function and pass it the two specific values ( B ) and ( A ) we found earlier. append_func takes the value A and appends it to the object B. (Current representation. of object B : ['b', [1] ] ) Note, that this is because the label b[0] refers to the value "b" and the label b[1] is stuck onto the value A
  5. Find the value that "a" is stuck onto ( A ) and use it as an argument to a function call. Find the value that "c" refers to it ( C ), and find the value that the label "append" is stuck onto ( append func ). Treat that value ( append func ) as a function and pass it the two specific values ( C ) and ( A ) we found earlier. append_func takes the value A and appends it to the object B. (Current representation. of object C : ['c', [1] ] ) Note, that this is because the label c[0] refers to the value "c" and the label c[1] is stuck onto the value A
  6. displays ['b', [1] ]
  7. displays ['c', [1] ]
  8. Boils down to find the value that label b[1] is stuck on - which is the value A - and append the value "xyz" to the end of A.
  9. Since b[0] is stuck on "b" and b[1] is stuck on A, this displays: ['b', [1, "xyz"] ]
  10. Since c[0] is stuck on "c" and c[1] is stuck on A, this displays: ['c', [1, "xyz"] ]

Remember "=" means "stick the label on". Therefore as odd as it sounds, b[0] is a label, as is b[1], as is c[0], c[1], etc.

It just happens that the variable b and c also understand how to remember what these labels what values are stuck to.

@drj11
Copy link
Author

drj11 commented Nov 3, 2016

I think if you drew a diagram it would become clear that: you don't really use that model above; or, it's terrible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment