In Python 2.7, I can declare a new-style class and set an attribute using object.__setattr__
:
class A (object):
def __init__ (self):
object.__setattr__(self, 'foo', 'bar')
A().foo # 'bar'
However, this does not work in an old-style class:
class B:
def __init__ (self):
object.__setattr__(self, 'foo', 'bar')
B().foo
TypeError: can't apply this __setattr__ to instance object
I know that old-style classes do not support this behaviour, but I do not know why it doesn't work.
What is going on under the hood when I call object.__setattr__
on a new-style class that cannot occur for old-style classes?
Please note that I am not using this code for anything, I am just trying to understand it.