Field initialization

You cannot make use of the this keyword when initializing fields inline. The reason for this is the order in which the code is executed: for all intents and purposes, the code to initialize a field inline is run before the constructor for the class (i.e. the C# compiler will prevent access to the this … Read more

Scala: Using HashMap with a default value

Wow, I happened to visit this thread exactly one year after I posted my last answer here. 🙂 Scala 2.9.1. mutable.Map comes with a withDefaultValue method. REPL session: scala> import collection.mutable import collection.mutable scala> mutable.Map[Int, String]().withDefaultValue(“”) res18: scala.collection.mutable.Map[Int,String] = Map() scala> res18(3) res19: String = “”

How can I prevent a base constructor from being called by an inheritor in C#?

There is a way to create an object without calling any instance constructors. Before you proceed, be very sure you want to do it this way. 99% of the time this is the wrong solution. This is how you do it: FormatterServices.GetUninitializedObject(typeof(MyClass)); Call it in place of the object’s constructor. It will create and return … Read more

VBA takes wrong branch at If-statement – severe compiler bug?

This bug is not present on 32-bit, but it seems to be present in 64-bit VBA-capable applications (I’ve tried Excel, Word, and AutoCAD). Since the question already covers what happens if the Object does not get terminated or if there is no Class_Terminate event, the following examples all use an Object that will surely go … Read more

How to make an Abstract Class inherit from another Abstract Class in Python?

Have a look at abc module. For 2.7: link. For 3.6: link Simple example for you: from abc import ABC, abstractmethod class A(ABC): def __init__(self, value): self.value = value super().__init__() @abstractmethod def do_something(self): pass class B(A): @abstractmethod def do_something_else(self): pass class C(B): def do_something(self): pass def do_something_else(self): pass

Terminology of Class “attribute” vs “member” vs “variable” vs “field” [closed]

Based on the variety of answers, Class “attributes”, “fields”, and “variables” are used relatively interchangeably but have nuanced distinctions that vary from person to person. As such, probably best to lump them together and not rely on the nuances. There’s consensus that a Class “member” includes methods as well as data, so it is distinct … Read more

Python unitest – Use variables defined in module and class level setup functions, in tests

For the string variable a, the only solution is global a. If you look at the Python 2 and Python 3 source code, setupModule() doesn’t appear to do anything magical, so all the usual namespace rules apply. If a were a mutable variable, like a list, you could define it at global scope and then … Read more

Python equivalent of Ruby’s ‘method_missing’

There is no difference in Python between properties and methods. A method is just a property, whose type is just instancemethod, that happens to be callable (supports __call__). If you want to implement this, your __getattr__ method should return a function (a lambda or a regular def, whatever suite your needs) and maybe check something … Read more