'Python assigning the string to the variable - Str object is not callable message
I have the following problem where
roles = models.StringField
def role(player):
if player.rank == 1:
return 'leader'
else:
return 'member'
Later I am trying to assign the role function outcome which is string to roles variable i.e.
for i in range(len(players)):
players[i].roles = players[i].role()
.....
However, I am getting the message that str function is not callable. I am new to python programming. Any help will be much appreciated. The function is working properly otherwise. I am using python 3.9.
I have used the setattr function by trying
for i in range(len(players)):
name = 'roles'
setattr(players[i], name, players[i].role())
.....
However, still I am getting the same message that str function is not callable. Any idea how can I fix this.
Solution 1:[1]
this error msg is a runtime error.
it means that players[i].role is a string not a function.
that means that somewhere is your app, you assigned a string value to players[i].role. so it is no longer a function.
this may be a typo (ie: you probably wroteplayers[i].role = "blabla bla"
instead ofplayers[i].roles = "blabla bla")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ibrahem |
