Suggest adding the ‘&&’ operator instead of ‘and’, and the ‘||’ operator instead of ‘or’ in Python.
Hello, @tygk, and welcome to Python Discourse!
What is the advantage of switching to
||? Is it an issue of readability of expressions?
The advantage of doing this is that it is more convenient to port program code from C or other languages to Python.
One difficulty to consider is whether
|| would become confused with the existing bitwise
| operators. Currently, it is visually easy to quickly distinguish the boolean from the bitwise operators when reading code.
We will see the opinions of others about this.
There are already a lot of other simple transformations to be done. I don’t think it’ll make a lot of difference which way these are spelled.
Perhaps some will consider this to be purely a matter of personal taste, and therefore frivolous on my part, but in my opinion, the early designers of Python made many clever design decisions from the perspective of aesthetics and cognitive ergonomics. See Wikipedia: Cognitive ergonomics.
or operators lend an appropriate high level aesthetic to expressions, while the bitwise
| operators create a more low level machine level feel that is also appropriate. This is in fitting with the meaning of those operators. That manner of language style along with other design features makes Python code easier and more pleasant to read than C code, in my opinion. I could cite many other design decisions regarding Python in support of this view, but will refrain from doing so for now, for fear that it would distract from the current discussion.
I have see this done to make C look like other languages:
#define and && #define or || #define not !
In C++ since 98 they have been keywords.
Python can still retain the ‘and’ and ‘or’ operators, but it is recommended to add the ‘&&’ and ‘| |’ operators to improve Python’s flexibility.
I know what you are suggesting and I am pushing back to say the exact opposite.
Python’s choice of and and or is far better then the C && etc.
I think it would make python harder to work with. I gave the example of C++
adding and and or keywords to show that this is seen as a bad idea to not have keyword versions.
Anyway, convenience for users is the best choice.
The ship has sailed, really.
You should look into tools like GitHub - DAN-329/C_to_Python_translator: Using File I/O we were able to convert C code written in one text file to Python code in another text file with the application of multiple function that could identify and accordingly process specific key words and formats used in the C language..
My guess about this is that it’s to easy to accidentally type ‘&&’ or ‘&’ when you meant to other and wind up with a semantically “correct” program with a subtle bug.
The op’s argument that it would make translation easier goes against the idea that Python has fewer sharp edges for programmers than most other languages. An example of that is assignment being a statement, not an expression. Assignment as and expression is another gotcha from C that Guido actively avoided. I suspect and/or vs. &&/|| Is another example of that.
Python now has a syntax checker, and input errors are easily detected, which is not a problem.
I think you’ve missed my point. Suppose we have this expression in code someone else wrote (say, you’re performing a code review someone requested on GitHub) involving two typed ints:
a and b
Now postulate your change. It becomes:
a && b
Later on, you see
a & b
In the world you propose, both of the last two are syntactically correct.
Quick. Without tracking down other context for
b, can you tell me authoritatively which of the second or third (or both) uses are correct? With the current syntax, the binary operators used tell you what the programmer intended (granted, their intent may still be a semantic error), logical or bitwise operations. With the change you propose, you can’t be 100% sure of the programmer’s intent.
This has nothing to do with translating C to Python. It has everything to do with filing off sharp edges so non-expert programs don’t cut themselves.
A high-end syntax checker like Pylance would only help when typing & instead of && in cases where none of the two operands implement the
__rand__ methods (respectively). And assuming the checker can guess the type of the operands at all, which is never a given.
In all other cases, there’s no way for a checker to detect an error, since the difference is semantic and not syntactic as Skip pointed out.
The input error is your own error, not the error of the ‘&&’ or ‘| |’ or other operators. Don’t switch concepts. Convenience for users is the development direction of a language.
Well exactly. If we want to optimize user convenience, we want to avoid situations where they could make mistakes. && vs & operators seem like a great example of programmer mistake catalyzers.
Also, I’m surprised nobody mentioned that (although the points already mentioned are certainly sufficient), the
there should be one, and preferably only one (obvious) way to do it. This proposition adds a second way of doing the same thing.
And it adds another thing to the list of reserved syntax, which should never be wasted lightheadedly.
It is strongly recommended to add the ‘&&’ and ‘||’ operators in Python, while retaining the ‘and’ and ‘or’ operators. Those who want to use ‘&&’ and ‘| |’ can use it, while those who don’t want to use it can also use ‘and’ and ‘or’.
For clarity, I would recommend putting yourself as the subject of a sentence like this; it is not that some mythical entity is making this recommendation, but you yourself are requesting it.
If you don’t like using ‘&&’ and ‘| |‘, just use your ’and‘ and ‘or’. It’s that simple.