What do you call a class not intended to be instantiated

James Mills prologic at shortcircuit.net.au
Mon Sep 22 02:12:38 CEST 2008


On Mon, Sep 22, 2008 at 9:39 AM, Calvin Spealman <ironfroggy at gmail.com> wrote:
> I call it an obvious misuse and misunderstanding of why you'd use a class in
> the first place. Either create an instance and not make these things
> classmethods or just share the stuff in a module-level set of variables. But
> the instantiating is the best options. Your class attributes might not be
> globals, but you're still using global state and you should avoid it where
> you can.

I concur. Use a _proper_  state object that you share
amongst your other objects. For instance, in many of
my systems and applications I write, I often have
an "Environment" instance, which is a container
object that holds other objects required by parts
of the system. Every other component/object in the
system that is instantiated recievees exactly one
instnace of thie "Environment" called, "env".

Accessing shared states amongst components/objects
within the system is as simple as this:

class Foo(object):

   def __init__(self, env, *args, **kwargs):
      self.env = env

   def foo(self):
      if self.env.some_state:
         print "Do something useful"

env = Environment()
foo = Foo(env)
foo.foo()

cheers
James

-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list