truthiness

Testing Truthy or Falsy arguments passed through a function into an if statement

蹲街弑〆低调 提交于 2019-12-10 20:13:30
问题 I am drawing a blank on this one too. Rather than provide an answer, I would appreciate if someone could help me understand why my code is not printing the expected output: def bool_to_str(bval): if bval is True: mytest = 'Yes' else: mytest = 'No' return mytest Expected output: >>>bool_to_str([1, 2, 3]) 'Yes' >>>bool_to_str(abcdef) 'Yes' What's actually output: >>>bool_to_str([1, 2, 3]) 'No' >>>bool_to_str(abcdef) 'No' Please help me to understand what I did wrong. I think that the function

Truth value of numpy array with one falsey element seems to depend on dtype

左心房为你撑大大i 提交于 2019-12-06 16:51:16
问题 import numpy as np a = np.array([0]) b = np.array([None]) c = np.array(['']) d = np.array([' ']) Why should we have this inconsistency: >>> bool(a) False >>> bool(b) False >>> bool(c) True >>> bool(d) False 回答1: For arrays with one element, the array's truth value is determined by the truth value of that element. The main point to make is that np.array(['']) is not an array containing one empty Python string. This array is created to hold strings of exactly one byte each and NumPy pads

Truth value of numpy array with one falsey element seems to depend on dtype

爱⌒轻易说出口 提交于 2019-12-04 22:28:06
import numpy as np a = np.array([0]) b = np.array([None]) c = np.array(['']) d = np.array([' ']) Why should we have this inconsistency: >>> bool(a) False >>> bool(b) False >>> bool(c) True >>> bool(d) False Alex Riley For arrays with one element, the array's truth value is determined by the truth value of that element. The main point to make is that np.array(['']) is not an array containing one empty Python string. This array is created to hold strings of exactly one byte each and NumPy pads strings that are too short with the null character. This means that the array is equal to np.array(['\0

In ruby, is truthiness idiomatic for a method name ending with a question mark?

可紊 提交于 2019-12-04 17:41:55
问题 Is it normal for methods with a question mark to return something that's truthy (for example, a number) to indicate that something is true, or should true itself be returned? Are there any examples of truthiness being used in the Ruby standard library or by Rails, for example? Background: Someone wrote a String#int? method in an answer to a separate question, which returned an integer to represent true, and nil to represent false. Another user was surprised at not returning a boolean. 回答1: It

Why does truth && “string” return “string”

旧时模样 提交于 2019-12-04 06:07:34
Let's say I have a something like true && true #=> true Which makes sense, so I try something like: true && "dsfdsf" #=> "dsfdsf" Which surprises me because often times I'll do something like if something && something and I always thought that that was evaluating to true and would return true. Further experimentation doing things like: jruby-1.7.3 :009 > "ad" && "dsf" => "dsf" jruby-1.7.3 :010 > "ad" && "sdfd" && nil => nil jruby-1.7.3 :011 > "ad" && nil && "sdf" => nil makes it seem like Ruby returns either the last value if all are true or the first false value it finds. Why does it do this?

If ([] == false) is true, why does ([] || true) result in []?

与世无争的帅哥 提交于 2019-12-04 04:21:26
问题 Was just doing some testing and I find this odd: [] == false Gives true, this makes sense because double equal only compares contents and not type and tries to do type-coercion. But if its comparing contents and returns true, that means [ ] is falsey (if you did [] == true you get false too), which means: [] || false Should give false, but it gives [ ], making it truthy? Why? Another example: "\n " == 0 Gives true, but "\n " || false gives "\n " ? Is there an explanation for this or its just

Why does … == True return False in Python 3?

╄→гoц情女王★ 提交于 2019-12-03 06:16:16
I am learning python, but I'm a bit confused by the following result. In [41]: 1 == True Out[41]: True In [42]: if(1): ...: print('111') ...: 111 In [43]: ... == True Out[43]: False <===== why this is False while '1 == True' is True in previous sample In [44]: if (...): <==== here ... just behaves like True ...: print('...') ...: ... According to the documentation , ... has a truth value of True. But I still feel the above code a bit inconsistent. ...And something more interesting: In [48]: 2==True Out[48]: False <===== why 1==True returns True while 2==True returns False? In [49]: if(2): ...:

if(negetive number) is true? Is something wrong with js?

Deadly 提交于 2019-12-02 04:02:48
Is something wrong with js? if("hello".indexOf("world")) { // I forgot to add > -1 here console.log("hello world"); } Basically if(-1) is true. How is this possible? It took me a whole day to fix this. Is there a list available where these kind of things are listed? Or tools available to catch things like these. As per ECMA 5.1 Standard Specifications , the following table is used to determine the truthyness of an expression +-----------------------------------------------------------------------+ | Argument Type | Result | |:--------------|-----------------------------------------------------

If ([] == false) is true, why does ([] || true) result in []?

蓝咒 提交于 2019-12-01 22:43:17
Was just doing some testing and I find this odd: [] == false Gives true, this makes sense because double equal only compares contents and not type and tries to do type-coercion. But if its comparing contents and returns true, that means [ ] is falsey (if you did [] == true you get false too), which means: [] || false Should give false, but it gives [ ], making it truthy? Why? Another example: "\n " == 0 Gives true, but "\n " || false gives "\n " ? Is there an explanation for this or its just an oddity. When I tried this in C, we get: int x = "\n " == 0; printf("%d\n", x); int y = "\n " || 0;

Why is 'false' truthy in javascript?

空扰寡人 提交于 2019-12-01 19:00:24
问题 I understand that an empty string is falsy in javascript and a not-empty string is truthy in javascript. However, why is 'false' truthy in javascript, is there anything explicit in the specification? Is it a performance issue or are there situations where you would want the string 'false' to represent true ? 回答1: Replying to the last part of your question: Are there situations where you would want the string 'false' to represent true? Let's consider I am testing for empty strings in my user