简体   繁体   中英

Pass a parent class as an argument?

Is it possible to leave a parent class unspecified until an instance is created?
eg something like this:

class SomeParentClass:
    # something

class Child(unspecifiedParentClass):
    # something

instance = Child(SomeParentClass)

This obviously does not work. But is it possible to do this somehow?

You can change the class of an instance in the class' __init__() method:

class Child(object):
    def __init__(self, baseclass):
        self.__class__ = type(self.__class__.__name__,
                              (baseclass, object),
                              dict(self.__class__.__dict__))
        super(self.__class__, self).__init__()
        print 'initializing Child instance'
        # continue with Child class' initialization...

class SomeParentClass(object):
    def __init__(self):
        print 'initializing SomeParentClass instance'
    def hello(self):
        print 'in SomeParentClass.hello()'

c = Child(SomeParentClass)
c.hello()

Output:

initializing SomeParentClass instance
initializing Child instance
in SomeParentClass.hello()

Have you tried something like this?

class SomeParentClass(object):
    # ...
    pass

def Child(parent):
    class Child(parent):
        # ...
        pass

    return Child()

instance = Child(SomeParentClass)

In Python 2.x, also be sure to include object as the parent class's superclass, to use new-style classes.

You can dynamically change base classes at runtime. Such as:

class SomeParentClass:
    # something

class Child():
    # something

def change_base_clase(base_class):
    return type('Child', (base_class, object), dict(Child.__dict__))()

instance = change_base_clase(SomeParentClass)

For example:

class Base_1:
    def hello(self):
        print('hello_1')

class Base_2:
    def hello(self):
        print('hello_2')

class Child:pass

def add_base(base):
    return type('Child', (base, object), dict(Child.__dict__))()

# if you want change the Child class, just:
def change_base(base):
    global Child
    Child = type('Child', (base, object), dict(Child.__dict__))

def main():
    c1 = add_base(Base_1)
    c2 = add_base(Base_2)
    c1.hello()
    c2.hello()

main()

Result:

hello_1
hello_2

Works well in both python 2 and 3.

For more information, see the related question How to dynamically change base class of instances at runtime?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM