In dynamically typed language, truthiness is a term used to describe a value that might evaluate to boolean true.
Questions tagged [truthiness]
99 questions
1
vote
1 answer
pass truthy value of two non-boolean values to a function in a more terse way
My goal here is to pass the non-empty value of 2 given values (either a string or an array) to a function foo.
In Javascript I'd be able to do:
// values of variables a and b when calling foo
// a = "hello"
// b = []
foo( a || b )
// passes a,…

Ethicist
- 791
- 2
- 7
- 23
1
vote
2 answers
CodeBat Python - Logic 1 - where is the error in my logic?
The question, my answer, and the output is screenshotted here
Constraints:
0=no ticket
1=small ticket
2=big ticket.
If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result…

Caleb
- 11
- 1
1
vote
1 answer
Check Truthiness of Values in Pandas Series
In pandas, is it possible to construct a boolean series for indexing that use custom objects?
i.e.
class Test():
def __init__(self, num):
self.num = num
def __bool__(self):
return self.num == 3
x = Test(2)
y = Test(3)
df =…

ErstBlack
- 25
- 3
1
vote
1 answer
Testing truthiness in guards
I can use guards to test if an argument is true:
defmodule Truth do
def true?(term) when term, do: "#{term} is true"
def true?(term), do: "#{term} is not true"
end
This works as expected for boolean values:
Truth.true?(true)
#=> "true is…

Adam Millerchip
- 20,844
- 5
- 51
- 74
1
vote
2 answers
Evaluate truthiness in Java
I am writing an interactive console app in Java. I need to ask the user questions with Boolean responses, however, Scanner.in.nextBoolean() is rather brittle in that it throws exceptions on incorrect input, so wrapping it in a method seems like a…

Ron Jensen
- 641
- 7
- 20
1
vote
4 answers
Is there a way to clarify the true and false statements in this JS?
I've been working on learning JS, and I can't seem to figure out why my boolean values keep coming back either always true or always false.
So I believe I understand the basics of the truthy/falsy situation in JS, but I can't seem to get it right.…

Alexander Coffman
- 503
- 1
- 5
- 8
1
vote
1 answer
Use of IS outside of
I am not quite sure what this does.
Is this short hand for
Are there other truthy things that get set…

James A Mohler
- 11,060
- 15
- 46
- 72
1
vote
4 answers
Javascript Truthy / Falsy Operation
I have a question regarding javascript truthy / falsy
As far as I know, any non-zero number including negative numbers is truthy. But if this is the case then why
-1 == true //returns false
But also
-1 == false //returns false
Can someone shed…

Ovidiu
- 157
- 13
1
vote
1 answer
Can't understand validation logic: === vs ==
I'm learning validation stuff, and I just can't understand this:
if (strpos($value, "@") === false) { echo "Validation failed." }
What's the difference between === and ==? and why can't we use == instead and also why is it === false? does false…

ZaKi SB
- 37
- 3
1
vote
1 answer
Why does Python's bool builtin only look at the class-level __bool__ method
The documentation clearly states that
When this method (__bool__) is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its…

Mad Physicist
- 107,652
- 25
- 181
- 264
1
vote
2 answers
Is testing identity different than testing whether in a tuple?
I need to check if a function written by another team returns True or None.
I want to check for identity, not equality.
I'm unclear what type of check happens when using in. Which of the following does if result in (True, None): behave like?
if…

Jeff Widman
- 22,014
- 12
- 72
- 88
1
vote
6 answers
Return objects in array with 'true' parameters
I have a teamDetails array, within which is a squad array, within which are player objects. Each player object has an injured property which contains the value "true" or "false".
I want to write a function that loops through the array returning only…

Paulos3000
- 3,355
- 10
- 35
- 68
1
vote
2 answers
Python truthiness - Different behavior between different checks
I went through Truthiness in Python and understood that [] and similar empty objects are interpreted as False in Python.
But when I type in the following in REPL, it returns False:
>>> [] == False
False
How is this possible?

chaudharyp
- 3,394
- 3
- 26
- 42
1
vote
5 answers
Python Regex - find a pattern of string in a word
I'm getting false positives in python for the following example. I'm trying to find if a key word exist in a string. The problem is that the string has words connected by usually an underscore or hyphen so I only want positive result if the keyword…

MAXGEN
- 735
- 1
- 10
- 21
1
vote
1 answer
Why does this semi-colon cause a incorrect falsey result
Strange truth test results
filter = /rob/gi
>> /rob/gi
filter.test('hey')
>> false
filter.test('rob')
>> true
true && filter.test('rob');
>> false
true && filter.test('rob') ;
>> true
(true && filter.test('rob'));
>> false
(true &&…

Parris
- 17,833
- 17
- 90
- 133