[Edu-sig] more simmering debate...
kirby urner
kirby.urner at gmail.com
Wed Apr 20 18:19:15 EDT 2016
>
> Our classes often behave a lot more like objects with a life of their own.
>
>
For example I might do something like this. One could argue this is not
describing an "is a" relationship i.e. how can each member of the landing
party be a "ship".
I'm saying we internalize our type inheritance and "is a" might not apply
in quite the same way in this particular knowledge domain. Keep an open
mind.
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 20 14:56:55 2016
@author: Kirby Urner
10 Cloverfield Lane,
Nowhere, Nebraska
"""
import random
class MotherShip:
attack_mode = False
# note to self, need to learn more Earthling names
earthling_names = ['Alan', 'Helga', 'Ahmed', 'Jerome', 'Achio', 'Kelly']
@classmethod
def toggle(M):
if M.attack_mode:
M.attack_mode = False;
else:
M.attack_mode = True
@classmethod
def spawn(M, n): # size of pod
pod = []
for _ in range(n):
pod.append(M()) # blessings
return pod
def __init__(self): # rarely used except by spawn
self.name = random.choice(self.__class__.earthling_names)
def __repr__(self):
return self.name # we each feel empowered by the whole!
# ship lands...
landing_party = MotherShip.spawn(10) # spawn a swarm of little selves
print("Landing Party:", landing_party)
print("Hostile?: ", landing_party[3].attack_mode)
# what hath triggered their ire? Everything was going so well...
MotherShip.toggle() # each self has a shared collective hive mind
print("Hostile?: ", landing_party[3].attack_mode) # uh oh...
=== Anaconda.console (blank rows added for readability):
runfile('/Users/kurner/Documents/classroom_labs/martians_landed.py',
wdir='/Users/kurner/Documents/classroom_labs')
Landing Party: [Kelly, Kelly, Achio, Kelly, Jerome, Alan, Alan, Helga,
Achio, Alan]
Hostile?: False
< some triggering event? >
Hostile?: True
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20160420/15f9287e/attachment.html>
More information about the Edu-sig
mailing list