gpt4 book ai didi

python - 接受与关键字/标准库冲突的变量名约定

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:27 24 4
gpt4 key购买 nike

我有一个函数,我将 datetime.datetime 对象作为参数。假设我还想要一个需要命名为 break 或类似名称的参数。我显然不能在不抛出 SyntaxError 的情况下将 break 用作 arg。

为了避免与标准 Python 库名称或关键字发生冲突,最广为接受的重命名约定是什么?

最佳答案

根据 the style guide :

  • single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

    Tkinter.Toplevel(master, class_='ClassName')

具体来说,对于参数:

If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)

和属性名称:

  • If your public attribute name collides with a reserved keyword, append a single trailing underscore to your attribute name. This is preferable to an abbreviation or corrupted spelling. (However, notwithstanding this rule, cls is the preferred spelling for any variable or argument which is known to be a class, especially the first argument to a class method.)

关于python - 接受与关键字/标准库冲突的变量名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33284195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com