Python assigning the string to the variable - Str object is not callable message

68 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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 wrote
players[i].role = "blabla bla"
instead of
players[i].roles = "blabla bla")