Apr 27th, 2005, 10:45 PM
|
#10
|
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4 
|
Classes (Long post - sorry)
Quote:
|
Originally Posted by Sane
I have functions for that. See my "I'm proud..." topic. 
|
Gnaaaaaaaah... that way lies insanity once you have a lot of objects
Here's an example set of classes. It would be... interesting... to see how you'd implement it using functions  - One base class, Thing(), which every object class should inherit from
- Two mixins, M_Lightable(), and M_Burnable() which define common behaviours. They don't make objects by themselves, but things can inherit them to inherit their behaviour. M_Burnable() is a subclass of M_Lightable(); that is, it inherits the behaviours of M_Lightable and redefines some of those.
- Three object classes, Lamp(), Paper(), and MrStrawman(). These are game objects. There would probably only be one MrStrawman() instance, but there could be several Lamp() instances, and many Paper() instances. It's likely that these would need other mixins - for instance Paper() would need an M_Readable, and an M_Writeable.
class M_Lightable(object):
def base_setup(self):
self.lit = False
def light(self):
if self.lit:
print "%s is already lit."%self.The
else:
print "You light %s."%self.the
self.lit = True
def extinguish(self):
if self.lit:
print "You extinguish %s."%self.the
self.lit = False
else:
print "%s is not currently lit."%self.The
class M_Burnable(M_Lightable):
def light(self):
print "You light %s and %s quickly turns to ash."%(self.the,
self.pron)
self.light = self._light_fail
def _light_fail(self):
print "%s cannot be lit as %s's burnt to a crisp."%(self.The,
self.pron)
class Thing(object):
def __init__(self):
for base in self.__class__.__bases__:
base.base_setup(self)
self.setup()
def base_setup(self):
self.name = ""
self.pron = "it"
def get_the_name(self):
return "the " + self.name
the = property(get_the_name)
# property() means that when you do use self.The, it will call
# self.get_The_name()
def get_The_name(self):
return "The " + self.name
The = property(get_The_name)
# "self.eat()" could be thought of/rewritten as:
# tmp = self.__getattr__("eat")
# tmp()
# Since there's no eat() method, missing_action() is called.
# This creates and returns a function that self.__getattr__()
# can return. This function is then called.
def missing_action(self, action):
def _action():
print "You can't %s %s."%(action, self.the)
return _action
def __getattr__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError:
return self.missing_action(attr)
def setup(self):
pass
class Lamp(Thing, M_Lightable):
def setup(self):
self.name = "lamp"
class Paper(Thing, M_Burnable):
def setup(self):
self.name = "piece of paper"
class MrStrawman(Thing, M_Burnable):
def setup(self):
self.name = "Mr Strawman"
self.pron = "he"
def get_the_name(self):
return self.name
The = the = property(get_the_name)
lamp = Lamp()
lamp.light()
lamp.light()
lamp.extinguish()
lamp.extinguish()
print
paper = Paper()
paper.light()
paper.light()
paper.extinguish()
print
paper2 = Paper()
paper2.light()
paper2.eat()
print
straw = MrStrawman()
straw.light()
straw.light()
straw.extinguish()
straw.kill()
--OH.
|
|
|