Possible to tack on random stuff to objects?
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Feb 12 04:23:56 EST 2008
Cruxic a écrit :
> Is it possible to tack on arbitrary attributes to a python object?
Depends on the object's class. In the common case it's possible but
there are a couple cases - mostly builtin immutable types - where it's not.
> For example:
>
> s = 'nice 2 meet you'
> s.isFriendly = True
>
> In the above example Python complains on the second line with:
>
> AttributeError: 'str' object has no attribute 'isFriendly'
>
> Is there another way?
>>> class MyString(str): pass
...
>>> s = MyString("hello")
>>> s.is_friendly = True
>>> s
'hello'
>>>
More information about the Python-list
mailing list