Python-多重动态继承 [英] Python- Multiple dynamic inheritance

查看:79
本文介绍了Python-多重动态继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让多个动态继承起作用。这些示例对我来说最有意义(此处此处),但是在一个示例中没有足够的代码让我真正了解正在发生的事情,而另一个示例似乎没有可以根据需要进行更改时工作(以下代码)。

I'm having trouble with getting multiple dynamic inheritance to work. These examples make the most sense to me(here and here), but there's not enough code in one example for me to really understand what's going on and the other example doesn't seem to be working when I change it around for my needs (code below).

我正在创建可与多个软件包一起使用的通用工具。在一种软件中,我需要继承2个类:1个软件特定的API mixin和1个PySide类。在另一个软件中,我只需要继承1个PySide类即可。

I'm creating a universal tool that works with multiple software packages. In one software, I need to inherit from 2 classes: 1 software specific API mixin, and 1 PySide class. In another software I only need to inherit from the 1 PySide class.

我能想到的最简洁的解决方案是仅创建2个单独的类(使用所有相同的方法),然后根据正在运行的软件调用任一方法。我感觉有更好的解决方案。

The least elegant solution that I can think of is to just create 2 separate classes (with all of the same methods) and call either one based on the software that's running. I have a feeling there's a better solution.

这就是我正在使用的东西:

Here's what I'm working with:

## MainWindow.py

import os
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
    
# Build class
def build_main_window(*arg):
    
    class Build(arg):
        def __init__(self):
            super( Build, self ).__init__()

        # ----- a bunch of methods

# Get software
software = os.getenv('SOFTWARE')

# Run tool
if software == 'maya':
    build_main_window(maya_mixin_class, QtGui.QWidget)
if software == 'houdini':
    build_main_window(QtGui.QWidget)

我当前遇到此错误:

#     class Build(arg):
# TypeError: Error when calling the metaclass bases
#     tuple() takes at most 1 argument (3 given) # 

感谢您的帮助!

## MainWindow.py

import os
    
# Build class 
class BuildMixin():
    def __init__(self):
        super( BuildMixin, self ).__init__()

    # ----- a bunch of methods

def build_main_window(*args):
    return type('Build', (BuildMixin, QtGui.QWidget) + args, {})

# Get software
software = os.getenv('SOFTWARE')

# Run tool
if software == 'maya':
    from maya.app.general.mayaMixin import MayaQWidgetDockableMixin

    Build = build_main_window(MayaQWidgetDockableMixin)

if software == 'houdini':
    Build = build_main_window()


推荐答案

原始代码中的错误是由于未能在类定义中使用元组扩展引起的。我建议将代码简化为:

The error in your original code is caused by failing to use tuple expansion in the class definition. I would suggest simplifying your code to this:

# Get software
software = os.getenv('SOFTWARE')

BaseClasses = [QtGui.QWidget]
if software == 'maya':
    from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
    BaseClasses.insert(0, MayaQWidgetDockableMixin)

class Build(*BaseClasses):
    def __init__(self, parent=None):
        super(Build, self).__init__(parent)

更新

上面的代码仅适用于Python 3,因此Python 2似乎需要使用 type()的解决方案。从其他注释看来, MayaQWidgetDockableMixin 类可能是老式的类,因此可能需要这样的解决方案:

The above code will only work with Python 3, so it looks like a solution using type() will be needed for Python 2. From the other comments, it appears that the MayaQWidgetDockableMixin class may be a old-style class, so a solution like this may be necessary:

def BaseClass():
    bases = [QtGui.QWidget]
    if software == 'maya':
        from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
        class Mixin(MayaQWidgetDockableMixin, object): pass
        bases.insert(0, Mixin)
    return type('BuildBase', tuple(bases), {})

class Build(BaseClass()):
    def __init__(self, parent=None):
        super(Build, self).__init__(parent)

这篇关于Python-多重动态继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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