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.

Comments

James Gray

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

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.

Previous:
Searching for the obvious March 29, 2005 Wondering
Next:
callable Python objects April 2, 2005 Python
Related by category:
'Cache-Control' or Expires September 16, 2005 Zope
To all Zope developers: Does this sound familiar? March 8, 2011 Zope
IssueTrackerProduct now officially abandoned March 30, 2012 Zope
Sending HTML emails in Zope October 26, 2006 Zope
Related by keyword:
Display current React version January 7, 2018 React, JavaScript
Introducing: HUGEpic - a web app for showing massive pictures November 3, 2012 Python
More productive than Lisp? Really??! March 10, 2011 Python
premailer now excludes pseudo selectors by default May 27, 2013 Python, Web development