Injecting a global into a defined function??

James Mills prologic at
Fri Jan 16 05:13:05 CET 2009

On Fri, Jan 16, 2009 at 1:59 PM, Cong Ma <cma at> wrote:
> I've thought of this too, but it turns out those functions are related to each
> other very loosely. They do a lot of completely different things independently
> and have just one common global variable to share. IMHO it would reduce the
> readability of code because the role of this class is very unclear. Anyway,
> "readability" is mostly a subjective matter and I think what you pointed out is
> a good idea in general.

So then the other commonly used pattern
is a shared object between two other objects.

class Env(object):

   def __init__(self):
      self.spam = None

class A(object):

   def __init__(self, env):
      self.env = env

   def __call__(self):
      self.env.spam = "Eggs"

class B(object):

   def __init__(self, env):
      self.env = env

   def __call__(self):
      self.env.spam = "Bacon"

Very readable and makes perfect sense if it suits you.


More information about the Python-list mailing list