Finding the public callables of self

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu Feb 9 11:51:39 EST 2006


Russell Warren <russandheather at gmail.com> wrote:
>Is there any better way to get a list of the public callables of self
>other than this?
>
>    myCallables = []
>    classDir = dir(self)
>    for s in classDir:
>      attr = self.__getattribute__(s)
>      if callable(attr) and (not s.startswith("_")):
>        myCallables.append(s) #collect the names (not funcs)
>
>I don't mean a shorter list comprehension or something that just drops
>the line count, but whether or not I need to go at it through dir and
>__getattribute__.  This seems a bit convoluted and with python it often
>seems there's something already canned to do stuff like this when I do
>it.  At first I thought self.__dict__ would do it, but callable methods
>seem to be excluded so I had to resort to dir, and deal with the
>strings it gives me.

This last sentence suggests to me something like:

    attrs = set(s for s in dir(self) if not s.startswith('_'))
    myCallables = attrs.difference(a.__dict__)
    return list(myCallables)

(which you can get down to one line if you want).

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list