[Tutor] metaclass question

Alan Gauld alan.gauld at btinternet.com
Tue Jan 23 00:15:54 CET 2007

"Kim Branson" <kim.branson at gmail.com> wrote in message 
news:DD1B7C36-BF48-4DAF-8DCF-DA30933C095D at gmail.com...
> Hi i'm interested in implementing a factoryclass in python
> What i'd like to do is have my factoryClass produce an instance of a
> class with some methods defined in arguments to the factory class.
> The classes that are produced have many common methods, but a single
> unique method. This method actually is a series of calls to a c++ 
> api.

It sounds like you could create the basic class as normal but
pass in the appropriate function to the init constructor. Then
instead of making it a method create a metjod that calls that
function, something like this:

class C:
    def __init__(self, myfunc):
       self.func = myfunc
    def driver(self,params_as_needed):
       return self.func(params_as_needed)
    def common1(self....)

Now you can create instances like:

def f1(....):...
def f2(...)....

c = C(f1)
d = C(f2)


Is that what you want?

Alan G. 

More information about the Tutor mailing list