我想构建一个计算
的 Python 函数,并且想要将我的求和函数命名为 Σ。以类似的方式,想使用 Π 表示乘积,等等。我想知道是否有办法以这种方式命名 python 函数?
def Σ (..):
..
..
也就是说,Python 是否支持 unicode 标识符,如果支持,有人可以提供一个示例吗?
谢谢!
这样做的最初动机是我今天看到的一段 Clojure 代码,看起来像,
(defn entropy [X]
(* -1 (Σ [i X] (* (p i) (log (p i))))))
其中 Σ 是一个定义为的宏,
(defmacro Σ
... )
我认为这非常酷。
顺便说一句,为了解决一些关于可读性的评论 - 例如,对于大量统计/机器学习代码,能够使用符号组合操作将非常有帮助。 (特别是对于非常复杂的积分等)
φ(z) = ∫(N(x|0,1,1), -∞, z)
vs
Phi(z) = integral(N(x|0,1,1), -inf, z)
甚至只是 lambda() 的 lambda 字符!
I want to build a Python function that calculates,
and would like to name my summation function Σ. In a similar fashion, would like to use Π for product, and so on. I was wondering if there was a way to name a python function in this fashion?
def Σ (..):
..
..
That is, does Python support unicode identifiers, and if so, could someone provide an example for it?
Thanks!
Original motivation for this was a piece of Clojure code I saw today that looks like,
(defn entropy [X]
(* -1 (Σ [i X] (* (p i) (log (p i))))))
where Σ is a macro defined as,
(defmacro Σ
... )
and I thought that was pretty cool.
BTW, to address a couple of comments about readability - with a lot of stats/ML code for instance, being able to compose operations with symbols would be really helpful. (Especially for really complex integrals et al)
φ(z) = ∫(N(x|0,1,1), -∞, z)
vs
Phi(z) = integral(N(x|0,1,1), -inf, z)
or even just the lambda character for lambda()!
发布评论
评论(5)
(我认为这也很酷,这可能意味着我们是极客。)
您可以使用上面在 Python 3 中的代码来完成此操作。(它至少可以在我的 Python 3.1 解释器中工作。)请参阅:
但在Python 2中,标识符只能是ASCII字母、数字和下划线。
(I think it’s pretty cool too, that might mean we’re geeks.)
You’re fine to do this with the code you have above in Python 3. (It works in my Python 3.1 interpreter at least.) See:
But in Python 2, identifiers can only be ASCII letters, numbers and underscores.
值得指出的是,Python 3 确实支持 Unicode 标识符,但只允许类似字母或数字的符号(请参阅 http://docs.python.org/3.3/reference/lexical_analysis.html#identifiers 了解完整详细信息)。这就是 Σ 起作用的原因(请记住,它是一个希腊字母,而不仅仅是一个数学符号),但 √ 不起作用。
对于任何感兴趣的人,我创建了一个网站,列出了 Python 变量中有效的每个 Unicode 字符 https://www.asmeurer.com/python-unicode-variable-names/ (请注意,它们的数量相当多,实际上超过 100000 个)
It's worth pointing out that Python 3 does support Unicode identifiers, but only allows letter or number like symbols (see http://docs.python.org/3.3/reference/lexical_analysis.html#identifiers for full details). That's why Σ works (remember that it's a Greek letter, not just a math symbol), but √ doesn't.
For anyone interested, I made a website that lists every Unicode character that is valid in a Python variable https://www.asmeurer.com/python-unicode-variable-names/ (be warned that there are quite a lot of them, over 100000 in fact)
(这个答案只是一个小附录,而不是一个完整的答案)
unicode 标识符的额外陷阱(@mike-desimone 提到过,当我认为这是一个很酷的线程并切换到终端来玩它时,我很快就发现了) ,就如何在每个平台上获取每个字形而言,每个字形的多个版本并不等效。例如 Σ(又名希腊大写字母 sigma,又名 U+03A3,[找不到直接的 mac 输入法])就可以,但不幸的是 Σ(又名N-ary Summation,又名 >U+2211,又名 opt/alt-w(使用 Mac OS X))不是有效的标识符。
但是,
如果您有多个平台上的多个开发人员为您的代码做出贡献,则专门使用 Σ (通常可能是 unicode 字符)作为标识符可能会产生一些非常难以诊断的错误,例如,直观地调试此内容:
这两个字形在此页面上更容易区分,但根据所使用的字体,这可能不会就这样吧。
除非 Σ 打印在 Σ 附近,否则即使回溯也不会更清晰
(this answer is meant to be a minor addendum not a complete answer)
The additional gotcha to unicode identifiers (which @mike-desimone mentions and I discovered quickly when I thought this was a cool thread and switched to a terminal to play with it), is the multiple versions of each glyph are not equivalent, with regards to how you get to each glyph on each platform. For example Σ (aka greek capital letter sigma, aka U+03A3, [can't find a direct mac input method]) is fine, but unfortunately ∑ (aka N-ary Summation, aka U+2211, aka opt/alt-w using Mac OS X) is not a valid identifier.
but
Using Σ specifically (and probably unicode chars in general) as an identifier might generate some very hard to diagnose errors if you have multiple developers on multiple platforms contributing to your code, for example, debug this visually:
The two glyphs are easier to differentiate on this page, but depending on the font used, this may not be the case.
Even the traceback isn't much clearer unless Σ is printed near the ∑
根据这很糟糕,您可以使用一些 unicode 字符,但不是全部:您只能使用标识为字母的字符。
另外:我认为能够使用 unicode 作为标识符是非常酷的 - 我希望我可以使用所有。
我使用 neo 键盘布局,它在额外的层上提供了希腊语和数学符号:
αβχδεφγψιθκλνοπphiστ[&ωψυζ
∀⇐ℂΔ∃ΦШ∫Λ⇔Σεℚℝ∂⊂√∩Ξ
According to is it bad, you can use some unicode characters, but not all: You are restricted to characters identified as letters.
Besides: I think it is very cool to be able to use unicode as identifiers - and I wish, i could use all.
I use the neo keyboard layout, which gives me greek and math symbols on extra layers:
αβχδεφγψιθκλνοπϕστ[&ωξυζ
∀⇐ℂΔ∃ΦΓΨ∫Λ⇔Σ∈ℚℝ∂⊂√∩Ξ
Python 2.x 不支持 unicode 标识符,因此不支持 Σ 作为标识符。 Python 3.x确实支持 unicode 标识符,尽管如果必须使用标识符 A 和 A(拉丁文 A 和希腊大写字母 alpha)等标识符编辑源文件,许多人会感到生气。Sigma 通常是足够可读,但仍然不如单词 sigma那么可读,那为什么还要麻烦呢?
Python 2.x does not support unicode identifiers, and consequently does not support Σ as an identifier. Python 3.x does support unicode identifiers, although many people will get cross if they have to edit source files with, for example, identifiers A and Α (latin A and greek capital alpha.) Sigma is often readable enough, but still, not as readable as the word sigma, so why bother?