将父类作为参数传递? [英] Pass a parent class as an argument?

查看:39
本文介绍了将父类作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在创建实例之前不指定父类?
例如像这样:

Is it possible to leave a parent class unspecified until an instance is created?
e.g. 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?

推荐答案

你有没有尝试过这样的事情?

Have you tried something like this?

class SomeParentClass(object):
    # ...
    pass

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

    return Child()

instance = Child(SomeParentClass)

在 Python 2.x 中,还要确保包含 object 作为父类的超类,以使用新样式的类.

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

这篇关于将父类作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆