Last active
February 19, 2019 12:09
-
-
Save grierson/f38d5a5044a72f7f475baae8876a53de to your computer and use it in GitHub Desktop.
Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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