Question about multiple inheritance, dynamic class creating and instantiation

别来无恙 提交于 2020-03-23 16:55:51

问题


Hello I have the following situation:

  • A specialized class that inherits from two parent class
  • The need to define the most specialized class at run time, based on some information that I get only when I start reading data from a database.

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'])) 

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')

I get the following error:

---------------------------------------------------------------------------
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'

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.


回答1:


The initalization occurs on line 9:

    return func()

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

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


来源:https://stackoverflow.com/questions/60281424/question-about-multiple-inheritance-dynamic-class-creating-and-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!