I have strange issue that I'm having hard time to understand possibly based on how global or class variables work in python. I've a falcon class where i call rest api with a class design. I modify team_map
based on another API call that I'm making. (I cleaned up the code, so it would be easier to understand). However, it seems like it's always using the updated values no matter what user_permission
is. If I shut down my gunicorn server and start again, then it works fine for the first time but the second requests are always same or cached.
class Reach(BaseClass):
team_map = {
"Lakers": 'Los Angeles',
"Celtics": 'Boston',
"GSW": 'San Francisco',
"Clippers": 'Los Angeles',
}
def __init__(self, name):
super().__init__(name)
self.disabled = False
def get_query_template(self, req_data):
user_permission= self.get_user_permission(req_data) //sample API request, this returns true or false depending on different scenarios
# new xandr reach freq mviews
if user_permission:
self.team_map["Lakers"] = 'New Los Angeles'
self.team_map["Celtics"] = 'New Boston'
self.team_map["GSW"] = 'New San Francisco'
print("team map",team_map)
return team_map
First call: user_permission
is True
.
It returns:
"Lakers": 'New Los Angeles',
"Celtics": 'New Boston',
"GSW": 'San Francisco',
"Clippers": 'Los Angeles',
Second call: user_permissions
is False
and it still returns
"Lakers": 'New Los Angeles',
"Celtics": 'New Boston',
"GSW": 'San Francisco',
"Clippers": 'Los Angeles',
whereas I'm expecting it to return the first state. I don't know why this is happening, I tried using a global
keyword and move my team_map
outside of the class, but that didn't work.