Is there a way to instantiate a class without calling __init__?

You can circumvent __init__ by calling __new__ directly. Then you can create a object of the given type and call an alternative method for __init__. This is something that pickle would do. However, first I’d like to stress very much that it is something that you shouldn’t do and whatever you’re trying to achieve, there … Read more

How is an instance initializer different from a constructor?

This seems to explain it well: Instance initializers are a useful alternative to instance variable initializers whenever: initializer code must catch exceptions, or perform fancy calculations that can’t be expressed with an instance variable initializer. You could, of course, always write such code in constructors. But in a class that had multiple constructors, you would … Read more

Why is Class.newInstance() “evil”?

The Java API documentation explains why (http://java.sun.com/javase/6/docs/api/java/lang/Class.html#newInstance()): Note that this method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor … Read more

Creating instance of type without default constructor in C# using reflection

I originally posted this answer here, but here is a reprint since this isn’t the exact same question but has the same answer: FormatterServices.GetUninitializedObject() will create an instance without calling a constructor. I found this class by using Reflector and digging through some of the core .Net serialization classes. I tested it using the sample … Read more