Why does this raise an error:
o = object()
o.i = 1
But this does not:
class A(object):
pass
a = A()
a.i = 1
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because built-in types don’t have dictionaries associated with them to hold added attributes:
See? No
__dict__.But adding a subclass gives the attribute somewhere to go:
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.