truthiness

How to inherit from NilClass or how to simulate similar function

*爱你&永不变心* 提交于 2019-11-30 15:58:41
I just want to use Null Object Design Pattern, but I found I can inherit from NilClass. I can write a method "nil?" and return false but what if user write code below if null_object puts "shouldn't be here" end For clarify what I try to do is: record = DB.find(1) # if it can not find record 1, the bellow code should not raise exception record.one_attr # and what's more if record puts "shouldn't be here" end # I don't want to override all NilClass An approach that may work for you is to overide the method #nil? in your Null object. This means that in your code to test for null you have to use

How to inherit from NilClass or how to simulate similar function

橙三吉。 提交于 2019-11-29 22:07:51
问题 I just want to use Null Object Design Pattern, but I found I can inherit from NilClass. I can write a method "nil?" and return false but what if user write code below if null_object puts "shouldn't be here" end For clarify what I try to do is: record = DB.find(1) # if it can not find record 1, the bellow code should not raise exception record.one_attr # and what's more if record puts "shouldn't be here" end # I don't want to override all NilClass 回答1: An approach that may work for you is to

Why does Python's bool builtin only look at the class-level __bool__ method [duplicate]

这一生的挚爱 提交于 2019-11-29 18:16:50
This question already has an answer here: Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate] 2 answers 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 instances are considered true. Bold is my insertion, italics is mine but the text is actually there. The fact that the class must contain the method is readily tested by class A: pass a = A() a.__bool__ = (lamda self

Understanding JavaScript hoisting and truthy & falsy

邮差的信 提交于 2019-11-28 12:31:46
I've been reading about JavaScript hoisting sometime back. JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting” by Dmitry Soshnikov and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and JavaScript and some other resource And while practicing some, and found I m missing some important concept about the hoisting and a variable' truthy & falsy. 1: 'if' truth test with duplicate variable declaration var foo = 1; function bar() { if (!foo) { alert('inside if'); var foo = 10; } } bar(); o/p: inside if Doubt: 'foo' value being '1', if(!foo) should

Why does Python's bool builtin only look at the class-level __bool__ method [duplicate]

耗尽温柔 提交于 2019-11-28 11:59:08
问题 This question already has an answer here: Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate] 2 answers 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 instances are considered true. Bold is my insertion, italics is mine but the text is actually there. The fact

What are the true and false criteria for a python object?

痞子三分冷 提交于 2019-11-28 09:09:13
问题 I have seen the following cases: >>> def func(a): ... if a: ... print("True") ... >>> a = [1, 2, 3] >>> func(a) True >>> a == True False Why does this difference occur? 回答1: All objects 1 in Python have a truth value: Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: None False zero of any numeric type, for example, 0 , 0.0 , 0j . any empty sequence, for example, '' , () , []

Why does {} == false throw an exception?

◇◆丶佛笑我妖孽 提交于 2019-11-27 22:45:47
In IE and Chrome, typing this into the JavaScript console throws an exception: {} == false // "SyntaxError: Unexpected token ==" However, all of these statements are evaluated with no problem: false == {} // false ({} == false) // false var a = {}; a == false // false Is this intentional behavior? Why does this happen? In the console, when you start a statement with {} , you are not creating an object literal, but a code block (i.e. the same block as you would make with an if statement or a loop body). A symbol like == is then obviously not expected afterwards. If you think of a code block,

How does (A == B == C) comparison work in JavaScript?

匆匆过客 提交于 2019-11-27 15:38:47
I was expecting the following comparison to give an error: var A = B = 0; if(A == B == 0) console.log(true); else console.log(false); but strangely it returns false . Even more strangely, console.log((A == B == 1)); returns true . How does this "ternary" kind of comparison work? First, we need to understand that a == comparison between a number and a boolean value will result in internal type conversion of Boolean value to a number ( true becomes 1 and false becomes 0 ) The expression you have shown is evaluated from left to right. So, first A == B is evaluated and the result is true and you

How does (A == B == C) comparison work in JavaScript?

别等时光非礼了梦想. 提交于 2019-11-26 17:14:12
问题 I was expecting the following comparison to give an error: var A = B = 0; if(A == B == 0) console.log(true); else console.log(false); but strangely it returns false . Even more strangely, console.log((A == B == 1)); returns true . How does this "ternary" kind of comparison work? 回答1: First, we need to understand that a == comparison between a number and a boolean value will result in internal type conversion of Boolean value to a number ( true becomes 1 and false becomes 0 ) The expression