excerpt from real world python class
When I'm not doing exotic Martian Math summer school, sharing weird Portland math no one knows if not an American literature buff (1900s New England Transcendentalism), I'm teaching night school Intro to Python, in California, or nationwide, online. Tonight I was saying that whereas Python is named for Monty Python, I think Greek Mythology probably has a long half-life (it's not either/or anyway), so I'm into exploring Vaults of Parnassus etc., not a new idea among Pythonistas (to link to Greek myths). This is to explain some of the comments.
From tonight's summary meetup (last session, 10 of 10):
===== #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Jul 12 16:39:32 2018 @author: Kirby Urner """ class I: """ a goofy goofy I normally Pythonistas say self not me, but hey, it's just a placeholder. Free country. """ def __init__(me): me.stomach = [ ] # empty (subclasses use it) me.energy = 100 def __repr__(self): return "I live at {} on Memory Lane".format(id(self)) def __str__(me): return "Well hello, I'm a self" def selfie(me): "Click!" return me def __enter__(me): """ context manager I am """ return me.selfie() # same as return self def __exit__(me, *oops): if oops[0]: return False # woopsy, sorry about that return True def __del__(me): print("I now have one less refcount!") if __name__ == "__main__": with I() as me: print(me) print([me]) # me sticks around even after context ends other = I() print(other, "too") print([other]) del me del other ========== #!/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 https://en.wikipedia.org/wiki/Pythia """ def __del__(self): # https://en.wikipedia.org/wiki/Athena_Parthenos 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 ======= OUTPUT: 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!
participants (1)
-
kirby urner