question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recipe: Consider the following seemingly-innocent program:

a = 3==4
print("yyyyy", a, 0, a==0)

Desired and expected output: yyyyy False 0 True

This works as expected under real python from the command line. The corresponding javascript also works as expected. However…

Observed rapydscript behavior: yyyyy false 0 false

This is 100% reproducible chez moi. I am using the latest rapydscript-ng, from a fresh git-pull. FWIW the old non-ng version exhibits the same misbehavior.

This is more important than it might seem. The example above is highly simplified, to facilitate debugging. The same issue affects more-complicated code in ways that are much harder to understand. This wastes significant amounts of programmer time.

This started out as BruceSherwood/glowscript#60 but is hereby pushed upstream.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
kovidgoyalcommented, Jul 14, 2016

I’m going to spell it out one last time for anyone else reading this bug report.

  1. There is only one possible reason to do a == 0 when a is of unknown type, and that is to check its truthiness. You trying to move the goalposts notwithstanding.

  2. Using a == 0 to check truthiness is wrong, because it fails when a is any of; None, empty list, empty tuple, empty set, empty dict, empty string, any user defined type with a __bool__ method.

  3. Your proposed solution, to use the javascript == operator instead of the === operator is so stupid, it boggles the mind. You want to bring in all the infelicities of the JS == operator in order to enable a == 0, which is a stupid thing to do in the first place. And here is a link that explicates some of the problems with the == operator in JS. http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons

@JohnDenker Dont bother replying (although I suspect you cannot resist) I have blocked you.

0reactions
JohnDenkercommented, Jul 13, 2016

Sigh, go read up on truthiness.

I agree that reading the language specifications is a good idea in situations like this.

Just recently I was reading https://docs.python.org/3.1/library/stdtypes.html#numeric-types-int-float-complex where the second sentence says: Booleans are a subtype of integers.

Python considers False == 0 to be a comparison between integers. Booleans inherit comparison from integers. They inherit a lot of other things, too; for example: (1==1) + (2==2) evaluates to 2.

Calling it «wrong in python» does not make it so. Calling it «insane» and «idiotic» and «asinine» does not make it so.

If you wish to argue that booleans ought not be a subtype of integers, please argue with the folks at python.org, not with me.

Javascript arrives at the same destination via a different path, via automatic type conversions, as previously discussed. The automatic conversion of integers to booleans, and vice versa, is mandatory. It is not open to question.

Read more comments on GitHub >

github_iconTop Results From Across the Web

1 = false and 0 = true?
There's no good reason for 1 to be true and 0 to be false; that's just the way things have always been notated....
Read more >
language design - Why is 0 false?
0 is false because they're both zero elements in common semirings. Even though they are distinct data types, it makes intuitive sense to...
Read more >
Is 1 true and 0 false in programming?
There is no general rule that 1 represent true and 0 represent false. Depending on language and context, 0 can represent true and...
Read more >
How to correct a #VALUE! error in the IF function
Problem : The syntax is incorrect. If a function's syntax is not constructed correctly, it can return the #VALUE! error. Solution: Make sure...
Read more >
How to Fix the R Error: missing value where true/false needed
The reason why the “missing value where true/false needed” error message occurs is that you are passing an invalid value to “if statement”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found