Skip to content

Instantly share code, notes, and snippets.

@josephmosby
Last active August 29, 2015 14:08
Show Gist options
  • Save josephmosby/5def758af0e420b7b557 to your computer and use it in GitHub Desktop.
Save josephmosby/5def758af0e420b7b557 to your computer and use it in GitHub Desktop.
Lazy Initialization
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