Making immutable instances

bonono at gmail.com bonono at gmail.com
Wed Nov 30 21:05:29 EST 2005


Mike Meyer wrote:
> bonono at gmail.com writes:
> > I am puzzled, and could have read what you want wrong. Are you saying
> > you want something like this :
> >
> > a={}
> > a.something = "I want to hang my stuff here, outside the intended use
> > of dict"
>
> Exactly. For a use case, consider calling select.select on lists of
> file objects. If the processing is simple enough, the clearest way to
> associate a handler with each socket is just to add it as an
> attribute. But that doesn't work - sockets are bulitin types. So you
> consider less light-weight solutions, like subclassing socket (now
> that that's possible), or a dictionary of handlers keyed by socket.
>
> This works by default with classes written in Python. That it doesn't
> work for builtins is inconsistent, non-orthogonal, and
> incomplete. However, it's easy to work around, and the obvious fix -
> adding a dictionary to every builtin - is rather costly. So we'll live
> with it since practicality beats purity.
>
While I agree with the use case(I want it sometimes too), it seems that
the language creator may also deliberately disallow that, not because
it is not doable or costly. So how, the built-in types still need to
have some form of dictionary or else how would dir(a) of the above
dictionary work ?




More information about the Python-list mailing list