我可以在 __init__ 函数中有条件地从一组固定的父类中选择一个类的父类吗? [英] Can I choose the parent class of a class from a fixed set of parent classes conditionally in the __init__ function?

查看:51
本文介绍了我可以在 __init__ 函数中有条件地从一组固定的父类中选择一个类的父类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子类,有两个父类,函数名称相同.

I have a child class with two parents with identical function names.

我需要在构造函数 (__init__) 中决定(基于正在创建的对象的类型)哪些父类的函数需要被子类的对象调用.有没有办法将默认函数调用动态链接/切换到其中一个父对象的调用.下面给出了示例代码.我不想使用容器运送对象,因为它很难维护和重构.

I need to decide (based on type of object being created) within the constructor (__init__) that which parent class' functions need to be called by the object of child. Is there a way to dynamically link/switch default function calls to that of one of the parents. Sample code given below. I don't want to use container-ship of objects as its very difficult to maintain and refactor.

class Square: 
 def perform(self, num):
     print(num * num)

class Cube: 
 def perform(self, num):
     print(num * num * num)

class Exponent(Square, Cube):
 def __init__(self, type):
    if type == 'square':
        # inherit default functions from square <--- ??
    else:
        # inherit default functions from cube <--- ??

    square = Square()  # <-- i don't want to do this
    cube = Cube()      # <-- i don't want to do this

Exponent('cube').perform(2)    # --> should print '8'
Exponent('square').perform(2)  # --> should print '4'

Exponent('cube').cube.perform(2)      # <-- i don't want to do this
Exponent('square').square.perform(2)  # <-- i don't want to do this

下面给出了一种方法,但涉及复制所有父类函数,这太过分了:

One way is given below but involves duplicating all parent class functions which is way too much overkill:

class a:
    def x (self):
        print('a')

class b:
    def x (self):
        print('b')

class c(a,b):
    def __init__(self, type_):
        if type_ == 'a':
            self.ref = a
        else:
            self.ref = b

    def x(self):
        self.ref.x(self)


c('a').x()
c('b').x()

推荐答案

Python 提供了很大的灵活性,但您正在反对该语言提供的类机制.没有办法不做任何额外的努力"来做到这一点,因为您将在语言提供的基础上实现自己的机制.老实说,忘记使用多重继承吧,因为继承不是你所描述的,你需要一个委托给适当对象的代理对象.根据您的具体情况,这可能看起来有所不同,但这会让您继续前进:

Python offers a lot of flexibility, but you're working against the class mechanisms the language offers. there is no way to do this "with out any additional effort" since you will be implementing your own mechanisms on top of what the language offers. Honestly, just forget about using multiple inheritance, because inheritance is not what you are describing, you want a proxy object that delegates to the appropriate object. Depending on your specific circumstances, this could look different, but this will get you going:

In [1]: class A:
   ...:     def x (self):
   ...:         print('a')
   ...:
   ...: class B:
   ...:     def x (self):
   ...:         print('b')
   ...:
   ...: class C:
   ...:     def __init__(self, type_, *args, **kwargs):
   ...:         self.__wrapped = type_(*args, **kwargs)
   ...:     def __getattr__(self, attr):
   ...:         return getattr(self.__wrapped, attr)
   ...:
   ...:

In [2]: C(A).x()
a

In [3]: C(B).x()
b

注意,C.__init__ 的实现方式,第一个参数之后的所有内容都传递给委托类型的构造函数.

Note, the way C.__init__ is implemented, everything after the first argument is passed to the delegated type's constructor.

这篇关于我可以在 __init__ 函数中有条件地从一组固定的父类中选择一个类的父类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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