How to set a class inheritance at instance creation?

Discussion in 'Python' started by glomde, May 29, 2007.

  1. glomde

    glomde Guest

    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
    glomde, May 29, 2007
    #1
    1. Advertising

  2. On May 29, 8:52 pm, 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)


    class WriteStruct:
    def __init__(self, SubClass):
    self._sub = SubClass()
    def Generate(self, vars):
    for var in vars:
    print self._sub.AssignVar()

    This does what you want but isn't inheritance.

    > 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.
    >


    I don't think I understood what you want here.

    Ram
    Ramashish Baranwal, May 29, 2007
    #2
    1. Advertising

  3. glomde

    Guest

    Why not just have Lang1 and Lang2 inherit from WriteStruct as well?

    On May 29, 8:52 am, 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.
    >
    > Cheers,
    >
    > /T
    , May 29, 2007
    #3
  4. glomde

    glomde Guest

    On 29 Maj, 19:27, "" <> 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 <> 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
    glomde, May 29, 2007
    #4
  5. glomde

    glomde Guest

    On 29 Maj, 19:20, Ramashish Baranwal <>
    wrote:
    > On May 29, 8:52 pm, 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)

    >
    > class WriteStruct:
    > def __init__(self, SubClass):
    > self._sub = SubClass()
    > def Generate(self, vars):
    > for var in vars:
    > print self._sub.AssignVar()
    >
    > This does what you want but isn't inheritance.


    This would work I think. Thanks.

    >
    > > 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.

    >
    > I don't think I understood what you want here.
    >

    I just dont want to pass the class in the instancecreation.
    Somhehow in my:

    class Lang1(CoreLang):

    def Function(self):
    WriteStruct().Generate()

    Then somehow this WriteStruct should magically know that has been
    instantiated in Lang1.
    But this is not really needed. Just wondered if it was possible.

    > Ram
    glomde, May 29, 2007
    #5
  6. On May 29, 8:52 pm, 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)


    class WriteStruct:
    def __init__(self, SubClass):
    self._sub = SubClass()
    def Generate(self, vars):
    for var in vars:
    print self._sub.AssignVar()

    > 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.
    >


    I don't think I understood what you want here.

    Ram
    Ramashish Baranwal, May 29, 2007
    #6
  7. glomde

    Stargaming Guest

    glomde schrieb:
    > 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
    >


    If you really need to inherit at runtime, you could utilize `type()`.

    >>> def foo(self, blah):

    .... print self, blah
    ....
    >>> attrs = {'foo': foo}
    >>> cls = type('MyCls', (object,), attrs)
    >>> cls().foo(4)

    <__main__.MyCls object at 0x009E86D0> 4

    Changing ``object`` (the tuple contains all bases) will change the
    parent. But, better stick to previous solutions. :)

    Stargaming
    Stargaming, May 29, 2007
    #7
  8. glomde

    Steve Holden Guest

    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.

    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 ----------------
    Steve Holden, May 29, 2007
    #8
  9. glomde

    Steve Holden Guest

    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.

    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 ----------------
    Steve Holden, May 29, 2007
    #9
  10. glomde

    glomde Guest

    On 29 Maj, 22:45, Steve Holden <> 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 ----------------
    glomde, May 29, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,382
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Andre Meyer
    Replies:
    9
    Views:
    290
    Jacek Generowicz
    Apr 8, 2004
  3. Gerard Flanagan
    Replies:
    3
    Views:
    429
    Terry Hancock
    Nov 19, 2005
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    365
    Martin P. Hellwig
    Mar 26, 2010
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    587
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page