a "generic" problem

Erik Max Francis max at alcyone.com
Wed Feb 12 19:41:25 EST 2003


Sean Ross wrote:

> ISSUE 1:
> "How do I construct new instances of a class
> when I do not know the name of that class?"

This is usually referred to as a "factory pattern."  What you do is pass
in a thing that can generate the desired instances that you want.  In
Python, the most obvious thing to do is to pass in the class that you
want, and then invoke the class with normal call conventions.

> ISSUE 2:
> "How do I implement this method, so that I do not need to
> override it for each sublass?"

Same way, probably; pass in the factory object that you plan to use.

> ISSUE 3:
>    "Is there a way to overide the value of a class variable of the
> super
> class
>     inside the inheriting class?"

Sure.  When you have an instance of the class, refer to it like this:

	instance.classAttribute, 

e.g., self.classAttribute if you're actually in one of that instance's
methods.

and then just replace it in subclasses:

	class C:
	    classAttribute = something

	class D(C):
	    classAttribute = somethingElse

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If you can't fight and you can't flee, flow.
\__/ Robert Elliot
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.




More information about the Python-list mailing list