What is the purpose of subclassing the class “object” in Python?

In short, it sets free magical ponies. In long, Python 2.2 and earlier used “old style classes”. They were a particular implementation of classes, and they had a few limitations (for example, you couldn’t subclass builtin types). The fix for this was to create a new style of class. But, doing this would involve some … Read more

Why does @foo.setter in Python not work for me?

You seem to be using classic old-style classes in python 2. In order for properties to work correctly you need to use new-style classes instead (in python 2 you must inherit from object). Just declare your class as MyClass(object): class testDec(object): @property def x(self): print ‘called getter’ return self._x @x.setter def x(self, value): print ‘called … Read more

Difference between type(obj) and obj.__class__

This is an old question, but none of the answers seems to mention that. in the general case, it IS possible for a new-style class to have different values for type(instance) and instance.__class__: class ClassA(object): def display(self): print(“ClassA”) class ClassB(object): __class__ = ClassA def display(self): print(“ClassB”) instance = ClassB() print(type(instance)) print(instance.__class__) instance.display() Output: <class ‘__main__.ClassB’> … Read more

What is the difference between old style and new style classes in Python?

From New-style and classic classes: Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type ‘instance’>. This reflects … Read more