Home > Enterprise >  Problem with accessing the methods due to Multilevel and Multiple inheritance in python
Problem with accessing the methods due to Multilevel and Multiple inheritance in python

Time:10-22

I have 3 classes as stated below -

AWSClient

This class is responsible for creating the clients for the AWS resources being used in the application.

import boto3


class AWSClient:

    def __init__(self):

        self.region_name = "ap-south-1"
    
    def get_dynamo_client(self):

        try:
            self.dynamo_client = boto3.resource("dynamodb", region_name=self.region_name)
        except BaseException as e:
            raise Exception("Some error!")
        
        return self.dynamo_client
            
    def get_ses_client(self):
        return True

DynamoTable

class DynamoTable(AWSClient):
    """
    This method will use attributes of AWSClients to create dynamo table instance.
    """

    def __init__(self):

        # Now we can access the attributes of AWSClients' __init__() method.
        AWSClients.__init__(self)

    def get_admin_table(self):
        """
        This method will create the client for table Admins.
        """

        try:
            table = super(
                DynamoTable, self).get_dynamo_client().Table("Admins")
        except ClientError as e:
            raise e

BaseModel

class BaseModel(DynamoTable):
    """
    This class will act as a base for all developers while creating models.
    """
    
    def __init__(self):
        
        # Now we can access the attributes of DynamoTable's __init__() method.
        # Additionally, attributes of AWSClient can also be accessed as DynamoTable 
        # is inheriting AWSClient.

        DynamoTable.__init__(self)

The relations among the classes above is -

AWSClient is a super class of DynamoTable and the DynamoTable is a super class of BaseModel.

Now inheriting BaseModel other developers might create their own model classes. However, in some cases the developer's model class might want to get clients of other AWS services like get_ses_client() and it could be done via calling a specific method of class AWSClient().

Here is what I have tried so far -

class InviteAdminsModel(BaseModel, AWSClient):
    
"""
This is a sample model for Admins table.
This must be in the helper module of each API.
All the API specific interation with dynamo should be written in the methods of this class.
"""

def __init__(self):
    """
    This method will have all the clients of dynamo table initialized.
    
    """
    
    BaseModel.__init__(self)
    
    self.admin_table = super(BaseModel, self).get_admin_table()
    self.ses_client = super(BaseModel, self).get_ses_client()

In the implementation above I am able to get the client of admin_table but I am not able to call the method get_ses_client() which belongs to AWSClient(). It throws me an error super(type, obj): obj must be an instance or subtype of type.

Is this the correct implementation for InviteAdminsModel()?

How can I call the methods of AWSClient()?

Should BaseModel() be an abstract class?

CodePudding user response:

Let me point out few mistakes you have made.

class DynamoTable(AWSClients):

I see there is a typo , it must be AWSClient not AWSClients

AWSClient is a subclass of DynamoTable and the DynamoTable is a subclass of BaseModel.

No, it is super classes not subclasses

AWSClient is a superclass of DynamoTable and the DynamoTable is a superclass of BaseModel.

And in your InvoteAdminsModel, you need not use multiple inheritance lie

class InviteAdminsModel(BaseModel, AWSClient):

As, AWSClient is already a parent of MaseModel. Just use

class InviteAdminsModel(BaseModel):

With these modifications, it should work fine. I hope you understood the problem ,and how it gets solved.

  • Related