Created
July 28, 2018 03:18
-
-
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
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
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