简体   繁体   English

Python样式,命名常量参数

[英]Python style, naming constant arguments

I'm thinking of spelling those Python function arguments that are not modified in the function body as ALL_UPPERCASE, signaling to users of such an API that the passed value won't be modified (if everything goes as advertised, at any rate). 我正在考虑将未在函数体中修改的那些Python函数参数拼写为ALL_UPPERCASE,向这些API的用户发出信号,告知不会修改传递的值(如果所有内容都按照广告宣传,无论如何)。 I'm unaware of Python conventions or language mechanisms that this would collide with; 我不知道这会碰到的Python惯例或语言机制; are there any? 有没有? Would such a naming convention be advisable, or just unnecessarily odd? 这样的命名惯例是可取的,还是只是不必要的奇怪?

Eg: 例如:

def foo(bar, BAZ):
    return bar.some_method(BAZ.some_property)

UPPERCASE is for global constants, not function arguments. UPPERCASE用于全局常量,而不是函数参数。 See the Python Style Guide here. 请参阅此处的Python样式指南

In Python function signatures, you'd expect all arguments to be unmodified. 在Python函数签名中,您希望所有参数都不被修改。 If an argument expects a mutable object and it'll be changed in-place, then that is the exception and should be documented as such: 如果一个参数需要一个可变对象并且它将被就地更改,那么这是一个例外,应该记录如下:

def foo(bar, baz):
    """Frobs the bar in baz

    Alters the baz list in-place, and returns None.

    """

Using UPPERCASE for function arguments that are not being modified would thus be a) confusing (as it conflicts with the style guide) and b) redundant, as most code won't alter mutable objects in place unless documented as doing so explicitly. 因此,对未被修改的函数参数使用UPPERCASE将是a)混淆(因为它与样式指南冲突)和b)冗余,因为大多数代码不会改变可变对象,除非记录为明确这样做。

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

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