Why can’t I subclass datetime.date?

Regarding several other answers, this doesn’t have anything to do with dates being implemented in C per se. The __init__ method does nothing because they are immutable objects, therefore the constructor (__new__) should do all the work. You would see the same behavior subclassing int, str, etc. >>> import datetime >>> class D(datetime.date): def __new__(cls, … Read more

Subclassing dict: should dict.__init__() be called?

You should probably call dict.__init__(self) when subclassing; in fact, you don’t know what’s happening precisely in dict (since it’s a builtin), and that might vary across versions and implementations. Not calling it may result in improper behaviour, since you can’t know where dict is holding its internal data structures. By the way, you didn’t tell … Read more

Subclass dict: UserDict, dict or ABC?

If you want a custom collection that actually holds the data, subclass dict. This is especially useful if you want to extend the interface (e.g., add methods). None of the built-in methods will call your custom __getitem__ / __setitem__, though. If you need total control over these, create a custom class that implements the collections.MutableMapping … Read more

How to check if a subclass is an instance of a class at runtime? [duplicate]

You have to read the API carefully for this methods. Sometimes you can get confused very easily. It is either: if (B.class.isInstance(view)) API says: Determines if the specified Object (the parameter) is assignment-compatible with the object represented by this Class (The class object you are calling the method at) or: if (B.class.isAssignableFrom(view.getClass())) API says: Determines … Read more