Making immutable instances

bonono at gmail.com bonono at gmail.com
Wed Nov 30 23:11:58 EST 2005


Mike Meyer wrote:
> Built-in types don't have a real dictionary. They have a C struct that
> holds the various methods.  The entries in the struct are called
> "slots", hence the __slots__ magic attribute. That __slots__ makes it
> impossible to add an attribute is documented as an implementation
> detail. This makes me think that the same restriction on the builtin
> types is the same.
>
> FWIW, dir returns a list built from a number of source. But if you
> look at, for example, list.__dict__, you'll notice that it's not a
> dict.
>
Well, in this case, would it be simple for the OP that if he wants to
disallow this attaching additional things, just use __slot__.

What I wan to say though is, if we can live with the inability of not
able to attach to built-in types, why is it so difficult for other user
defined class ? If the authors go to the length of not allowing it, so
be it. They are afterall define it for their use and how someone else
will use it don't matter.




More information about the Python-list mailing list