Why prefer != over <> for Python 3.0?

K

kwitters

I don't know if this is the right place to discuss the death of <> in
Python 3.0, or if there have been any meaningful discussions posted
before (hard to search google with '<>' keyword), but why would anyone
prefer the comparison operator != over <>???

I've written an article about it to try and save this nice "is not
equal" operator, located at http://dewitters.koonsolo.com/python_neq.html

Please set it straight in 3.0, and if not, convince me with a good
reason of doing so, so that I can live with it and don't have to spend
the rest of my life in 2.x ;).
 
P

Paul Rubin

I don't know if this is the right place to discuss the death of <> in
Python 3.0, or if there have been any meaningful discussions posted
before (hard to search google with '<>' keyword), but why would anyone
prefer the comparison operator != over <>???

I doubt anyone cares. Python probably chose != because it's what C uses.
The scary choice is /= which can be interpreted as an assignment. Python
lacks assignment expressions partly because of a meme that using = instead
of == by accident is a common C bug. I'm sure it happens but at least in
my own experience (from having written plenty of buggy C code over the
years) it's not that frequent. Using /= instead of != seems more likely,
for programmers who switch between C and languages that use /= for nonequality.
 
S

Steven D'Aprano

I doubt anyone cares.

[channeling Luke Skywalker sotto voice]
I care.

Python probably chose != because it's what C uses.

Coming from a background in Pascal, I originally hated the look of != and
preferred <> but I've now got used to the look of it.

When I design my own language (ha!), I'll use != for "not equal to" and
reserve <> for "greater than or less than but not equal to" which is
subtly different. (Think about unordered values, where x != y does not
imply that x said:
The scary choice is /= which can be interpreted as an assignment.

"Can be"?
2.5


Koen, I've read your blog and I'm afraid that your reasoning is specious.
You say:

For comparison (not assignment!), they use operators like <, >, <=
(smaller or equals), >= (larger or equals), == (double '=' so there is no
confusion with assignment).

All pretty clear hey? But now comes the catch, there exists an operator
!=... but what does it mean? Well, that one is pretty easy, of course !
must be an operator of its own (in non-python languages meaning 'not'),
and it resembles the fancy assignment statement, so
a != b

must mean "assign the value 'not b' to 'a'... right?... Wrong! Somehow
this is a comparison operator meaning "not equals"... what??? Yes, that's
right, but hey, you learn to live with it eventually.
[end quote]

Given that <= is a comparison operator, not an assignment, why do you
jump to the conclusion that != is an assignment? Why don't you argue that
"x <= y" means "assign the value of x<y to x"?

Since you jump to an invalid conclusion about !=, the rest of your
argument fails.
 
P

Paul Rubin

Steven D'Aprano said:
reserve <> for "greater than or less than but not equal to" which is
subtly different. (Think about unordered values, where x != y does not
imply that x < y or x > y, e.g. IEEE NaNs.)

Heh, good point.
"Can be"?

Yes, what I mean is that some languages (e.g. Ada, Haskell) use /= for
nonequality. So if you switch between Haskell and C, you could find
yourself typing /= when you mean != and the compiler won't flag it.
 
L

Lie

I don't know if this is the right place to discuss the death of <> in
Python 3.0, or if there have been any meaningful discussions posted
before (hard to search google with '<>' keyword), but why would anyone
prefer the comparison operator != over <>???

I've written an article about it to try and save this nice "is not
equal" operator, located athttp://dewitters.koonsolo.com/python_neq.html

Please set it straight in 3.0, and if not, convince me with a good
reason of doing so, so that I can live with it and don't have to spend
the rest of my life in 2.x ;).

<quote who=OP's blog>
All pretty clear hey? But now comes the catch, there exists an
operator !=... but what does it mean? Well, that one is pretty easy,
of course ! must be an operator of its own (in non-python languages
meaning 'not'), and it resembles the fancy assignment statement
<...snip...>

When I started to learn python I came across an operator <>. What
could it mean? Well, you don't have to think long about it. You're
only reference are the < and > operators (with meanings the same as in
mathematics or plain written language, any non programmer would
understand this). So <> must mean "smaller or larger", or not-equals,
no question about that. It couldn't possible be mistaken as an
assignment operator. You just have to love a language that uses
operators with clear and obvious meaning.
</quote>

You're forcing your argument too much, both != and <> are NOT standard
mathematics operators -- the standard not-equal operator is >< -- and
I can assure you that both != and <> won't be comprehensible to non-
programmers. And I'm not sure if anyone could have mistaken != for
assignment operator because they're used in completely different
places (which is the reason why some languages can use the same
operator for = (assignment) and == (comparison)). = is always used in
a line by itself while != is always used in places where a
'condition' (a.k.a. 'expression') is required.

The problem you stated may be a problem in C/C++ (if C/C++ do have
both != and <>, fortunately they only have !=) because C/C++'s =
(assignment) is an operator and operators always return a value but in
Python = (assignment) is a statement and can't return a value so "if a
= b:" always raise an error.

Since != operator can only be used in places where a
'condition'/'expression' is required and never in a line by itself as
a statement, it could never be mistaken as an assignment (which is a
statement).

The problem just don't exist in Python.
 
D

Duncan Booth

Lie said:
You're forcing your argument too much, both != and <> are NOT standard
mathematics operators -- the standard not-equal operator is >< -- and
I can assure you that both != and <> won't be comprehensible to non-
programmers.

My maths may be a bit rusty, but I always thought that the standard not-
equal operator was like an = sign but with a diagonal slash through it as
displayed when you do:

print u'\u2260'
 
L

Lie

My maths may be a bit rusty, but I always thought that the standard not-
equal operator was like an = sign but with a diagonal slash through it as
displayed when you do:

print u'\u2260'

Ah yes, that is also used (I completely forgot about that one, my
math's aren't that sharp anymore) and I think it's used more
frequently than ><. Some books use >< while most use $B!b(B, but my
argument was that no math book use != or <> (except in math for
programmers).
 
C

Carl Banks

why would anyone

Questions that begin with the words "why would anyone" are almost
always betray an arrogance about their own beliefs and an ignorance
(or feigning ignorance) of human nature.

Wiser folks know better than to phrase this question so judgmentally.

Please set it straight in 3.0, and if not, convince me with a good
reason of doing so, so that I can live with it and don't have to spend
the rest of my life in 2.x ;).

1. It's not going to change in Python 3.0.

2. It's a silly thing to care so much about that you will avoid using
a langauge because of it.


Carl Banks
 
D

Dan Bishop

I doubt anyone cares. Python probably chose != because it's what C uses.

MOST of Python's operators are based on C's. Consider, for example,
the bitwise operators | ^ & << >> ~ and the compound assignment
operators += -= etc.

The exceptions are ** (from Fortran), //, and the logical operators.
 
G

Gabriel Genellina

En Sat, 29 Mar 2008 16:24:01 -0300, Michael Wieher
to me it seems simple.

C uses !=

why does C use != .... because its kind of hard to type the "equal with a
slash"

In C, ! by itself is the logical "not", so !(a==b) is the same as (a!=b)
and that's rather consistent.
Python doesn't use ! for anything else; != is rather arbitrary but
certainly much better than <> (for many objects < and > are meaningless;
being equal or not equal has nothing to do with being less or greater)
 
S

Steven D'Aprano

1. It's not going to change in Python 3.0.

2. It's a silly thing to care so much about that you will avoid using a
langauge because of it.


I dislike the attitude that "oh, feature X is unimportant, why should we
care about it?". It's often -- but not always -- said by those who do
care very much about it themselves, except they prefer the way it is
rather then the requested feature.

If Guido had a sudden brain tumor and replaced comparison operators with
Fortran-style operators, it wouldn't destroy Python. It would just be a
minor wart on an otherwise good language. So why should we care about
using == instead of .EQ.?

Why does Python use # for comments instead of Basic-style remarks? Would
it be silly to care if Python 3 discarded # and introduced REM?

I could belabor the obvious with dozens of syntax elements which, *in
isolation*, it would be silly to care about. But syntax defines the feel
of the language. Perl and Java have better (or at least *larger*)
libraries, although arguably not as many "batteries included", but the
syntax, oh my. We use Python because it is Python and not Perl or Basic
or Fortran or Java. If the syntax changes, so does the language.

Yes, in isolation the question of != versus <> is a tiny little thing,
silly to drop Python merely because of it. But it's not silly to care
about the feel of the language. Python is as good as it is because Guido
cares very much indeed about the feel of the language, and so do many of
we Python users.
 
A

Aahz

I don't know if this is the right place to discuss the death of <> in
Python 3.0, or if there have been any meaningful discussions posted
before (hard to search google with '<>' keyword), but why would anyone
prefer the comparison operator != over <>???

Are you asking why Python 3.0 gets rid of one of them or are you asking
why != was chosen over <>?
 
H

hdante

I don't know if this is the right place to discuss the death of <> in
Python 3.0, or if there have been any meaningful discussions posted
before (hard to search google with '<>' keyword), but why would anyone
prefer the comparison operator != over <>???

I've written an article about it to try and save this nice "is not
equal" operator, located athttp://dewitters.koonsolo.com/python_neq.html

Please set it straight in 3.0, and if not, convince me with a good
reason of doing so, so that I can live with it and don't have to spend
the rest of my life in 2.x ;).

I don't know, but we should have only one operator.

BTW, my opinion is that it's already time that programmer editors
have input methods advanced enough for generating this:

if x ¡Á 0:
¢£y ¡ô s:
if y ¡Ã 0: f1(y)
else: f2(y)

;-)
 
C

Carl Banks

1. It's not going to change in Python 3.0.
2. It's a silly thing to care so much about that you will avoid using a
langauge because of it.
[snip indignant rant]

But it's not silly to care
about the feel of the language.

I'm not exactly sure who you're arguing with, bud. No one around here
that I can tell said anything about what you're railing against.


Carl Banks
 
G

Gabriel Genellina

BTW, my opinion is that it's already time that programmer editors
have input methods advanced enough for generating this:

if x ≠ 0:
∀y ∈ s:
if y ≥ 0: f1(y)
else: f2(y)

Fine if you have the right keyboard... Try to write APL with a standard
keyboard :)
 
D

dewitters

Given that <= is a comparison operator, not an assignment, why do you
jump to the conclusion that != is an assignment? Why don't you argue that
"x <= y" means "assign the value of x<y to x"?

Yes, you are right, that could be confusing ;).
Since you jump to an invalid conclusion about !=, the rest of your
argument fails.

No, you said <= could be confusing, but we're talking about <> here,
and there is no confusion about that :).
 
D

dewitters

You're forcing your argument too much, both != and <> are NOT standard
mathematics operators -- the standard not-equal operator is >< -- and
I can assure you that both != and <> won't be comprehensible to non-
programmers.

What I meant was that both < and > are standard mathematics operators,
and that by that knowledge one could deduce what <> means. But ><
would also be fine by me :).
 
D

dewitters

MOST of Python's operators are based on C's. Consider, for example,
the bitwise operators | ^ & << >> ~ and the compound assignment
operators += -= etc.

The exceptions are ** (from Fortran), //, and the logical operators.

Borrowing parts out of other languages (C in this case) is not a
problem, but I don't think there is a need to try be consistent with
C. It's Python, not C, so we should do things better ;).
 
G

Gabriel Genellina

On Mar 29, 12:41 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:

No, you said <= could be confusing, but we're talking about <> here,
and there is no confusion about that :).

Yes, there is: <> looks like "less or greater", and being equal or not is
a different thing that being less or greater. Trichotomy law holds for
real numbers (although some people don't even accept that) but not for
floating point (NANs); and Python isn't about numbers only. For a lot of
objects it makes sense to compare them for equality or not, but you can't
say which one is greater than the other (the weather forecast, books in a
bookstore)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top