class method generates “TypeError: … got multiple values for keyword argument …”

The problem is that the first argument passed to class methods in python is always a copy of the class instance on which the method is called, typically labelled self. If the class is declared thus: class foo(object): def foodo(self, thing=None, thong=’not underwear’): print thing if thing else “nothing” print ‘a thong is’,thong it behaves … Read more

Java synchronized static methods: lock on object or class

Just to add a little detail to Oscar’s (pleasingly succinct!) answer, the relevant section on the Java Language Specification is 8.4.3.6, ‘synchronized Methods’: A synchronized method acquires a monitor (ยง17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method’s class is used. For an instance method, … Read more

Is it wrong to use Deprecated methods or classes in Java?

1. Is it wrong to use Deprecated methods or classes in Java? From the definition of deprecated: A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. The method is kept in the API for backward compatibility for an unspecified period … Read more

Passing just a type as a parameter in C#

There are two common approaches. First, you can pass System.Type object GetColumnValue(string columnName, Type type) { // Here, you can check specific types, as needed: if (type == typeof(int)) { // … This would be called like: int val = (int)GetColumnValue(columnName, typeof(int)); The other option would be to use generics: T GetColumnValue<T>(string columnName) { // … Read more

What does ‘public static void’ mean in Java?

It’s three completely different things: public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details. static means that the method is associated with the class, not a specific instance (object) of that class. This means … Read more

Why does the Scala compiler disallow overloaded methods with default arguments?

I’d like to cite Lukas Rytz (from here): The reason is that we wanted a deterministic naming-scheme for the generated methods which return default arguments. If you write def f(a: Int = 1) the compiler generates def f$default$1 = 1 If you have two overloads with defaults on the same parameter position, we would need … Read more

Properties vs Methods

From the Choosing Between Properties and Methods section of Design Guidelines for Developing Class Libraries: In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a … Read more