5 ms·
That actually proves my point. I'm not really good in learning certain facts by heart so if I see a < b < c it is going to take multiple seconds, likely followe
by stinos 7y ago
That actually proves my point. I'm not really good in learning certain facts by heart so if I see a < b < c it is going to take multiple seconds, likely followed by an internet search so add a couple of minutes to that, before I can be 100% sure what it does. I mean, I know what precedence is and I'd probably assume it would be (a < b) < c but that's not going to cut it.
- aktuel 7y agoJust program in assembler then. Honestly, Python's way is the only sane way to parse this. This is how a human who never programmed before with some knowledge of math would understand it. Everyone's else mind is just corrupted by broken legacy languages.
- xyproto 7y agoMath notation is just one of many possible notations, though. And math notation is not static throughout history.
- aktuel 7y agoTrue. But the syntax of almost all other languages are derived from the same basic math syntax we use commonly for at least since programming languages are a thing.
- maest 7y agoEven if Python's way was the only sane way of doing it (view which, btw, denotes having narrow experience with programming languages), that's irrelevant. GP's point was that different languages use different rules so, unless you use brackets, you won't easily know which precedence rules get applied.
- Retric 7y agoPython’s edge cases are just as odd as every other language >>> a=16 >>> a*a is a*a True >>> a=17 >>> a*a is a*a False “is”, “==“, and “=“ are all very different and none of them match their mathematical equivalents very well.
- dhosek 7y agoNot really familiar with Python. Do you know why the second statement evaluates as false?
- mkl 7y agoSmall integer caching: https://wsvincent.com/python-wat-integer-cache/ https://wsvincent.com/python-wat-integer-cache/ The objects for small integers are built-in and static, but 17^2 is too big, and so new integer objects are created when the expressions are evaluated. "is" checks if two expressions represent the same object in memory. "==" checks for equality of values.
- xen0 7y agoCPython keeps an array for "small value" integers (I think in the range [-5, 256]). So when Python wants 256, it'll give you a reference to the existing object. Larger numbers require a brand new object.
- h8hawk 7y ago`is` is identity comparison and in python everything is "object". It check if two variable refer to same object (location in memory) or not. So you should not expect that `a is b` for `a=1.2 , b=1.2` be true. In this case actually first expression returned True is weird. because 0-256 objects are cached and every instance of them refer to same object.
- apricot 7y agoI sometimes teach programming to beginners who have some knowledge of math. A common mistake they make is to write compound conditionals like this: if a == 2 or 3: do stuff because that's how they would write it in math. Does that mean Python is broken and should support that construct as well?
- edflsafoiewq 7y agoI believe COBOL supports this.
- shkkmo 7y agoWhen you use a tricky language feature to improve readability, you can just add a comment to the link some piece of documentation for that behavior. It is still a trade-off, but it saves a bit of time and whenever I come across a place where past me did this, I am always appreciative.
- stinos 7y agoDepends. For obscure behavior features, ok, for 'language features' I'm probably still going to pass. Because in this example case that would come down do # See stackoverflow/highlyupvoted/wtf/does/python/do/here # This is the same as (a < b) < c. x = a < b < c whih is not clearer, not easier to parse and not faster to read and harder to maintain (leads to rot easily when variable names change) than the non-ambiguous other choice.
- shkkmo 7y agoI would have skipped that second line and just left the line with the documentation link. If you are going to provide the equivalent code in a comment, you might as well just use the equivalent code.