If you're a Zope developer like myself you will most likely have written code like this many times:

class MyProduct(...):
   def showName(self, nameobject):
       """ doc string """
       print dir(nameobject) # this is the example
       return nameobject.getName()

Namely that you use the dir() builtin function to inspect an object's attributes. It's a wonderful function that from what I've experienced only Python excels. (Apparently Bruce Eckels lists introspection as one of the best features of Python and he's a guru with Java and C++)

The problem with doing this sometimes in Zope is that the list of attributes is HUGE. Often it contains so many inherited attributes that it because nearly impossible to spot the ones your looking for. Zope objects also have loads of permission attachment attributes like showName__roles__ which are useful but not what you're trying to debug. How about this one-liner to get much nicer output in the console:

map(pprint, [x for x in dir(o) if not x.endswith('__roles__')])

It can be rewritten to make more sense but if it's clarity you're looking for you might as well install DocFinderTab or rewrite it like this:

def ppdir(object):
    for e in dir(object):
        if e.endswith('__roles__'):
            continue
        print e

Anyway. I'm ranting. It's bad style to use map() and this one-liner might scare beginners so I shall shut my hole now and get to work. If you are a beginner, consider this a chance to learn list-comprehension.

James Gray - 03 April 2005 [«« Reply to this]
You don't need the map in order to get what you want. You could do something like this:

[pprint(x) for x in dir(o) if not x.endswith('__roles__')]

The fun thing about list-comprehensions is that you can 'filter' and 'map' all in one shot.
Peter Bengtsson - 03 April 2005 [«« Reply to this]
Of course! Why didn't I think of that. It's just a bit strange to create something that is never assigned to something else. Thanks for the tip


Your email will never ever be published