__dict__ attribute for built-in types
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Oct 28 02:21:44 EDT 2011
On Fri, 28 Oct 2011 01:36:41 +0200, candide wrote:
> Le 28/10/2011 00:57, Hrvoje Niksic a écrit :
>
>> was used at class definition time to suppress it. Built-in and
>> extension types can choose whether to implement __dict__.
>>
>>
> Is it possible in the CPython implementation to write something like
> this :
>
> "foo".bar = 42
>
> without raising an attribute error ?
No, because built-in strings don't have a __dict__ and so you cannot add
new attributes that didn't already exist.
But you can subclass str and do whatever you like.
class Str(str):
pass
Str("foo").bar = 42
--
Steven
More information about the Python-list
mailing list