encapsulating a global variable

BlindAnagram blindanagram at nowhere.com
Tue Feb 25 07:38:36 EST 2020


I would appreciate advice on whether it is possible to avoid the use of
a global variable used in a function by encapsulating it in a class
without maaking any changes to the call interface (which I cannot change).

I have:

----------------
seen = dict()

def get_it(piece):
   ...
   return seen[piece]
----------------

and I am wondering if it is possible to use a class something like

----------------
class get_it(object):

  seen = dict()

  def __call__(piece):
    return seen[piece]
----------------

to avoid the global variable.

  Brian Gladman


More information about the Python-list mailing list