Is there a more Pythonic way to instantiate sub-classes and
provide instance-specific implementations without the
overhead of an unused "anonymous" class cluttering my
code/namespace?

I agree with Carl Banks that what you do is already fairly
Pythonic: explicit is better than implicit, and simple is
better than complex. What you do *is* simple, and easy to read
- no magic involved. You wish it to be more concise - but I
think that would make *less* pythonic, not more.

After playing with the various solutions (hurray for version-control), I find myself back where I started, agreeing with Carl & Martin for the most part. Everything else ended up looking so baroque that it made matters worse. Even the extra "del" statements for cleaning up the namespace were extra clutter in the code, so I didn't bother tidying the namespace. I did switch from

  do_foo = _Foo(args)
  do_bar = _Bar(args)

to having each backend contain a list of available actions, so I simply maintain

  self.actions = set([
    _Foo(args),
    _Bar(args),
    _Baz(args),
    ])

(each Action knows how to hash itself for entry in a set) and then descendants of Backend (such as DBBackend) simply add to this set of actions in their own __init__ call.

The final product is what I'd call a "most understandable/least convoluted" solution which, in the big picture, seems to be a good part of "pythonicity". Until such time as Python offers anonymous classes natively :)

Thanks to all who offered suggestions.

-tkc




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to