Python Syntax Soup: x in y
Python has these operators to check if a key exist in hash table or element exist in list:
x in list
x not in list
There are several issues:
x not in list is not necessary. It can simply be done with
not (x in list). This way, you reduce special syntax and rely on function/expression composition.
not operator is part of
not in operator. This means, the words has context dependent semantics.
not in is a operator. Using English words as operator is not so good. but now that operator is 2 words, separated by space! So, syntactically, it becomes not distinguishable from statements.
x in y is subform the loop form
for x in y
[see Python: for, while, Loops]
, and also the list comprehension syntax
[… for x in y]
[see Python: List Comprehension]
. This is a example of context dependent semantics.
[see Why List Comprehension is Bad]
k in d is itself is not necessary. Python has a
has_key() method for dictionary, which is in sync with the 20 other methods on dict.
Method syntax is systematic and also informative of its meaning because of the name.
Though, strangely, python decided to deprecate it, and in python 3, it's gone.
[see Python: Dictionary]
This is the state of the affair of syntax soup. When you learn a language, there's little governing principle of what symbol can go where (as in formal language), instead, you learn by rote of what symbols or words can go where in what context means what.
Here's another example.
the python import statement syntax, is a prime example of context dependent semantics. witness:
from aa import bb
in the above, the two
import name have different meaning.
Context Dependent Syntax
- Lambda in Python 3000
- The Fate of Lambda in Python 3000 and Scheme v300
- Python, Lambda, Guido: is Language Design Just Solving Puzzles?
- From Why Not Ruby to Screw Python Hello Ruby
- Docstring Convention: Python vs Emacs Lisp
- Why Learn Lisp When There Are Perl and Python
- Python Problem: String Methods, Functions, Slashes, Backslashes