equality

python equality precedence

我们两清 提交于 2019-12-01 14:59:46
class L(object): def __eq__(self, other): print 'invoked L.__eq__' return False class R(object): def __eq__(self, other): print 'invoked R.__eq__' return False left = L() right = R() With this code, left side gets the first shot at comparison, as documented in the data model: >>> left == right invoked L.__eq__ False But if we make a slight modification on line 6 (everything else the same): class R(L): Now the right side gets to have the first shot at comparison. >>> left == right invoked R.__eq__ False Why is that? Where is it documented, and what's the reason for the design decision? This is

.NET Dictionaries have same keys and values, but aren't “equal”

别来无恙 提交于 2019-12-01 14:38:32
问题 This test fails: using Microsoft.VisualStudio.TestTools.UnitTesting; [TestMethod()] public void dictEqualTest() { IDictionary<string, int> dict = new Dictionary<string, int>(); IDictionary<string, int> dictClone = new Dictionary<string, int>(); for (int x = 0; x < 3; x++) { dict[x.ToString()] = x; dictClone[x.ToString()] = x; } Assert.AreEqual(dict, dictClone); // fails here Assert.IsTrue(dict.Equals(dictClone)); // and here, if the first is commented out Assert.AreSame(dict, dictClone); //

Object and primitive type equality

只愿长相守 提交于 2019-12-01 13:08:33
I know that identical objects are not equal, i.e: var obj = { name: "Value" }; var obj2 = { name: "Value" }; console.log("obj equals obj2: " + (obj === obj2)); //evaluates to false Yet primitive types are: var str = "string1"; var str2 = "string1"; console.log("str equals str2: " + (str === str2)); //evaluates to true My question is why. Why are objects and primitives treated differently? If an object is nothing but an empty container, with only the attributes you specify to put in the container, why wouldn't the container's identical attributes evaluate to be the same? I looked around for

How do you create a dynamic equality implementation where you can pass in the property names to be compared?

若如初见. 提交于 2019-12-01 11:14:10
Say I have an object Person with the properties below: public class Person { public int ID { get; set; } public int EmployeeNo { get; set; } public string JobDescription { get; set; } public string Code { get; set; } } How would I dynamically check the equality of specific properties by name? eg. var dynamicEqualityComparer = RetrieveDynamicEqualityComparer("ID", "JobDescription"); var intersectedPersons = listOfPerson1.Intersect(listOfPerson2, dynamicEqualityComparer); The above snippit would use the default linq intersect method using the dynamically generated equality comparison method

How to check class equality in Python 2.5?

余生颓废 提交于 2019-12-01 10:41:22
问题 I've looked through Python 2.5 documentation and I couldn't find an answer to this: How do I check if an object is the same class as another object? def IsClass(obj1, obj2): return obj1.class == obj2.class #doesn't work 回答1: You can use type(obj1) is type(obj2) Note that you usually try to avoid type checking in Python, but rather rely on duck typing. 回答2: I think what you want to do is use type(obj). :) -EDIT- Looks like he beat me to it. And he's right about the Duck Typing. 来源: https:/

Object and primitive type equality

喜夏-厌秋 提交于 2019-12-01 10:14:47
问题 I know that identical objects are not equal, i.e: var obj = { name: "Value" }; var obj2 = { name: "Value" }; console.log("obj equals obj2: " + (obj === obj2)); //evaluates to false Yet primitive types are: var str = "string1"; var str2 = "string1"; console.log("str equals str2: " + (str === str2)); //evaluates to true My question is why. Why are objects and primitives treated differently? If an object is nothing but an empty container, with only the attributes you specify to put in the

How do you create a dynamic equality implementation where you can pass in the property names to be compared?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 09:39:18
问题 Say I have an object Person with the properties below: public class Person { public int ID { get; set; } public int EmployeeNo { get; set; } public string JobDescription { get; set; } public string Code { get; set; } } How would I dynamically check the equality of specific properties by name? eg. var dynamicEqualityComparer = RetrieveDynamicEqualityComparer("ID", "JobDescription"); var intersectedPersons = listOfPerson1.Intersect(listOfPerson2, dynamicEqualityComparer); The above snippit

How to check if a variable is the same as at least one of two other variables? [duplicate]

泪湿孤枕 提交于 2019-12-01 09:27:09
This question already has an answer here: How to test multiple variables against a value? 23 answers I have a variable, and want to check if it matches at least one of the other two variables. Clearly I can do: if a == b or a == c: But I want to know if there is any shorter way, something like: if a == (b or c): How to test if a variable is the same as - at least - one of the others? sshashank124 For that use in : if a in (b, c): Testing for membership in a tuple has an average case of O(n) time complexity. If you have a large collection of values and are performing many membership tests on

Check two object, of unknown type, for equality, comparing all their fields

百般思念 提交于 2019-12-01 08:33:53
I need to define a method to compare two different objects of a same type. The type of objects is not specific. The objects may be a DLL type, so I can't override Equals method. I have to do this by reflection. This code works if all the members of objects are of primitive type. But it doesn't work when an object has a field that isn't primitive. How can I do it by reflection? public bool Equals(object obj1, object obj2) { List<FieldInfo> fieldInfos = obj1.GetType().GetFields().ToList(); return (fieldInfos.Select(fieldInfo => new {fieldInfo, type = fieldInfo.GetType()}) .Where(@t => @t.type

Check two object, of unknown type, for equality, comparing all their fields

半城伤御伤魂 提交于 2019-12-01 07:52:48
问题 I need to define a method to compare two different objects of a same type. The type of objects is not specific. The objects may be a DLL type, so I can't override Equals method. I have to do this by reflection. This code works if all the members of objects are of primitive type. But it doesn't work when an object has a field that isn't primitive. How can I do it by reflection? public bool Equals(object obj1, object obj2) { List<FieldInfo> fieldInfos = obj1.GetType().GetFields().ToList();