Skip to content

Instantly share code, notes, and snippets.

@asduser
Created August 17, 2016 18:53
Show Gist options
  • Save asduser/53712dd4c3615f3198c673422f92c372 to your computer and use it in GitHub Desktop.
Save asduser/53712dd4c3615f3198c673422f92c372 to your computer and use it in GitHub Desktop.
A simple code, which explains the process of entity status changing by using of private and public methods.
#!/usr/bin/env python
class Fruit:
def __init__(self):
self.__state__ = False
def isEaten(self):
return self.__state__
def consume(self):
self.__state__ = True
class Person:
counter = 0
def eat(self, fruit):
if not fruit.isEaten():
fruit.consume()
self.counter += 1
f1 = Fruit()
f2 = Fruit()
p1 = Person()
p1.eat(f1)
p1.eat(f1)
p1.eat(f2)
print(p1.counter) # should be 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment