operator-keyword

How efficient/fast is Python's 'in'? (Time Complexity wise)

戏子无情 提交于 2019-11-28 04:06:58
In Python, what is the efficiency of the in keyword, such as in: a = [1, 2, 3] if 4 in a: ... It depends on the right hand operand : The operators in and not in test for collection membership. [...] The collection membership test has traditionally been bound to sequences; an object is a member of a collection if the collection is a sequence and contains an element equal to that object. However, it make sense for many other object types to support membership tests without being a sequence. In particular, dictionaries (for keys) and sets support membership testing. Classes can implement the

Commutative operator overloading + of 2 different objects

青春壹個敷衍的年華 提交于 2019-11-28 03:50:40
问题 I have 2 classes which represent a matrix: 1. RegularMatrix - O(n^2) representation 2. SparseMatrix - a matrix that is represented as linked list (without zeros). lets say i have: RegularMatrix a; SparseMatrix b; i want to be able to do: a+b; and also: b+a; so i'm overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case? RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &);

Set “in” operator: uses equality or identity?

若如初见. 提交于 2019-11-28 01:47:15
class A(object): def __cmp__(self): print '__cmp__' return object.__cmp__(self) def __eq__(self, rhs): print '__eq__' return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2]) Why does first line prints True, but second prints False? And neither enters operator eq ? I am using Python 2.6 You need to define __hash__ too. For example class A(object): def __hash__(self): print '__hash__' return 42 def __cmp__(self, other): print '__cmp__' return object.__cmp__(self, other) def __eq__(self, rhs): print '__eq__' return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2

decltype and the scope operator in C++

耗尽温柔 提交于 2019-11-28 00:44:39
I need to obtain the type which was supplied when instantiating a template. Consider the following example: template <typename T> struct Foo { typedef T TUnderlying; }; static Foo<int> FooInt; class Bar { public: auto Automatic() -> decltype(FooInt)::TUnderlying { return decltype(FooInt)::TUnderlying(); } }; int main() { Bar bar; auto v = bar.Automatic(); return 0; } Problem with this code is using the scope operator together with decltype. Visual C++ 2010 complains like this: error C2039: 'TUnderlying' : is not a member of '`global namespace'' I gathered some information on the topic on

Prolog GNU - Univ operator? Explanation of it

本秂侑毒 提交于 2019-11-27 21:26:58
So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This looks to see if another predicate is true. I don't understand what the ".." does. How would you rewrite this without the univ operator? Univ ( =.. ) breaks up a term into a list of constituents, or constructs a term from such a list. Try: ?- f(x,y) =.. L. L = [f, x, y]. ?- f(x,y,z) =.. [f|Args]. Args = [x, y, z]. ?- Term =.. [g,x,y]. Term = g(x, y).

What do >> and << mean in Python?

寵の児 提交于 2019-11-27 18:43:21
I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250. Also I can use >> in print : print >>obj, "Hello world" What is happening here? These are bitwise shift operators. Quoting from the docs : x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y . x >> y Returns x with the bits shifted to the right by y places. This is the same as dividing x by 2**y . I think it is important question and it is not answered yet (the OP seems to already know about shift operators). Let me

What does the operator |= do in JavaScript?

南笙酒味 提交于 2019-11-27 18:29:11
I found the following code in a JS project: var a = new Array(); a[0] = 0; for (var b = 0; b < 10; b++) { a[0] |= b; } What does the |= do in the body of the for loop? The code example is dubious, but has been presented here by V8 for an example of improved performance. Updated Example The above example is equivalent to var a = [15]; for most intents and purposes. A more realistic example for the |= operator would be to set up binary flags in a single variable, for example on a permission object: //Set up permission masks var PERMISSION_1_MASK = parseInt('0001',2); var PERMISSION_2_MASK =

Overriding == operator. How to compare to null? [duplicate]

被刻印的时光 ゝ 提交于 2019-11-27 17:25:10
Possible Duplicate: How do I check for nulls in an ‘==’ operator overload without infinite recursion? There is probably an easy answer to this...but it seems to be eluding me. Here is a simplified example: public class Person { public string SocialSecurityNumber; public string FirstName; public string LastName; } Let's say that for this particular application, it is valid to say that if the social security numbers match, and both names match, then we are referring to the same "person". public override bool Equals(object Obj) { Person other = (Person)Obj; return (this.SocialSecurityNumber ==

+ operator before expression in javascript: what does it do?

廉价感情. 提交于 2019-11-27 15:32:06
I was perusing the underscore.js library and I found something I haven't come across before: if (obj.length === +obj.length) { ... } What is that + operator doing there? For context, here is a direct link to that part of the file. The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number. According to MDN : The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already.

Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator [duplicate]

混江龙づ霸主 提交于 2019-11-27 14:54:57
This question already has an answer here: Does Python have a built in function for string natural sort? 16 answers I have data rows and wish to have them presented as follows: 1 1a 1a2 2 3 9 9.9 10 10a 11 100 100ab ab aB AB As I am using pyQt and code is contained within a TreeWidgetItem, the code I'm trying to solve is: def __lt__(self, otherItem): column = self.treeWidget().sortColumn() #return self.text(column).toLower() < otherItem.text(column).toLower() orig = str(self.text(column).toLower()).rjust(20, "0") other = str(otherItem.text(column).toLower()).rjust(20, "0") return orig < other