Python inheritance with generic object - how to call method only if it exists in that object

Viewed 26

So I have this piece of code:

class QuerySetUpdateOverriden(QuerySet, object):
    def update(self, *args, **kwargs):
        super().update(*args, **kwargs)
        if hasattr(self, method):
            self.method()
            return

I want to override the update method and if the second class that it inherits has a specific method, call that too.

But I get an error on the line containing the if statement saying "name method is not defined".

Why is this happening and how can I fix it?

Thanks.

1 Answers

you should pass a string with the name, so:

if hasattr(self, 'method'):
    self.method()
    return
Related