Making immutable instances

Mike Meyer mwm at mired.org
Sat Nov 26 04:59:59 EST 2005


Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> On Fri, 25 Nov 2005 23:20:05 -0500, Mike Meyer wrote:
>> If you've got a use case, I'd be interested in hearing it.
> frozenset perhaps? If it were needed once, it could be needed again.

That's not a use case, that's an example. And not a very good one, as
it's not at all clear that the restriction is intentional in that
case. After all, the same restriction applies to every builtin type,
including the mutable version of frozenset.

> The obvious case would be for a class where distinct instances that
> compare equal but not identical map to the same value in a dict.

How does the ability to add attributes to the instances of the class
change that behavior?

> In any case, I'm not the one claiming that I need custom immutable
> classes. I'm just suggesting that there is nothing non-Pythonic about
> them. If Ben thinks he needs them, I'm sure he has put *far* more thought
> into it than I have. I know Ben in RL, and he is not someone to make snap
> judgements about turning Python into Some Other Language Just Because.

I claim that the dynamic natture of Python - which is exemplified by
things like duck typing and the ability to add attributes to nearly
everything on the fly - is a fundamental part of what makes Python
Python. The best reason Ben could come up with is that it makes
finding bugs a bit easier. But so do type declarations, static
namespaces, private and protected attributes, and a slew of similar
B&D features that are pretty much anathema to dynamic languages. This
feature fits *very* well in languages that have those features, and
poorly in languages that reject them, which includes Python.

Of course, that a feature has a lot in common with features from
un-Pythonic languages doesn't make it ipso facto unPythonic. After
all, practicality beats purity. So what's the practical application
for such a feature? What's the use case?

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list