4 ms·
Python’s misleading readability
- golergka 9y agoThe second example, `if answer == "y" or "yes":` isn't even about Python - it's about understanding basic boolean notation, that takes directly from math. First example is similar - but coming from any programming language, you already know that comparison is `==` operator, and whatever `is` is, it's probably something else.
- eesmith 9y agoI think the sentence in the introduction, "Beginners construct valid Python expressions that don’t do what they seem like they should do.", is meant to include people who aren't coming from any programming language.
- tome 9y agoYes, moreover, it should be a type error.
- eesmith 9y agoIt isn't a type error: % python Python 3.6.0 ... >>> answer = "yes" >>> if answer == "y" or "yes": ... print("Thanks") ... Thanks The expression is the same as: (answer == "y") or "yes" If answer is 'y' then the if-expression evaluates to True; for any other string it evaluates to "yes": >>> "y" == "y" or "yes" True >>> "yes" == "y" or "yes" 'yes' >>> "no" == "y" or "yes" 'yes' Both True and 'yes' are considered True for an if-expression.
- deleted 9y ago[deleted]
- elcapitan 9y agoSo the use case where you actually want that exact syntax is "if a == someValue or someVariableIsNotNull"
- dllthomas 9y agoI think you read the parent as saying "I expect that Python currently reports this as a type error", which of course is not the case. I suspect, however, that the parent intended something more along the lines of "Other things being equal, the better language would reject this as a type error."
- in9 9y agoI too find those comparisons confusing. Being a python programmer for 3 years now, I can see what actually made me keep learning it to the point I use it professionally, daily. The language has enough mixes of advanced features that allowed me to curate what features I learned (and then used). A classic example is the use of the logging module and decorators for login function calls. I did the whole thing I wanted to do without proper logging, but when I wanted to add logging to a few function calls, I simple research pointed me to using decorators and the logging module. And so, more python goodness for myself. That led to other features such as the decorated classmethods and etc, but all in my own pace. That is something that I've only encountered with python.
- tpaschalis 9y agoAfter guiding beginners through Python, one crucial advantage it has over other language for beginners is good documentation (and a lot of it), and a readily available REPL. The beginner can quickly test different variables/data structures, observe that something's fishy, and then read the documentation to find out what the code is supposed to do. Contrast that to a beginner debugging a similar problem in, say C, which would be far more difficult.
- vgy7ujm 9y agoTime to move to Perl 6. https://news.ycombinator.com/item?id=16211864 https://news.ycombinator.com/item?id=16211864
- in9 9y agoHaha, no.
- jwilk 9y agoSometimes Python leans to the DWIM side: >>> 42 is not None True >>> 'oo' in 'foobar' True One could argue that both answers should be False, because: >>> 42 is (not None) False >>> 'oo' in iter('foobar') False
- elcapitan 9y agotldr: "is" checks for identity, not equality "a == b or c" is different from "a == b or a == c"
- okket 9y agoSometimes ugly verbosity in programming languages is helpful, like 'is' -> 'isObject' or similar. Or choose a special Unicode symbol for every case. Unambiguous, but make sure you bury a Rosetta stone somewhere.
- gtirloni 9y agoIt's puzzling that "x is 17" should work but then "1000 + 1 is 1001" doesn't. The reason is that Python pre-allocates 262 numbers at initialization (-5 to 256) and the allocation uses a different method/structure for bigger numbers. >>> x = 256 >>> x is 256 True >>> id(x) 94900419138400 >>> id(256) 94900419138400 >>> x = 257 >>> x is 257 False >>> id(x) 94900419504344 >>> id(257) 94900419504368 Reference: https://www.laurentluce.com/posts/python-integer-objects-implementation/ https://www.laurentluce.com/posts/python-integer-objects-imp...