tags:

views:

326

answers:

5

I have always wondered why can't we use hyphens in between function names and variable names in 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

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?

+6  A: 

Because it would make the parser even more complicated. It would be confusing too for the programmers.

Consider def is-even(num): : now, if is is a global variable, what happens?

Also note that the - is the subtraction operator in Python, hence would further complicate parsing.

jldupont
A: 

Many languages don't support hyphens within identifiers.

Bart Kiers
+18  A: 

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 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 dialects don't have this problem, since they use prefix notation. For example, there's clear difference between

(is-even 4)

and

(- is (even 4))

in Lisps.

mipadi
Regarding Lisp, the prefix aspect is irrelevant; the difference is rather that “-” is not considered necessarily a separate token; any combination of letters and 'ordinary' punctuation (e.g. not “)”) can make up a single token. An infix language could work perfectly well with such rules; then “`is-even(another_var)`” would be a function call and “`is - even(another_var)`” would be the subtraction, just as in Lisps `(is - even 4)` is a different s-expression from `(is-even 4)`.
Kevin Reid
Lisp doesn't have this problem because "-" is just a character.
S.Lott
A: 
is-even(num)

contains a hyphen ? I thought it was a subtraction of the value returned by function even with argument num from the value of is.

As @jdupont says, parsing can be tricky.

Mark

High Performance Mark
`is` is a keyword, it can't have a value :)
balpha
Parsing can be tricky. Subtracting a value from a keyword is likely to cause the parser to complain.
High Performance Mark
+4  A: 

Because Python uses infix notation to represent calculations and a hyphen and a minus has the exact same ascii code. You can have ambiguous cases such as:

a-b = 10
a = 1
b = 1

c = a-b

What is the answer? 0 or 10?

JPvdMerwe
I got this one. It seems very straight. Any language which has infix notation can not use hyphens. Hyphens are only allowed in the language which supports S-expression. :)
aatifh
Well, a programming language whose own character set is Unicode (they're coming) could easily distinguish between a hyphen and a minus sign. The problem is that too many languages can't distinguish, in their character sets, between hyphens and minus signs. It's the poverty of the character set, not the poverty of expressibility.
High Performance Mark
You're right, though that ability would be nasty if someone took advantage of it and used it in their identifiers.
JPvdMerwe
Man, I wouldn't want to have to visually distinguish between `‐` and `−` whilst debugging...
bobince