简体   繁体   English

如何覆盖 python 中 function 的原始签名

[英]How to override original signature of a function in python

Let's say I have the following function:假设我有以下 function:

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

If I execute help(person) I would get如果我执行help(person)我会得到

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:您可以简单地覆盖.__doc__属性:

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. doc方法,它打印出文档字符串。 It works like this:它是这样工作的:

    print.__doc__

Or for better readability:或者为了更好的可读性:

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

Pprint is pretty print. Pprint 是漂亮的打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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