有关多重继承,动态类创建和实例化的问题 [英] Question about multiple inheritance, dynamic class creating and instantiation

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

问题描述

你好,我有以下情况:

  • 从两个父类继承的专门类
  • 需要在运行时定义最专业的类,这是基于一些仅在我开始从数据库中读取数据时才得到的信息的.

我定义了以下代码来处理创建链中的所有类:

I defined the following code to handle the create all the classes in the chain:

class BusinessDocument():
    @staticmethod
    def get_class(doc_type):
        switch = {
            'MasterData': MasterData,
            'Transactional': Transactional
        }
        func = switch.get(doc_type, lambda: "Invalid Noun Type")
        return func()

    def __init__(self, doc_id, location, doc_type):
        self.doc_id = doc_id
        self.location = location
        self.doc_type = doc_type
        pass

    @property
    def get_location(self):
        return self.location

    @property
    def get_doc_id(self):
        return self.doc_id

class MasterData(BusinessDocument):
    def __init__(self, doc_id, location):
        BusinessDocument.__init__(self, doc_id, location, 'MasterData')

class Transactional(BusinessDocument):
    def __init__(self, doc_id, location):
        BusinessDocument.__init__(self, doc_id, location, 'Transactional')


class NounClass():
    @staticmethod
    def get_class(doc_name, doc_type):
        return type(doc_name, (BusinessDocument.get_class(doc_type), 
                           BusinessDocument, ),dict.fromkeys(['doc_id', 'location'])) 

然后在运行时获取doc_name并尝试创建一个新类.此时,我可能没有必需的参数 doc_id location ,但是我需要对类型进行分类.

Then at run time when I get the doc_name and I try to create a new class. At this point I may not have the required arguments doc_id and location but I need to class type.

invoice_cls = NounClass.get_class('Invoice', 'Transactional')

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-cb774746875a> in <module>
----> 1 invoice_cls = NounClass.get_class('Invoice', 'Transactional')

<ipython-input-9-aa5e0b316ed1> in get_class(doc_name, doc_type)
     35     @staticmethod
     36     def get_class(doc_name, doc_type):
---> 37         return type(doc_name, (BusinessDocument.get_class(doc_type), 
     38                            BusinessDocument, ),dict.fromkeys(['doc_id', 'location']))

<ipython-input-9-aa5e0b316ed1> in get_class(doc_type)
      7         }
      8         func = switch.get(doc_type, lambda: "Invalid Noun Type")
----> 9         return func()
     10 
     11     def __init__(self, doc_id, location, doc_type):

TypeError: __init__() missing 2 required positional arguments: 'doc_id' and 'location'

我知道这样做的原因是因为__init __()将在类实例化期间被调用,但是我认为该类型只会创建一个新类型,而不会立即实例化一个新类型.所以我的问题是,此时是否有一种方法可以延迟实例的实例化.

I understand that the reason for it is because the __init__() will be called during the class instantiation, but I thought that type would be only creating a new type and not instantiate one right away. So my question is if is there a way to defer the instantiation of the instance at this time.

在此先感谢您的帮助和提示.

Thank you in advance for any help and tips on this.

-MD.

推荐答案

初始化在第9行发生:

    return func()

我假设您想返回一个类对象,因此删除那些寄生对象.

I assume you want to return a class object, so remove those parantheses.

func 也误导了我,我将其更改为 cls :

Also func is misleding, I've changed it to cls:

def get_class(doc_type):
    switch = {
        'MasterData': MasterData,
        'Transactional': Transactional
    }
    cls = switch.get(doc_type, lambda: "Invalid Noun Type")
    return cls

这篇关于有关多重继承,动态类创建和实例化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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