dynamic

Kent Johnson kent37 at tds.net
Wed Jun 15 09:12:10 EDT 2005


Riccardo Galli wrote:
> Hi all.
> It's easier if I show an example first.
> 
> Say I have
> 
> class A(object):
>     def speak(self): print 'A!'
> 
> class B(object):
>     def speak(self): print 'B!'
> 
> I also have a factory function like this.
> 
> def foo(kind,*args,**kwds):
>    if kind=='a': return A(*args,**kwds)
>    else: return B(*args,**kwds)
> 
> I need foo to be a class, so that I could inherit from it and still use
> it as a factory, so that I can do:
> 
> Foo('a').speak()
> Foo('b'.speak()
> 
> class Final(Foo):
>    def __init__(self,kind,*args,**kwds):
>        super(Foo,self).__init__(kind,*args,*kwds)

I'm not clear why you want to do this. Presumably you want to be able to say
Final('a').speak() ?? 
How will this differ from
Foo('a').speak()
Why does Final need to subclass Foo?

Can you use either of these syntaxes? Either one can be specialized in a subclass:
Foo()('a').speak() # use Foo.__call__() as factory
Foo.make('a') # Use classmethod as factory

Kent



More information about the Python-list mailing list