[Tutor] Silly Object Namespace Questions

Kalle Svensson kalle@gnupung.net
Fri, 25 May 2001 01:29:40 +0200


Sez D-Man:
> On Thu, May 24, 2001 at 09:41:35AM -0700, Sean 'Shaleh' Perry wrote:
> | On 24-May-2001 Curtis Larsen wrote:
> | > 6. What function(s) can I use to see/list methods such as "doit" within
> | > an instance?  ("vars()" and "dir()" don't do it.)
> 
> dir( object1 )
> 
> or
> 
> instance = object1()
> dir( instance.__class__ )
> 
> 
> Notice that instead of passing the instance to the dir funtion I
> passed the class object.

Even this won't be enough if the class has inherited methods from
superclasses, though.  For that situation, you'll need something like this
(almost untested):

def classdir(c):
    res = dir(c)
    try:
        for s in c.__bases__:
	    res += classdir(s)
    except AttributeError:
        pass
    return res

It is far from perfect, but should illustrate the principle...

Finally, this would do what you (might) want (almost untested too):

import types
def superdir(obj):
    if isinstance(obj, types.InstanceType):
        return dir(obj) + classdir(obj.__class__)
    elif isinstance(obj, types.ClassType):
        return classdir(obj)
    else:
        return dir(obj)

Peace,
  Kalle
-- 
Email: kalle@gnupung.net     | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
 [ Not signed due to lossage.  Blame Microsoft Outlook Express. ]