Inheriting frozenset gives bug if i overwrite __repr__ method

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Nov 19 11:23:42 EST 2008


En Wed, 19 Nov 2008 11:56:28 -0200, Mark Dickinson <dickinsm at gmail.com>  
escribió:

> On Nov 19, 12:39 pm, srinivasan srinivas <sri_anna... at yahoo.co.in>
> wrote:
>> a1 = fs(1,2,3)
>> a2 = fs(3,4,5)
>> print a1.difference(a2)
>>
>> Error:
>>     return "%s(%r)" % (self.__class__.__name__, self.__data)
>> AttributeError: 'fs' object has no attribute '_fs__data'
>
> I guess you need to implement the difference method in your
> subclass.
>
> It's a little odd that an operation on subclasses of frozenset returns
> an instance of the subclass, rather than simply a frozenset. Most
> other
> Python types don't work that way.  Compare and contrast:

Yep; looks like a bug in the set/frozenset implementation. Usually builtin  
types don't return subclasses because they don't know how the constructor  
should be called - it is indeed the case here, as the OP has changed  
frozenset.__new__ signature. Even if fs.__new__ were called its arguments  
would be wrong.
The bug is not that the subclass constructor is skipped, but that  
a1.difference(a2) returns a subclass instead of a frozenset instance.
(set and frozenset are unrelated types but share a lot of their  
implementation by using generic algorithms, and it's the generic part the  
culprit here)

-- 
Gabriel Genellina




More information about the Python-list mailing list