Exposing all methods of a class

Tim Chase python.list at tim.thechases.com
Wed Mar 1 06:54:52 EST 2017


On 2017-03-01 05:50, Terry Reedy wrote:
> The class listing provided by the pydoc module browser, also in 
> help(someclass), do list all methods.  Try
>  >>> import tkinter
>  >>> help(tkinter.Text)
> for instance.

I've been stung by opaque objects a couple times:

1) when the method comes from calculation via __getattr__ type methods

  class Foo(object):
   def __getattr__(self, attr):
     if attr.startswith("say_"):
       utterance = attr[4:]
       def method():
         return "Hello, %s" % utterance
       return method

  f = Foo()
  f.say_Bob()
  dir(f)  # no "say_Bob" method

2) when the underlying object is written in C and doesn't expose the
dir() properly (I'm looking at mod_python's objects in particular
which didn't allow inspection the last time I used it; though in
fairness, this was 7-9yrs ago)

-tkc






More information about the Python-list mailing list