简体   繁体   English

Python函数/方法docstring变量

[英]Python function/method docstring variables

Let's say I what to write a docstring for the code bellow: 假设我要为以下代码编写文档字符串:

def some_thing_cool(age, name):
    better_age = age - 20
    cooler_name = name + 'awesome'
    bday_list = ['cake', 'balloons']
    return bday_list

Would it be like this: 会这样吗:

def some_thing_cool(age, name):
    """Something for bday

    Args:
         age (int) : age of bday person
         name (string) : name of bday person
    Variable:
             better_age (int) : age - 20 for more pleasing age
             cooler_name (string) : name + awesome
             bday_list (list) : things to remember for bday
    Returns:
            bday_list (list) : best to return the list
    """
    better_age = age - 20
    cooler_name = name + 'awesome'
    bday_list = ['cake', 'balloons']
    return bday_list

Or should it be like this: 还是应该像这样:

def some_thing_cool(age, name):
    """Something for bday

    Args:
         age (int) : age of bday person
         name (string) : name of bday person
    Returns:
            bday_list (list) : best to return the list
    """
    better_age = age - 20
    cooler_name = name + 'awesome'
    bday_list = ['cake', 'balloons']
    return bday_list

And most importantly why should it be one way or another? 最重要的是,为什么要采用一种或另一种方式? (Do not think about the docstring style, this is not of importance in this question.) Most example I could find online does not include any variables when displaying how to write good docstrings, and this is constantly on my mind. (不要考虑文档字符串样式,这在这个问题中并不重要。)在显示如何编写好的文档字符串时,我在网上发现的大多数示例都不包含任何变量,这一直在我的脑海中。

I have flagged this is a primarily opinion based, but in general, docstrings should only show the inputs and outputs of functions, and include a human readable description of what the function does . 我检举了这是一个基于主要的意见,但在一般情况下,文档字符串应仅显示的功能的输入和输出,包括的功能什么可读描述。 Here's an example from the Python docstring documentation : 这是Python文档字符串文档中的示例:

def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)
    """
    if imag == 0.0 and real == 0.0:
        return complex_zero

Put yourself in the perspective of a reader who has never seen the code. 将自己置于从未看过代码的读者的角度。 If I am just looking to use your function, do I really care about every variable that's used within it? 如果我只是想使用您的函数,那么我真的在乎其中使用的每个变量吗? I really should only care about what it takes as input and what it provides as output. 我真的应该只关心它作为输入的内容和它作为输出提供的内容。

Finally, please note that on Stack Overflow, questions should not be primarily opinion based as they tend to attract a lot of answers that aren't provably right or wrong. 最后,请注意,在Stack Overflow上,问题不应该主要基于观点,因为它们往往会吸引很多无法证明是非的答案。 Check out How To Ask . 查看如何提问

Those variables are not available outside of the function: 这些变量在函数外部不可用:

def some_thing_cool(age, name):
    better_age = age - 20 #this is a better age
    cooler_name = name + 'awesome' #This name is better (spaces not included)
    bday_list = ['cake', 'balloons']
    return bday_list

returnedValue = some_thing_cool(31.4159, 'Will')
#sets returned value equal to ['cake', 'balloons']

print(better_age) #NameError: name 'better_age' is not defined

Doc-strings are used to describe the function to people who want to use it. 文档字符串用于向想要使用该功能的人员描述该功能。 Since the variables are not available outside the function, no users of the function really care about them, and they just clutter up the docstring. 由于这些变量在函数外部不可用,因此该函数的用户都不在乎它们,它们只会使文档字符串变得混乱。 You could add comments in the code to describe what the variables do so that anyone reading your code can understand it. 您可以在代码中添加注释以描述变量的作用,以便任何阅读您的代码的人都可以理解它。

The reason that that the variables are unavailable is due to namespaces , you might want to look into them. 变量不可用的原因是由于命名空间 ,您可能需要调查一下它们。

Well there is PEP-257 , but I personally don't think it's verbose enough 嗯,有PEP-257 ,但我个人认为它不够详细

From the PEP: 从PEP:

def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)
    """
    if imag == 0.0 and real == 0.0:
        return complex_zero
    ...

I personally use the following convention in my code 我个人在代码中使用以下约定

def get_versions(self, db_id):
    """ Simple call to look up all versions in a jira project
    :param db_id:   int: The ID of the Jira project
    :return:        list: of version objects
    :raises:        JIRAError on failure
    """
    ...

This is just pretty much what PyCharm does for you automatically though... 尽管这只是PyCharm自动为您做的...

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

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