Python中变量名后的下划线 [英] Underscore after a variable name in Python

查看:571
本文介绍了Python中变量名后的下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解密别人的代码,并且看到以下内容:

I am deciphering someone else's code and I see the following:

def get_set_string(set_):
     if PY3:
         return str(set_)
     else:
         return str(set_)

变量后的下划线是什么意思,还是仅仅是变量名称的一部分,什么都没有?

Does the underscore AFTER the variable mean anything or is this just a part of the variable's name and means nothing?

推荐答案

没有语义与结尾的下划线相关联.根据 PEP 8 的指导, Python,强烈建议用户使用结尾的下划线,以免与Python关键字和/或Python内置插件冲突:

No semantics are associated with a trailing underscore. According to PEP 8, the style guide for Python, users are urged to use trailing underscores in order to not conflict with Python keywords and/or Python built-ins:

single_trailing_underscore_:按惯例用于避免与Python关键字发生冲突,例如

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

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

使用set_表示集的内置名称,即set,不会在函数调用期间被遮盖并丢失其已知引用.

Using set_ means that the built-in name for sets, i.e set, won't get shadowed and lose its known reference during the function call.

这篇关于Python中变量名后的下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆