Last active
August 29, 2015 14:08
-
-
Save josephmosby/5def758af0e420b7b557 to your computer and use it in GitHub Desktop.
Lazy Initialization
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 Fruit: | |
def __init__(self, sort): | |
self.sort = sort | |
class Fruits: | |
def __init__(self): | |
self.sorts = {} | |
def get_fruit(self, sort): | |
if sort not in self.sorts: | |
self.sorts[sort] = Fruit(sort) | |
return self.sorts[sort] | |
if __name__ == '__main__': | |
fruits = Fruits() | |
print fruits.get_fruit('Apple') | |
print fruits.get_fruit('Lime') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment