How can I define __getattr__ to operate on all items of container and pass arguments?

Jeremy jlconlin at gmail.com
Thu Feb 24 16:33:49 EST 2011


On Tuesday, February 15, 2011 2:58:11 PM UTC-7, Jeremy wrote:
> 
> > So the arguments haven't yet been passed when __getattr__() is
> > invoked. Instead, you must return a function from __getattr__(); this
> > function will then get called with the arguments. Thus (untested):
> > 
> > def __getattr__(self, name):
> >     def _multiplexed(*args, **kwargs):
> >         return [getattr(item, name)(*args, **kwargs) for item in self.items]
> >     return _multiplexed
> 

Sorry to resurrect an old(ish) thread, but I have found a problem with this approach.  Defining __getattr__ in this manner allows me to access methods of the contained objects---this was the original goal.  But it has introduced a problem that I can no longer access the documentation for my class.  The error I get is copied below.  It seems like the help function is trying to access an attribute of the class, but can no longer get it.  Any suggestions:

Thanks,
Jeremy

/home/jlconlin/CustomPython/trunk/Collect.py in <module>()
----> 1 
      2 
      3 
      4 
      5 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc in __call__(self, *args, **kwds)
    455     def __call__(self, *args, **kwds):
    456         import pydoc
--> 457         return pydoc.help(*args, **kwds)
    458 
    459 def sethelper():

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc in __call__(self, request)
   1721     def __call__(self, request=None):
   1722         if request is not None:
-> 1723             self.help(request)
   1724         else:
   1725             self.intro()

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc in help(self, request)
   1768             elif request: doc(request, 'Help on %s:')
   1769         elif isinstance(request, Helper): self()
-> 1770         else: doc(request, 'Help on %s:')
   1771         self.output.write('\n')
   1772 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc in doc(thing, title, forceload)
   1506     """Display text documentation, given an object or a path to an object."""
   1507     try:
-> 1508         pager(render_doc(thing, title, forceload))
   1509     except (ImportError, ErrorDuringImport), value:
   1510         print value

l/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc in render_doc(thing, title, forceload)
   1483     desc = describe(object)
   1484     module = inspect.getmodule(object)
-> 1485     if name and '.' in name:
   1486         desc += ' in ' + name[:name.rfind('.')]
   1487     elif module and module is not object:

TypeError: argument of type 'function' is not iterable



More information about the Python-list mailing list