How to set a class inheritance at instance creation?

glomde tbrkic at yahoo.com
Tue May 29 13:35:21 EDT 2007


On 29 Maj, 19:27, "shand... at gmail.com" <shand... at gmail.com> wrote:
> Why not just have Lang1 and Lang2 inherit from WriteStruct as well?

This wont work I think since if add antoher Class:

class WriteStruct():

   def func1(self);
     print "Hello2"

   def Generate(self):
      self.func1()

class WriteStruct2(WriteStruct):

   def func1(self);
     print "Hello"

   def Generate(self):
      self.func1()

Den if Lang1, inherit both WriteStruct and WriteStruct2 I will
get name clashes.

In my real code I have very big Generate Method in WriteStruct that
calls submethods.
and thes submethods should be overriden by the subclasses. So I cant
change the name
on the submethods.

>
> On May 29, 8:52 am, glomde <tbr... at yahoo.com> wrote:
>
> > Hi I wonder if you can set what subclass a class should
> > have at instance creation.
>
> > The problem is that I have something like:
>
> > class CoreLang():
> >     def AssignVar(self, var, value):
> >          pass
>
> > class Lang1(CoreLang):
> >      def AssignVar(self, var, value):
> >           return var, "=", value
>
> > class Lang2(CoreLang):
> >      def AssignVar(self, var, value):
> >           return var, "<=", value
>
> > class WriteStruct():
> >      def Generate(self, vars):
> >         for var in vars:
> >              print self.AssignVar()
>
> > The problem is that I want  WriteStruct to sometimes be a subclass of
> > Lang1 and sometimes
> > of Lang2.
> > In the above example I could but the Generate Method in CoreLang. But
> > in my real
> > example I also want to able to subclass WriteStruct to be able to easy
> > customize WriteStruct.
> > Which I wouldnt be able to do if it was a method in CoreLang.
>
> > So in code I would like to write something like:
>
> > WriteStruct(Lang1).Generate(vars)
>
> > Even better would be that if I in the Lang1 class could
> > just do WriteStruct().Generate(vars) and Lang1 class would
> > magically make WriteStruct a subclass of itself.
>
> > Cheers,
>
> > /T





More information about the Python-list mailing list