问题描述
我一直想知道为什么我们不能在python中的函数名和变量名之间使用连字符
I have always wondered why can't we use hyphens in between function names and variable names in python
尝试过函数式编程语言,如 Lisp 和 Clojure,其中允许使用连字符.为什么python不这样做.
Having tried functional programming languages like Lisp and Clojure, where hyphens are allowed. Why python doesn't do that.
# This won't work -- SyntaxError
def is-even(num):
return num % 2
# This will work
def is_even(num):
return num % 2
我确信 Guido 爵士一定是出于某些原因这样做了.我用谷歌搜索但无法找到答案.任何人都可以对此有所了解吗?
I am sure Sir Guido must have done this because of some reasons. I googled but couldn't manage to find the answer. Can anyone please throw some light on this?
推荐答案
因为连字符用作减法运算符.想象一下,你可以有一个 is-even
函数,然后你有这样的代码:
Because hyphen is used as the subtraction operator. Imagine that you could have an is-even
function, and then you had code like this:
my_var = is-even(another_var)
is-even(another_var)
是对函数is-even
的调用,还是减去函数even
的结果来自名为 is
?
Is is-even(another_var)
a call to the function is-even
, or is it subtracting the result of the function even
from a variable named is
?
Lisp 方言没有这个问题,因为它们使用前缀表示法.例如,
Lisp dialects don't have this problem, since they use prefix notation. For example, there's clear difference between
(is-even 4)
和
(- is (even 4))
在 Lisps 中.
这篇关于为什么python不允许在函数和变量名中使用连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!