Skip to content

Instantly share code, notes, and snippets.

@grierson
Last active February 19, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grierson/f38d5a5044a72f7f475baae8876a53de to your computer and use it in GitHub Desktop.
Save grierson/f38d5a5044a72f7f475baae8876a53de to your computer and use it in GitHub Desktop.
Problems
# If objects share object then it breaks Encapsulation
# A -> C
# B -> C
# A and B are sharing C state, breaking the encapsulation of parent Objects (A, B).
# Marionettes problem - https://youtu.be/VSdnJDO-xdg?t=1953
class Animal:
def __init__(self, name):
self.name = name
class Person:
def __init__(self, name, pet):
self.name = name
self.pet = pet
fluffy = Animal("fluffy")
alice = Person("Alice", fluffy)
bob = Person("Bob", fluffy)
alice.pet.name = "Charlie"
print(bob.pet.name) # Returns "Charlie" even though it was "Fluffy" when bob was assigned pet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment