python 中的 unicode 标识符是不好的做法吗?

Are unicode identifiers in python bad practice?

我正在学习机器学习,为了记录我的代码,我将在 Jupyter/IPython 笔记本中的代码旁边编写函数的 LaTeX 数学版本。数学定义包括许多希腊符号,所以我想我也可以在函数和变量名中使用希腊符号,因为这在 python 中是可能的。这是不好的做法吗?

official style guide 是这样说的:

For Python 3.0 and beyond, the following policy is prescribed for the standard library (see PEP 3131): All identifiers in the Python standard library MUST use ASCII-only identifiers, and SHOULD use English words wherever feasible (in many cases, abbreviations and technical terms are used which aren't English). In addition, string literals and comments must also be in ASCII. The only exceptions are (a) test cases testing the non-ASCII features, and (b) names of authors. Authors whose names are not based on the latin alphabet MUST provide a latin transliteration of their names.

Open source projects with a global audience are encouraged to adopt a similar policy.

换句话说:如果您的目标是全球受众,则最好只使用 ascii。如果代码仅供您的团队阅读,这是一个偏好问题。

真的,个人看法。请记住,变量名称的 Unicode 字符支持仅在 Python 3 中,因此请确保任何外部库都支持 Python 3。除此之外,没有理由拒绝。

在这些假设下这似乎是一个很好的用例:

  • 观众精通数学,
  • 您使用了很多 Jupyter Notebook 功能,例如内联绘图和 table 显示(例如 pandas)以便在笔记本之外使用您的代码 不太可能,
  • 它是应用程序代码而不是其他人将使用的库。

提示:在笔记本中输入希腊字母很简单。 只需键入 LaTeX 数学符号和 TAB。 例如,键入:

\pi

然后TAB键得到一个π