29

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?

Ebony Maw
  • 514
  • 7
  • 23
aatifh
  • 2,317
  • 4
  • 27
  • 30
  • 1
    That syntax error happens because `is` is a keyword. In `def mightbe-even(num)`, the syntax error would be because of the hyphen. – balpha Jan 14 '10 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. – S.Lott Jan 14 '10 at 13:51
  • (@aatifh, please see http://meta.stackexchange.com/questions/35582/inaccurate-revision-information -- did you change the tag from `pyhon` to `python`? And maybe also removed a trailing backtick in the title?) – Arjan Jan 14 '10 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. – aatifh Jan 15 '10 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. – aatifh Jan 15 '10 at 10:24
  • The important thing about Lisp and Clojure is not that they are functional, but that they use prefix notations. Haskell is functional and doesn't allow hyphens in identifiers; OTOH, there are dialects of Lisp that are very imperative and do allow them. – Max Jun 22 '12 at 14:28
  • If you want to be hated, you can always call your variable `is۔even`. – gerrit Feb 19 '19 at 10:30

5 Answers5

58

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
  • 398,885
  • 90
  • 523
  • 479
  • 12
    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 Jan 14 '10 at 13:43
  • Lisp doesn't have this problem because "-" is just a character. – S.Lott Jan 14 '10 at 13:49
  • 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. – Michael Bylstra Dec 19 '12 at 12:44
  • “Lisp dialects don't have this problem, since they use prefix notation.”—Dylan is infix and uses hyphens in variable names. – Guildenstern Oct 06 '20 at 16:05
14

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
  • 3,328
  • 3
  • 27
  • 32
  • 1
    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 Jan 14 '10 at 13:43
  • 1
    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 Jan 14 '10 at 13:55
  • You're right, though that ability would be nasty if someone took advantage of it and used it in their identifiers. – JPvdMerwe Jan 14 '10 at 14:07
  • 12
    Man, I wouldn't want to have to visually distinguish between `‐` and `−` whilst debugging... – bobince Jan 14 '10 at 14:26
  • @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. – Guildenstern Oct 06 '20 at 16:11
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.

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • 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? – Guildenstern Oct 06 '20 at 16:13
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.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

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
pekowski
  • 131
  • 3