I have a class where I add new methods and properties dynamically. The new properties are handled by overriding __getattr__ and __setattr__ while the new methods are added directly (obj.mymethod = foo). Is there a way to make these show up if I do "help(inst)" where inst is an instance of my class? Right now I only see the methods and attributes I have "hardcoded" in the source. The methods do show up if I do "dir(inst)".
+1
A:
The issue is that help(inst) provides the information about class from which that instance "inst" is derived from.
say obj is derived from class A, then instead of doing obj.mymethod = foo, if you did A.mymethod = foo, then this will show up in help(obj)
Look at the example below and it's output.
class A(object):
def __init__(self):
pass
def method1(self):
"This is method1 of class A"
pass
a = A()
help(a)
def method2(self):
""" Method 2 still not associated"""
pass
A.method2 = method2
# if you did a.method2 = method2
# Then it won't show up in the help() statement below
help(a)
As per the documentation, if the argument is any other kind of object, a help page on the object is generated. But from the example above, I see that adding the method in the namespace of class is shown up in help() function but if you added the method to just one instance of that class, then it does not show up in the help().
pyfunc
2010-09-25 20:07:00
Thanks, works perfectly!
pafcu
2010-09-26 08:36:36