selecting base class from user input

Jackson jackson at hotmail.com
Mon Aug 14 14:04:33 EDT 2006


John Machin wrote the following on 2006-08-14 01:45:
> Here are a couple of thoughts that *might* help:
> 
> (1) mix-in i.e. a class can have multiple base classes:
> 
> class AntWorker(Animal, Worker):
> 
> (2) you can create classes on the fly using the 3-argument form of the
> built-in type() function:
> 
> new_cls = type(name_of_class, base_classes_tuple, dict_of_methods_etc)
> 

This seems like it should work. The only problem I have with it is that
there are a _lot_ of classes to define (and remember).  For 256
different animals, we'd have to create 256 more animal-worker classes.
Obviously this works, but it seems not to be in the spirit of classes
and subclasses in that it doesn't (in some central way) highlight that
there is just one occupation: a worker.

As another example, suppose we had Shakespeare's Romeo and Juliet.  What
I mean is that we have the "idea" (the storyline, themes, etc) of Romeo
and Juliet. Further, suppose that we have various ways of expressing the
piece:  poem, book, movie, broadway.

It is true, we could do:

a = RaJPoem()
a = RaJBook()
a = RaJMovie()
a = RaJBroadway()

but it would be nice if we could do something like this:

a = RaJ(Poem)
a = RaJ(Book)
a = RaJ(Movie)
a = RaJ(Broadway)

And then a method call to RaJ might do something different for each
media. For example,

a.get_opening()

should fetch the opening of each media(first stanza, first chapter,
first scene, etc).  Thus, Poem, Book, Movie, and Broadway should
probably have a get_opening() method, and the RaJ class should pass this
call onto the respective class.  Notice, get_opening() is not exclusive
to RaJ.  Maric's method works nicely for this.

Additionally, I might want some methods which are exclusive to RaJ. For
example,

a.get_love()

would fetch elements of love from each type of media. Clearly this
method depends on the type of media. And there is not one method that
each media class could call.

For a poem, the RaJ class might look for a specific way that love can be
expressed (specific to RaJ). Studpid example, look for the word 'love'
at the end of each line.

For a movie, we might look for any scenes where the couple kisses.

The point is that there are methods for which the set of calls will
differ depending on the media type (poem, book, movie, etc).

This seems like a fun idea to me, and I'd like to think that things like
this are done frequently.

Thanks.



More information about the Python-list mailing list