Python Inheritance

Python Inheritance

·

2 min read

POO is awesome. I have worked with django some time ago and recently I thougth about ViewSets and its pagination system.

Let's imagine a web service that is going to return some paginated data like this:

{
    "count": 5,
    "page": 1,
    "data": [
        {
            "coin": "BTC",
            "price": "60k"
        },
        {
            "coin": "ETH",
            "price": "3k"
        }
    ]
}

And with your favorite Javascript framework you can display it on a data table with its paginator.

Now, we are not going to do a paginator, instead of that, using python classes let's build a endpoint service.

First call our entrypoint

    get_response('currency', 'GET')

And now...

urls = [
    ('currency', CurrencyView)
    # other urls...
]

class Error404(Exception):
    pass

def get_response(url, method):
    endpoint = list(filter(lambda x: x[0] == url, urls))
    if not endpoint:
        raise Error404

    return endpoint[-1][-1]().dispatch(method) # remember this

There the important thing is in our urls var. Let's take a look of CurrencyView class.

class CurrencyView(ModelView):
    paginator_class = MyPaginator('Custom paginator')

    def list(self):
        print(self.reference) # and this
        return super().get()

As we could see in our urls we setted CurrencyView and we accesed to .dispatch method. But in CurrencyView definition that method is not displayed. It means that that method was inherited.

class ModelView:
    def __init__(self):
        # it was used in CurrencyView class
        self.reference = "My model view" 

    def dispatch(self, method): # Here is it!
        if method == 'GET':
            return self.get()

    def get(self):
        """
            Look that paginator_class was defined in
            CurrencyView, the child class.
        """
        return self.paginator_class.paginate()

This is great because you can use .dispatch to manage all the HTTP methods like POST, PUT, DELETE etc. without define to each one in other 'child' endpoints.

Now see our previous CurrencyView class. Do you remeber print(self.reference)? We used the ModelView atrribute instance in its child.

And finally, let's take a look our MyPaginator class.

class MyPaginator:
    def __init__(self):
        self.name = ""

    def paginate(self):
        if method == 'GET':
            print(self.name)
            return "Data about currencies..."

In our previous class we used self.paginator_class.paginate(). There we used the class atrribute from his child class and then was used the method from MyPaginator.

Maybe it's like make harder all. But as we realized we can save a loooot of code using classes.

As conclusion we can say that in this example we can use both class attributes and instance atributtes from parent to children classes and vice versa.

I did this post because I didn't know that! I used it only from parent to child class, but backwards is awesome and very useful too.