@staticmethod vs @classmethod in Python

Maybe a bit of example code will help: Notice the difference in the call signatures of foo, class_foo and static_foo: class A(object): def foo(self, x): print(f”executing foo({self}, {x})”) @classmethod def class_foo(cls, x): print(f”executing class_foo({cls}, {x})”) @staticmethod def static_foo(x): print(f”executing static_foo({x})”) a = A() Below is the usual way an object instance calls a method. The … Read more

Difference between methods and attributes in python

Terminology Mental model: A variable stored in an instance or class is called an attribute. A function stored in an instance or class is called a method. According to Python’s glossary: attribute: A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute … Read more

C++ using scoped_ptr as a member variable

scoped_ptr is very good for this purpose. But one has to understand its semantics. You can group smart pointers using two major properties: Copyable: A smart pointer can be copied: The copy and the original share ownership. Movable: A smart pointer can be moved: The move-result will have ownership, the original won’t own anymore. That’s … Read more

Speed of C# lists

List<T> uses a backing array to hold items: Indexer access (i.e. fetch/update) is O(1) Remove from tail is O(1) Remove from elsewhere requires existing items to be shifted up, so O(n) effectively Add to end is O(1) unless it requires resizing, in which case it’s O(n). (This doubles the size of the buffer, so the … Read more