Usually, we define classes for this.
class XClass( object ):
def __init__( self ):
x.myAttr= None
x= XClass()
x.myAttr= 'magic'
x.MyAttr
However, you can, to an extent, do this with the setattr
and getattr
built-in functions. However, they don't work on instances of object
directly.
>>> a= object()
>>> setattr( a, 'hi', 'mom' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'hi'
They do, however, work on all kinds of simple classes.
class YClass( object ):
pass
y= YClass()
setattr( y, 'myAttr', 'magic' )
y.myAttr