简体   繁体   中英

How to override original signature of a function in python

Let's say I have the following function:

def person(*args, **kwargs):
    '''
       person(name, age, parents=2)
       this is a docstring.
    '''

If I execute help(person) I would get

person(*args, **kwargs)
    person(name, age, children=5)
    this is a docstring.

But instead, I want to see:

person(name, age, children=5)
    this is a docstring.

You can simply overwrite .__doc__ attribute:

def a():
    """orig docstring"""
    pass
help(a)
a.__doc__ = """modified docstring"""
help(a)

You could always use docstrings, like this:

def function(...):
    """
    This function does this and that. The parameters are the following:
    param1 = ...
    param2 = ...
    ...
    """
    # code

Then you can simply call the. doc method and it prints out the docstring. It works like this:

    print.__doc__

Or for better readability:

    import pprint
    pprint.pprint(pprint.__doc__, indent=4)

Pprint is pretty print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM