Dynamic object attribute creation

ast nomail at invalid.com
Mon Feb 29 10:36:47 EST 2016


Object's attributes can be created dynamically, ie

class MyClass:

obj = MyClass()
obj.test = 'foo'

but why doesn't it work with built-in classes int, float, list.... ?

L = [1, 8, 0]
L.test = 'its a list !'

(however lists are mutable, int, float ... are not)

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    L.test = 'its a list !'
AttributeError: 'list' object has no attribute 'test'

but it works on functions:

def funct(a,b):

funct.test = 'this is a function'

'this is a function'

More information about the Python-list mailing list