0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
curiosityrock
  • 213
  • 4
  • 20
  • Can you please fix your code? As written, it cannot run. Please make this a reproducible example that users can copy and paste and run without having to change anything. – jkr Dec 10 '21 at 16:39
  • 1
    `team_map` is defined as a global variable. Change the content of the dictionary at one place and you will see this change everywhere. If you define `team_map` in `get_query_template` this won't happen. – Matthias Dec 10 '21 at 16:39
  • 6
    `team_map` is not a global variable, it's a *class attribute*. If you want the data not to be shared between all instances of the class, you have to make it an *instance attribute*. You can do this, by setting it not in the class body but setting `self.test_map` in the `__init__` method of the class. – MaxNoe Dec 10 '21 at 16:41
  • I don't see how the code you shared is producing the output you're showing. If `user_permissions` is `True` the first time you call it, and yet `team_map` has `"GSW": 'San Francisco',` rather than `'New San Francisco'`. I don't see how that's possible unless another instance of the class is overwriting that item before you print the results. And yeah, like was said, a class variable like that is shared across all instances. Only a `self.` variable is specific to an instance. To me it's not really clear what you're expecting to happen versus what is happening. – Random Davis Dec 10 '21 at 16:46
  • @Matthias's answer worked since i declare it as a local variable – curiosityrock Dec 10 '21 at 16:46
  • However, I also tried @MaxNoe's answer and initialized as an instance attribute. That still gives the same result and share the results between the class. – curiosityrock Dec 10 '21 at 16:47
  • What do you mean with "between the class"? Different instances of the same class? That's should not be the case. Or calling the same method twice from the same instance? If yes, this will indeed use the same map again. If the team map should start fresh every time you call `get_query_template`, just define it inside the function not on the class or as attribute in `__init__`. – MaxNoe Dec 10 '21 at 16:49
  • Yes exactly, sorry if it wasn't clear enough. I want it to start fresh every time i call query_template and check the logic. Declaring it as a local variable in get_query_template helped with that. @MaxNoe – curiosityrock Dec 10 '21 at 16:50
  • check also https://stackoverflow.com/q/207000/4046632 – buran Dec 10 '21 at 17:03

0 Answers0