Display dynamically added methods and attributes in python help

I have a class in which I add new methods and properties dynamically. New properties are handled by overriding __getattr__ and __setattr__, while new methods are added directly (obj.mymethod = foo). Is there a way to do this if I do "help (inst)" where inst is an instance of my class? Now I see only methods and attributes that I "hard-coded" in the source. Methods really appear if I do "dir (inst)".

+3
source share
1 answer

The problem is that help (inst) provides information about the class from which this instance of "inst" is derived.

say obj A, obj.mymethod = foo, A.mymethod = foo, help (obj)

.

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)

, - , . , help(), , help().

+2

Source: https://habr.com/ru/post/1766566/


All Articles