Subclass dynamically

R

Robert Dailey

Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )

Note I'm using Python 3.1 on Windows. Thanks in advance.
 
G

Gary Herron

Robert said:
Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )

Note I'm using Python 3.1 on Windows. Thanks in advance.
Python makes it possible to change base classes, but that doesn't mean
it ever a good idea.

Moreover, the assignment is at the class level, not the instance level:

MyDerived.__bases__ = (base,) #A tuple of bases

would change the base class for the class MyDerived, and all instances
past, present, or future.

Better (but still not good) might be a factory function that derives a
class with the desired base class:

def Derive(base):
class Derived(base):
pass
return Derived

DerivedClass = Derive(MyBase)
foo = DerivedClass(...)

I believe that will produce what you were looking for.

Gary Herron
 
C

Carl Banks

1- Are you sure that you want that behavior? Given that in python, a class is
just a particular case of invocable object, you could just write a function
instead, and the code may be more robust.

2- If you are /sure/ that is the behavior you want, take a look at the __new__
method. The __new__ can decide which instance to return (you could even define a
new class inside of it, instantiate it, and return the instance).

3- You may want to take a look at metaclasses. But without more details about
why you want it, I can't give you more precise answers.

One could accomplish this using methods 2 and 3, but it would go
against so many common expectations I have to recommend never doing
it. If you call a type, it should return an object of that type (or,
at worst, a subtype or "null" type) or raise an exception.

The factory function is the way to do this.

Another alternative is to forego inheritance and simply call method on
the passed "base" object. Given that the OP posted code that almost
literally works in that case, this might be the best way to go for his
case.

So instead of this hypothetical code:

class MyDerived(self.base):
def __init__(self,base)
self.base = base
# MyDerived inherits a_method() from self.base

He could go with this real code:

class MyClass(object):
def __init__(self,base)
self.base = base
def a_method(self):
return self.base.a_method()

where, instead of inheriting self.base's a_method, it simply defines
a_method() to call self.base.a_method() directly.


Carl Banks
 
S

Sergey Simonenko

How can you subclass a class from an object?..
Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )

Note I'm using Python 3.1 on Windows. Thanks in advance.
 
P

Piet van Oostrum

Robert Dailey said:
RD> Hey,
RD> I have a class that I want to have a different base class depending on
RD> a parameter that I pass to its __init__method. For example
RD> (pseudocode):
RD> class MyDerived( self.base ):
RD> def __init__( self, base ):
RD> self.base = base

RD> Something like that... and then I would do this:
RD> foo = MyDerived( MyBase() )

What do you want? As you write it now foo would be an instance of
MyDerived, but you say you want to have a class with a different base
class...

So does this mean that foo should become that class or that foo should
become an instance of a new anonymous class that has a specified base
class?

And on the other hand is MyBase the required base class. But you pass an
instance of MyBase, not MyBase itself. As you have it above MyBase()
should be a class, therefore MyBase should be a metaclass. Or is that
not what you want?
 
B

Bruno Desthuilliers

Robert Dailey a écrit :
Hey,

I have a class that I want to have a different base class depending on
a parameter that I pass to its __init__method. For example
(pseudocode):

class MyDerived( self.base ):
def __init__( self, base ):
self.base = base


Something like that... and then I would do this:

foo = MyDerived( MyBase() )


What is your real use case ? I mean, what problem are you actually
trying to solve this way ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top