33

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?

7
  • 1
    That syntax error happens because is is a keyword. In def mightbe-even(num), the syntax error would be because of the hyphen. Commented Jan 14, 2010 at 13:36
  • I've always wondered why ASCII has "-" and "". While you're wondering about uses for "-", could you also wonder about uses for "", too? Indeed, I've got lots of punctuation questions that are as important as this one. Why is the "#" and the "♯" different? Can you wonder about that, also? I've always found it odd that we can't use "♯" for comments. Commented Jan 14, 2010 at 13:51
  • (@aatifh, please see meta.stackexchange.com/questions/35582/… -- did you change the tag from pyhon to python? And maybe also removed a trailing backtick in the title?) Commented Jan 14, 2010 at 16:03
  • @Arjan yes, i did. I realized within 5 minutes after posting the question that the tag is incorrect. But didn't edit the title. Commented Jan 15, 2010 at 10:21
  • @S.Lott The answer is very straight and simple for my question. Just we need to understand the difference between sexp and Infix notations. Which i realized after seeing answers. Commented Jan 15, 2010 at 10:24

5 Answers 5

62

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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).
Lisp doesn't have this problem because "-" is just a character.
I can see that '-' should be used for the minus operator and that infix notation is good for readability (for those who did'nt grow up with prefix notation calculators). However, is there a compelling reason to allow the absence of whitespace around the minus and other operators? Seeing as whitespace is already significant in Python, why not force the use of whitespace around operators which would allow the use of hyphens in names, which in my opinion is a far more readable and easier to type convention than both under_score and camelWTFCase, which I dislike in equal amounts.
“Lisp dialects don't have this problem, since they use prefix notation.”—Dylan is infix and uses hyphens in variable names.
15

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?

5 Comments

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. :)
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.
You're right, though that ability would be nasty if someone took advantage of it and used it in their identifiers.
Man, I wouldn't want to have to visually distinguish between and whilst debugging...
@bobince Those two characters look to be as easy to distinguish as - (hyphen) and (en-dash). So quite easy. (One can always see when someone is misusing hyphens when they should be using dashes in prose.) Although I wouldn’t recommend putting anyone in a situation where a hyphen or a minus sign could be mistaken for each other, given that text editors tend to use monospaced fonts.
8

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.

1 Comment

How would it make parsing more complicated? Here are two rules: (1) infix operators are surrounded by one or more whitespace; (2) infix operators are surrounded by zero or more whitespace. Is the second rule appreciably more complicated than the first rule?
2
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.

2 Comments

is is a keyword, it can't have a value :)
Parsing can be tricky. Subtracting a value from a keyword is likely to cause the parser to complain.
1

Oddly enough it is possible to have class variable names with hyphens using setattr(), not that you would want to. Here is an example:

class testclass:
    pass

x = testclass()
setattr(x, "is-even", True)
getattr(x, "is-even")
True

This still fails:

x.is-even
File "<stdin>", line 1
  x.is-even
     ^
SyntaxError: invalid syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.