Thoughts on new vs traditional idioms
Peter Otten
__peter__ at web.de
Tue Mar 2 12:13:44 EST 2004
Jeff Epler wrote:
> On Tue, Mar 02, 2004 at 03:48:19PM +0100, Peter Otten wrote:
>> Do you have a class with immutable instances handy where the following
>> assertion fails?
>>
>> assert immutableObj.__class__(immutableObj) is immutableObj
>
> Python 2.3.2 (#1, Oct 17 2003, 10:41:07)
>>>> import sets
>>>> ii = sets.ImmutableSet([1,2,3])
>>>> ii.__class__(ii) is ii
> False
You win :-)
Now I think about it (again), the above is the behaviour to be expected for
every immutable class (at least if it's implemented in Python, don't know
about C) that predates the advent of __new__().
By the way, is ImmutableSet meant to stay? In the 1.4 setobject.c code there
is a frozenset which _has_ the "do I really need to copy" check:
if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Py_INCREF(iterable);
return iterable;
}
Peter
More information about the Python-list
mailing list