equality

Python testing whether a string is one of a certain set of values

a 夏天 提交于 2019-11-27 16:12:35
I'm learning python on codecademy and my current task is this: Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use s for string). The shut_down function should return "Shutting down..." when it gets "Yes" , "yes" , or "YES" as an argument, and "Shutdown aborted!" when it gets "No" , "no" , or "NO" . If it gets anything other than those inputs, the function should return "Sorry, I didn't understand you." Seemed easy to me but somehow I still can't do it. My code I made to test the function: def shut_down(s): if s == "Yes" or s == "yes" or

Should you use 'isEqual' or '=='?

こ雲淡風輕ζ 提交于 2019-11-27 15:44:46
I saw a couple of questions here on SO, with ansers including the function isEqual: instead of the standard == . So far, I have only learned to use the == , so I'm wondering what's better to use, what are the pros and cons of each? When should you use them? Thank you. They do different things; so you need to use the appropriate one: Consider, if you will: NSString *a = @"Hello!"; NSString *b = a; NSString *c = [a mutableCopy]; if (a == b) NSLog(@"This prints"); if (b == c) NSLog(@"This doesn't"); if ([a isEqual:c]) NSLog(@"This does"); In other words; == merely checks if two pointers point to

When is the `==` operator not equivalent to the `is` operator? (Python)

蹲街弑〆低调 提交于 2019-11-27 15:07:12
I noticed I can use the == operator to compare all the native data types (integers, strings, booleans, floating point numbers etc) and also lists, tuples, sets and dictionaries which contain native data types. In these cases the == operator checks if two objects are equal. But in some other cases (trying to compare instances of classes I created) the == operator just checks if the two variables reference the same object (so in these cases the == operator is equivalent to the is operator) My question is: When does the == operator do more than just comparing identities? EDIT: I'm using Python 3

C++ object equality

风格不统一 提交于 2019-11-27 15:05:55
问题 I have a class MyCloth and one one object instance of that class that I instantiated like this: MyCloth** cloth1; And at one point in the program, I will do something like this: MyCloth** cloth2 = cloth1; And then at some point later, I want to check to see if cloth1 and cloth2 are the same. (Something like object equality in Java, only here, MyCloth is a very complex class and I can''t build an isEqual function.) How can I do this equality check? I was thinking maybe checking if they point

Override == operator in Ruby

孤人 提交于 2019-11-27 14:33:50
问题 According to the docs, Array.include? uses the == comparison on objects. I come from Java where such things are (usually) done with .equals() which is easy to override for a particular object. How can I override == in Ruby to allow me to specify the behaviour of Array.include? for my particular object? Many thanks. 回答1: In Ruby == is just a method (with some syntax sugar on top allowing you to write foo == bar instead of foo.==(bar) ) and you override == just like you would any other method:

Can I define custom operator overloads in Javascript? [duplicate]

丶灬走出姿态 提交于 2019-11-27 13:56:06
This question already has an answer here: Javascript: operator overloading 4 answers Overloading Arithmetic Operators in JavaScript? 11 answers Is it possible to define custom operators between instances of a type in JavaScript? For example, given that I have a custom vector class, is it possible to use vect1 == vect2 to check for equality, whilst the underlying code would be something like this? operator ==(a, b) { return a.x == b.x && a.y == b.y && a.z == b.z; } (This is nonsense of course.) I agree that the equal function on the vector prototype is the best solution. Note that you can also

Testing for equality between dictionaries in c#

无人久伴 提交于 2019-11-27 12:33:09
Assuming dictionary keys and values have their equals and hash methods implemented correctly, what is the most succinct and efficient way to test for equality of two dictionaries? In this context two dictionaries are said to be equal if they contain the same set of keys (order not important), and for every such key, they agree on the value. here are some ways i came up with (there are probably many more): public bool Compare1<TKey, TValue>( Dictionary<TKey, TValue> dic1, Dictionary<TKey,TValue> dic2) { return dic1.OrderBy(x => x.Key). SequenceEqual(dic2.OrderBy(x => x.Key)); } public bool

JavaScript - === vs == operators performance

你。 提交于 2019-11-27 12:25:56
A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C . It was said that there is no difference in the performance between < and <= as they are interpreted as same/similar machine commands. At the same time, in our company's "best practices", it was said that we should always use "===" to compare things instead of "==". So, I started to wonder if this is always appropriate as I am used to using the "==" and "typeof ... == " and do not want to change my way of writing :-] Note that this is in the context of JavaScript. So, I have a little research and

Why does the “is” keyword have a different behavior when there is a dot in the string?

非 Y 不嫁゛ 提交于 2019-11-27 11:32:21
Consider this code: >>> x = "google" >>> x is "google" True >>> x = "google.com" >>> x is "google.com" False >>> Why is it like that? To make sure the above is correct, I have just tested on Python 2.5.4, 2.6.5, 2.7b2, Python 3.1 on windows and Python 2.7b1 on Linux. It looks like there is consistency across all of them, so it's by design. Am I missing something? I just find it out that from some of my personal domain filtering script failing with that. is verifies object identity, and any implementation of Python, when it meets literal of immutable types, is perfectly free to either make a

Why the compiler emits box instructions to compare instances of a reference type?

安稳与你 提交于 2019-11-27 10:46:12
问题 Here is a simple generic type with a unique generic parameter constrained to reference types: class A<T> where T : class { public bool F(T r1, T r2) { return r1 == r2; } } The generated IL by csc.exe is : ldarg.1 box !T ldarg.2 box !T ceq So each parameter is boxed before proceeding with the comparison. But if the constraint indicates that "T" should never be a value type, why is the compiler trying to box r1 and r2 ? 回答1: It's required to satisfy the verifiability constraints for the