Search code examples
pythonmemory-managementstylesselfconvention

When to NOT use the self convention in Python?


I've just recently wrapped my head around the self convention in Python and have begun making more complex code. However, an experienced programmer and friend of mine told me that to use self for every variable in a class method is wasteful.

I understand that self will cause the variable to become attributed to that class. So would it be true that, unless the need arises, it is good practice to avoid using self?

Below is some code that fetches League of Legends information from an API and stores each variable in self.var_name to illustrate how I'm (perhaps unnecessarily) using self.

async def getChampInfo(self, *args):
    """ Return play, ban, and win rate for a champ """
    self.uri = "http://api.champion.gg/v2/champions/{}?api_key={}"
    self.champ = " ".join(args)
    self.champID = lu.getChampID(self.champ)
    self.res = requests.get(self.uri.format(
        self.champID, League.champion_gg_api_key)).json()
    self.role = self.res[0]["role"]
    self.role_rate = self.res[0]["percentRolePlayed"]
    self.play_rate = self.res[0]["playRate"]
    self.win_rate = self.res[0]["winRate"]
    self.ban_rate = self.res[0]["banRate"]

Solution

  • There are cases where using self is not needed.

    Off the top of my head:

    • when the variable is only used in 1 function, or is created inside a function/method and only used in that function/method
    • when the variable doesn't need to be shared between methods
    • when the variable doesn't need to be exposed to other classes/scopes/contexts

    Another partial answer is that when creating metaclass/factories/composition something like this might make more sense to move away from the convention of using self like:

    class Factory(object):
        def __init__(cls, *args, **kwargs):
            thing = cls(args, kwargs)
    

    I might be missing some stuff here, but those are what i can think of at the moment.

    related: