4

Why does this raise an error:

o = object() o.i = 1 

But this does not:

class A(object): pass a = A() a.i = 1 

?

2 Answers 2

9

Because built-in types don't have dictionaries associated with them to hold added attributes:

>>> o = object() >>> dir(o) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] 

See? No __dict__.

But adding a subclass gives the attribute somewhere to go:

>>> class A(object): .... pass .... >>> a = A() >>> dir(a) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] 

Saying that it's "because they're defined in C" isn't a "why". You could certainly define a type in C with an instance dictionary.

Sign up to request clarification or add additional context in comments.

Comments

2

object is defined in C. You cannot add arbitrary attributes to instances of types defined in C unless you fill the appropriate slots in the type definition.

1 Comment

This implies both that you couldn't define a type in C with an instance dictionary and that implementations not written in C won't have this limitation. Neither is true. "You can't" also isn't an answer to "why can't you".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.