Can ask a class for its functions?

Bengt Richter bokr at oz.net
Thu Aug 15 04:13:17 CEST 2002


On Wed, 14 Aug 2002 16:17:22 -0700, Sung Kim <skim at adobe.com> wrote:

>Assuming we import a module (which contains a class). Can we determine 
>its member functions programmatically? Thank you.
>
>--Sung H. Kim
>   skim at adobe.com

Yes, but note that there are varieties of functions as well as
other class attributes possible. E.g., an incomplete sample (also ignoring
presence or absence of 'self' parameters which will/won't be present/required
depending on how accessed/used, and which I'm too lazy to go back and fix ;-/ ):

 >>> class C:
 ...     K = 123
 ...     def f(x,y): return (x+y)**2
 ...     g= lambda x: (x+1)**3
 ...     def cm(): pass
 ...     cm=classmethod(cm)
 ...     def sm():pass
 ...     sm=staticmethod(sm)
 ...
 >>> for k,v in C.__dict__.items(): print '%15s: %-25s %s'% (k, type(v), v)
 ...
               g: <type 'function'>         <function <lambda> at 0x007D1A70>
      __module__: <type 'str'>              __main__
              cm: <type 'classmethod'>      <classmethod object at 0x007D1ED0>
              sm: <type 'staticmethod'>     <staticmethod object at 0x007D1E70>
               f: <type 'function'>         <function f at 0x007D1AB0>
               K: <type 'int'>              123
         __doc__: <type 'NoneType'>         None

and how you access the class attribute namespace also matters
-- i.e., C.__dict__[k] vs getattr(C, k)
(I don't know what all the distinct names signify ;-) :

 >>> for k in C.__dict__.keys(): print '%15s: %-25s %s'% (k, type(getattr(C,k)),getattr(C,k))
 ...
               g: <type 'instance method'>  <unbound method C.<lambda>>
      __module__: <type 'str'>              __main__
              cm: <type 'instance method'>  <bound method class.cm of <class __main__.C at 0x007D0A00>>
              sm: <type 'function'>         <function sm at 0x007D1EA0>
               f: <type 'instance method'>  <unbound method C.f>
               K: <type 'int'>              123
         __doc__: <type 'NoneType'>         None


And same for a new style class (deriving from object):

 >>> class C(object):
 ...     K = 123
 ...     def f(x,y): return (x+y)**2
 ...     g= lambda x: (x+1)**3
 ...     def cm(): pass
 ...     cm=classmethod(cm)
 ...     def sm():pass
 ...     sm=staticmethod(sm)
 ...
 >>> for k,v in C.__dict__.items(): print '%15s: %-25s %s'% (k, type(v), v)
 ...
      __module__: <type 'str'>              __main__
              cm: <type 'classmethod'>      <classmethod object at 0x007D2AE0>
               g: <type 'function'>         <function <lambda> at 0x007D2B10>
               f: <type 'function'>         <function f at 0x007D3120>
        __dict__: <type 'getset_descriptor'> <attribute '__dict__' of 'C' objects>
              sm: <type 'staticmethod'>     <staticmethod object at 0x007D30F0>
               K: <type 'int'>              123
     __weakref__: <type 'member_descriptor'> <member '__weakref__' of 'C' objects>
 >>>
 >>> for k in C.__dict__.keys(): print '%15s: %-25s %s'% (k, type(getattr(C,k)),getattr(C,k))
 ...
      __module__: <type 'str'>              __main__
              cm: <type 'instance method'>  <bound method type.cm of <class '__main__.C'>>
               g: <type 'instance method'>  <unbound method C.<lambda>>
               f: <type 'instance method'>  <unbound method C.f>
        __dict__: <type 'dict-proxy'>       {'__module__': '__main__', 'cm': <classmethod o
 bject at 0x007D2AE0>, 'g': <function <lambda> at 0x007D2B10>, 'f': <function f at 0x007D31
 20>, '__dict__': <attribute '__dict__' of 'C' objects>, 'sm': <staticmethod object at 0x00
 7D30F0>, 'K': 123, '__weakref__': <member '__weakref__' of 'C' objects>}
              sm: <type 'function'>         <function sm at 0x007D5650>
               K: <type 'int'>              123
     __weakref__: <type 'member_descriptor'> <member '__weakref__' of 'C' objects>

Regards,
Bengt Richter



More information about the Python-list mailing list