On 29 Maj, 22:45, Steve Holden <[EMAIL PROTECTED]> wrote: > glomde 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. > > You should rethink your program design. > > It seems that when you create a WriteStruct you should really be passing > its __init__() method the class that you want it to be a "subclass" of, > creating an instance of that class, and then using generic delegation to > that subclass (using a modified __getattr__()) to handle methods that > aren't found in the WriteStruct.
This is what I am going to do. For some reason I got stuck to think I needed to solve it with inheritance. Didnt think of the possibility to modify getattr to make the delegation be much nicer. Thanks for the tip > > I can see there are circumstances in which this might not work, but I > believe your current ugly intentions reveal a design smell that you > really need to get rid of if you want a clean program. > > regards > Steve > -- > Steve Holden +1 571 484 6266 +1 800 494 3119 > Holden Web LLC/Ltd http://www.holdenweb.com > Skype: holdenweb http://del.icio.us/steve.holden > ------------------ Asciimercial --------------------- > Get on the web: Blog, lens and tag your way to fame!! > holdenweb.blogspot.com squidoo.com/pythonology > tagged items: del.icio.us/steve.holden/python > All these services currently offer free registration! > -------------- Thank You for Reading ---------------- -- http://mail.python.org/mailman/listinfo/python-list