开发者

Difference between pydoc and help()?

开发者 https://www.devze.com 2023-03-17 23:42 出处:网络
Are these two things di开发者_高级运维fferent? The results that the two give in Python are similar.help() is a Python function.

Are these two things di开发者_高级运维fferent? The results that the two give in Python are similar.


help() is a Python function.

pydoc is a command-line interface to the same thing.

If you want to see more what pydoc does, take a look in pydoc.py (import pydoc; pydoc.__file__) and see what's in the cli function. It does do some extra importing magic, but I don't think it really needs to - help() accepts a string which is evaluated in the same sort of way, so if you have "foo.py", run python and do help('foo') it'll get just about the same result as import foo; help(foo) would, just with minor differences in layout, I think. Probably historical reasons there.

In short, pydoc foo is about equal to python -c "help('foo')"


The interactive help function just imports pydoc. From the source:

class _Helper(object):                                 
    """Define the builtin 'help'.                      
    This is a wrapper around pydoc.help (with a twist).

    """                                                

    def __repr__(self):                                
        return "Type help() for interactive help, " \  
               "or help(object) for help about object."
    def __call__(self, *args, **kwds):                 
        import pydoc                                   
        return pydoc.help(*args, **kwds)  

Note the __call__ definition.

You can see the documentation with help(help) ;-)

You can use the inspect module to check out the source yourself next time you're curious.

In case you want to investigate on your own, the following will return the source code definition of the module defined for an object:

inspect.getsource(inspect.getmodule(help))


In simple words I think pydocs can be explained as Tesla, Inc. and help is just a product of tesla like Tesla Model S

If one goes inside the help function code it is clearly mentioned that help is using pydocs.help

"""
Define the built-in 'help'.
    This is a wrapper around pydoc.help (with a twist).
"""

pydocs has got many more options and features as compared to help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号