1

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
  • I see no example of multiple inheritance in your question. I do see two cases of single inheritance from a common base class. Also, a base class can implement an instance method such as getClassName(self) with return self.__class__.__name__. So why are derived classes passing their names to the parent class? I don't know what problem you are trying to solve. Perhaps you can explain in more detail. Commented Feb 18, 2020 at 13:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

After the modification was done, it move further and was able to try it out in a jupyternote book, however when I move it into the actual code, when I try to execute the class gets created, and when I try to instantiate it by passing the 2 required arguments, I get the exception that '__new__()' takes x arguments but y were given, etc... so it seems like __new__() is being called somewhere with the incorrect number of parameters ? Where can I find that ?
@Cracoras Update your question then. Anyway, I have no problem running invoice_cls(123, 'asdf').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.