equality

How do I check if two variables reference the same object in Python?

徘徊边缘 提交于 2019-11-30 05:36:12
x and y are two variables. I can check if they're equal using x == y , but how can I check if they have the same identity? Example: x = [1, 2, 3] y = [1, 2, 3] Now x == y is True because x and y are equal, however, x and y aren't the same object. I'm looking for something like sameObject(x, y) which in that case is supposed to be False. You can use is to check if two objects have the same identity. >>> x = [1, 2, 3] >>> y = [1, 2, 3] >>> x == y True >>> x is y False To build on the answer from Mark Byers: The is evaluation to distinguish identities will work when the variables contain objects

Relational operations using only increment, loop, assign, zero

守給你的承諾、 提交于 2019-11-30 05:14:08
问题 This is a follow up question for: Subtraction operation using only increment, loop, assign, zero We're only allowed to use the following operations: incr(x) - Once this function is called it will assign x + 1 to x assign(x, y) - This function will assign the value of y to x (x = y) zero(x) - This function will assign 0 to x (x = 0) loop X { } - operations written within brackets will be executed X times For example, addition can be implemented as follows: add(x, y) { loop x { y = incr(y) }

Why isn't (“Maya” == “Maya”) true in C++?

≯℡__Kan透↙ 提交于 2019-11-30 04:53:44
Any idea why I get "Maya is not Maya" as a result of this code? if ("Maya" == "Maya") printf("Maya is Maya \n"); else printf("Maya is not Maya \n"); Georg Fritzsche Because you are actually comparing two pointers - use e.g. one of the following instead: if (std::string("Maya") == "Maya") { /* ... */ } if (std::strcmp("Maya", "Maya") == 0) { /* ... */ } This is because C++03, §2.13.4 says: An ordinary string literal has type “array of n const char ” ... and in your case a conversion to pointer applies. See also this question on why you can't provide an overload for == for this case. You are not

How do I check the equality of three values elegantly?

偶尔善良 提交于 2019-11-30 04:50:39
问题 Say I have values a , b and c . I want to find out if they are equal. If I do if a == b == c{...} Then I get a compile error invalid operation: a == b == c (mismatched types bool and TypeOfABandC) This is pretty obvious, because this parses to: (a == b) == c And (a == b) is a bool. Of course I can do: if a == b && a == c {...} However, this isn't very nice looking and feels confusing. Is there another way? 回答1: A note beforehand: Your last proposed solution is the shortest, clearest and most

Why does new String('hello') === new String('hello') evaluate to False? [duplicate]

£可爱£侵袭症+ 提交于 2019-11-30 04:39:09
This question already has an answer here: Which equals operator (== vs ===) should be used in JavaScript comparisons? 49 answers JavaScript comparison operators: Identity vs. Equality 4 answers Why does the following statement return false in JavaScript? new String('hello') === new String('hello') Pointy Two String objects will always be unequal to each other. Note that JavaScript has string primitive values as well as a String constructor to create wrapper objects. All object equality comparisons (especially with === ) are carried out as a test for reference equality . References to two

Equality relations in Scala

a 夏天 提交于 2019-11-30 03:51:53
问题 I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world

Why is == faster than eql?

北慕城南 提交于 2019-11-30 03:22:43
I read in the documentation for the String class that eql? is a strict equality operator, without type conversion, and == is a equality operator which tries to convert second its argument to a String, and, the C source code for this methods confirms that: The eql? source code: static VALUE rb_str_eql(VALUE str1, VALUE str2) { if (str1 == str2) return Qtrue; if (TYPE(str2) != T_STRING) return Qfalse; return str_eql(str1, str2); } The == source code: VALUE rb_str_equal(VALUE str1, VALUE str2) { if (str1 == str2) return Qtrue; if (TYPE(str2) != T_STRING) { if (!rb_respond_to(str2, rb_intern("to

Why is (18446744073709551615 == -1) true?

懵懂的女人 提交于 2019-11-30 01:40:27
问题 When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How can it be possible? Is it because of binary conversation? 回答1: 18,446,744,073,709,551,615 This number mentioned, 18,446,744,073,709,551,615 , is actually 2^64 − 1 . The important thing here is that 2^64-1 is essentially 0-based 2^64 . The first digit of

Equality between two enumerables

若如初见. 提交于 2019-11-30 00:22:59
问题 I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true. As a side question, the code below to compare each element works, but there must be a more elegant way var other = (ActivityService) obj; if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false; for (int i = 0; i < AllAccounts.Count(); i++) { if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) { return false; } } return true; 回答1: Have a look at the

== for pointer comparison

浪尽此生 提交于 2019-11-30 00:13:46
问题 I quote from "The C Programming Language" by Kernighan & Ritchie: Any pointer can be meaningfully compared for equality or inequality with zero. But the behavior is undefined for arithmetic or comparisons with pointers that do not point to members of the same array. (There is one exception: the address of the first element past the end of an array can be used in pointer arithmetic.) Does this mean I cannot rely on == for checking equality of different pointers? What are the situations in