beginner ques: dir( ) does'nt list my instance methods?
Jesse W
jessw at loop.com
Fri Jun 22 14:55:20 EDT 2001
Dear karthik,
I have written a short function that should show all the references in
a given object. I think it has a few bugs, but I don't remember what
they are.
Here it is:
def super_dir(object, top=1):
"""Return _all_ variables, functions, and methods.
This is a fix for the dir function that is provided with Python.
It will return all class methods, even if they are defined in a base
class. It also works with class objects, showing all the methods, not
just those defined in the given object."""
answer=dir(object)
if hasattr(object, '__class__'):
answer=answer+super_dir(object.__class__, 0)
elif hasattr(object, '__bases__'):
for item in object.__bases__:
answer=answer+super_dir(item, 0)
if top:
new_answer=[]
for item in answer:
if item not in new_answer:
new_answer.append(item)
return new_answer
return answer
For those who missed the original message:
karthik_guru at rediffmail.com wrote:
>
> class test:
> def __init__(self):
> print 'hello test'
> self.foo = 100
> self.bar = 900
>
> def func(self):
> print 'hello'
>
> if __name__ == '__main__':
> t = test()
> print dir(t)
> print t.__dict__
>
> Both dir(t) and t.__dict__ print only foo and bar.
> They don't print the func which is also a instance attribute (method
> reference)?
>
> But they are printed when i do test.__dict__ and dir(test)..ie
> <classname>
> why is it not getting printed in the earlier case?(wiht instance)
>
> I might be required to do a look up from dir(instance) and if an
> attribute happens to be of the type method i can invoke it?
>
> thanks
> karthik.
More information about the Python-list
mailing list