selecting base class from user input

Jackson jackson at hotmail.com
Mon Aug 14 13:37:50 EDT 2006


Maric Michaud wrote the following on 2006-08-14 01:26:
> In [28]: class Animal(object) :
>    ....:     _types = {}
>    ....:
>    ....:
> 
> In [29]: class Worker(object) :
>    ....:     def work(self) : print 'hard'
>    ....:
>    ....:
> 
[snip]
> What you are trying to achieve is more commonly done by agregation and 
> delegation :
> 
> In [47]: class Lion(Animal) :
>    ....:     def __init__(self, *classes) :
>    ....:         self._objects = tuple(c() for c in classes)
>    ....:     def isA(self, class_) :
>    ....:         return class_ in (type(o) for o in self._objects)
>    ....:     def __getattr__(self, name) :
>    ....:         for obj in self._objects :
>    ....:             try: return getattr(obj, name)
>    ....:             except: pass
>    ....:         raise AttributeError('not defined or found in objects "%s"' % 
> name)
>    ....:
>    ....:
> 
> In [48]: Lion().work()
> ---------------------------------------------------------------------------
> exceptions.AttributeError                            Traceback (most recent 
> call last)
> 
> /home/maric/<ipython console>
> 
> /home/maric/<ipython console> in __getattr__(self, name)
> 
> AttributeError: not defined or found in objects "work"
> 
> In [49]: Lion().isA(Worker)
> Out[49]: False
> 
> In [50]: Lion(Worker).isA(Worker)
> Out[50]: True
> 
> In [51]: Lion(Worker).work()
> hard
> 

This is exactly what I am looking for.  However, I am not sure how to
implement different Worker methods.  For example, a Lion might work
differently than an Bee. In my example, the Lion would take a cat-nap
while the Bee might do a dance.

It seems that I would need to what kind of class called the work()
method.  Is there a way to do that?

Even if I could do that, it seems these various definitions of work
should probably go into the class of the animal---so that Lion actions
are all within the Lion class.  Thus, the Lion class should have its own
work method, and the Bee class should have its own work method as well.
 The problem with this is that every Lion can use the work method, when
I really only work Workers to use the work method.

I can picture another way of achieving this...have a list of
occupations...which are booleans for each instance of the class.  Then
the work() method will call only if the Worker boolean is True.  This
should be sufficient...and the differing work methods would be in their
respective classes. However, now the actual method names are not
uniform---that is, it becomes a bookkeeping exercise to remember that
when Worker is True, then the method to create is work(), that when
Student is True, then the method to create is study().  So this
procedure has its own problems too.  It seems like I am trading off
hardships now.

So here is what I am looking for:

A single Worker class with a standardized set of method names. The
methods in the Worker class are dependent on the "superclass" (via
aggregation and delegation, as shown above) of the worker.  That is, a
Bee performs different actions when working than a Lion or a Human.  And
finally, the occupations such that "not every Bee is a worker" and
"there are some Workers which are Bees".

Thanks!



More information about the Python-list mailing list