can't add variables to instances of built-in classes

Kent Tong kent.tong.mo at gmail.com
Sun Jul 17 06:57:01 EDT 2016


Hi,

I can add new variables to user-defined classes like:

>>> class Test:
...     pass
... 
>>> a=Test()
>>> a.x=100

but it doesn't work if the instances belong to a built-in class such as str or list:

>>> a='abc'
>>> a.x=100
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 AttributeError: 'str' object has no attribute 'x'

What makes this difference?

Thanks in advance!



More information about the Python-list mailing list