Skip to content

Instantly share code, notes, and snippets.

@cms-codes
Created July 28, 2018 03:18
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 cms-codes/92e903e572022c6f0d1159e0d5a41abc to your computer and use it in GitHub Desktop.
Save cms-codes/92e903e572022c6f0d1159e0d5a41abc to your computer and use it in GitHub Desktop.
So I wanted to test instantiating an object inside another object and the object's constructor requires a reference of the object creating it so I pass it self
class Tree:
def __init__(self):
self.oranges = []
def grow_orange(self):
# Creating an object by passing it self
orange = Orange(self)
self.add_orange(orange)
def add_orange(self, orange):
self.oranges.append(orange)
def __len__(self):
return len(self.oranges)
class Orange:
def __init__(self, tree):
self.tree = tree
tree = Tree()
print (str(len(tree)))
tree.grow_orange()
print (str(len(tree)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment