#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 16:56:25 2018
@author: kurner
"""
from selfcentered import I
class Foe(I):
"""
The mythical Python lived under Mt. Parnassus (Παρνασσός)
and its breath (?) gave the Oracle of Delphi her powers
of diagnosis and prognosis
"""
def __del__(self):
print("Apollo thinks I'm dead, but I escaped to Nashville")
global selfcentered
import selfcentered
selfcentered.Python = self # hide out in another module
class Hero(I):
"""
Apollo is supposed to have slain the Python under Mt. Parnassus
"""
def __init__(self):
super().__init__()
print("A hero is born!")
def eat(self, food : str):
if not type(food) == str:
raise ValueError
self.stomach += [food] # yum!
self.energy += 10
def slay(self):
global Python
if self.energy:
del Python
print("Foe slain")
else:
print("I'm too weary to fight. Feed me!")
self.__spend_energy()
def __spend_energy(self):
if self.energy:
self.energy = (self.energy - 10
if self.energy >= 10
else 0)
if len(self.stomach):
print("Digesting {}".format(self.stomach.pop(0)))
def seek(self):
print("seeking foe")
self.__spend_energy()
def __repr__(self):
return "< Hero Energy:{} Stomach:{} >".\
format(self.energy, len(self.stomach))
if __name__ == "__main__":
Python = Foe() # Foe
Apollo = Hero() # Hero
Apollo.eat("Manly Meal")
print(Apollo.stomach)
Hero.seek(Apollo)
Apollo.seek()
print(repr(Apollo))
Apollo.eat("Rats")
Apollo.eat("Beer")
Apollo.seek()
Apollo.seek()
Apollo.seek()
Apollo.seek()
print(repr(Apollo))
Apollo.slay()
print(repr(Apollo))
Apollo._Hero__spend_energy() # remember name mangling?
print(repr(Apollo))
print("Good bye Apollo")
del Apollo
runfile('/Users/mac/Documents/pyt-pr/Session10/heroic_story.py', wdir='/Users/mac/Documents/pyt-pr/Session10')
A hero is born!
['Manly Meal']
seeking foe
Digesting Manly Meal
seeking foe
< Hero Energy:90 Stomach:0 >
seeking foe
Digesting Rats
seeking foe
Digesting Beer
seeking foe
seeking foe
< Hero Energy:70 Stomach:0 >
Apollo thinks I'm dead, but I escaped to Nashville
Foe slain
< Hero Energy:60 Stomach:0 >
< Hero Energy:50 Stomach:0 >
Good bye Apollo
I now have one less refcount!
selfcentered.Python
Out[26]: I live at 4892551320 on Memory Lane
"""LAB 3 UNTIL 10:10"""
Out[20]: 'LAB 3 UNTIL 10:10'
class Dog:
def bark(self):
print("Bark!")
dog = Dog()
dog.bark()
Bark!
Dog.bark(dog)
Bark!