Simplifying anonymous inner classes?

Tim Chase python.list at tim.thechases.com
Sat Nov 1 09:16:07 EDT 2008


>> PS: part of the aim is to have properties that can be discovered 
>> through introspection, know how to provide help on themselves, 
>> and have a protocol for parameter-discovery for the __call__ method.
> 
> Without a more concrete example it is hard to tell what you are trying to 
> achieve. Python already allows you to discover methods and properties 
> through instrospection, a standard method for them to provide help on 
> themselves and standard ways to inspect parameters. Are you adding some 
> value or just inventing a wheel with more sides?

It's an application with multiple back-ends (web, DB, textfile) 
and multiple front-end UIs (command-line, web, IM).  Yes, 
Python's introspection could determine the parameters to a 
function, but providing help on them is a bit unwieldy (and 
processing the motley assortment of parameters from a list of 
args becomes more complex).  Each Action contains an optparse 
parser that knows how to deal with that particular action's list 
of parameters, as well as provide documentation on that 
parameter, allow for defaults, parse more complex action 
syntaxes, etc.  All front-end requests are transformed into this 
syntax as an array of arguments to be passed to the Action.

As a common ground with which you're hopefully familiar, consider 
the interface to your typical VCS such as cvs/svn/hg/bzr/git:

   vcs [global options] <command> [command options] [args]

Each of those <commmand>s maps to an Action where the [command 
options] can be discovered based on the implementation of 
<command>, the help for that command can be readily generated, 
and all the other power of optparse (defaults values, custom 
parsing, option grouping, parameter synonyms, etc) are available. 
  The IM interface takes <command> onward accepted as messages, 
and the CGI web interface takes GET/POST requests and translates 
them into the corresponding command/options/args.  Thus, the 
actual call signatures are more accurrately

   def __init__(self, ...):
     self.parser = optparse.OptionParser(...)
   def __call__(self, argv): # argv is a list like sys.argv
     options, args = self.parser.parse_args(argv)
     # do something with options/args

So that background provided, the original question was more about 
how to create anonymous sub-classes with overridden methods so my 
Backend objects' code & namespaces weren't littered with objects 
that were only defined to be instantiated once in exactly the 
location they were defined.

-tim







More information about the Python-list mailing list