SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Python warning "'str' is not callable" when calling a function

I am getting a warning on the line below stating that: 'str' object is not callable.

Is there anything I can do to make the warning go away?

Thanks

Action.action()

Here is the block of code:

Action = DefineAction()
Action.action()


class DefineAction:
    def action(self):
        self.action = listAction[generateRandomNumber(0,4)]
        return self.action

Here is listAction:

listAction =['walks','runs','jaunts','ambles','dashes','sprints']

Upvotes: 1

Views: 1122

Answers (2)

Brendan Long
Brendan Long

Reputation: 54242

This is actually more complicated than I initially thought, since you have a function named self.action and a variable named self.action, so your code is confusing. The first time you call DefineAction.action(), you overwrite the function with a string, so the second time you call it, you get this error.

Just give your function and variable different names and it should work fine.

Upvotes: 6

Andrew Clark
Andrew Clark

Reputation: 208475

In the function action, you set self.action to a string. This means that when you call Action.action() the first time it sets the attribute Action.action to a string, so the next time you call Action.action() you will see this error.

Just pick a different name for your attribute, make sure that it doesn't match the name of the method. You could use DefineAction.action(Action) to still call the original action() method, but this is bad practice.

Upvotes: 2

Related Questions