2

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.

2
  • 1
    What/where is method defined? If you're trying to get the name update programmatically, this may help. Commented Sep 18, 2022 at 14:55
  • The parameter for hasattr should be a string, because you don't pass variables to functions, you pass values. Commented Sep 18, 2022 at 15:33

1 Answer 1

1

you should pass a string with the name, so:

if hasattr(self, 'method'):
    self.method()
    return
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I guess I misunderstood the question, but then isn't that just a typo? Or a duplicate of this: How to check whether a method exists in Python?
this was it. Sorry :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.