Inheriting frozenset gives bug if i overwrite __repr__ method

Terry Reedy tjreedy at udel.edu
Wed Nov 19 12:06:28 EST 2008


srinivasan srinivas wrote:
> Hi,
> I am getting an error while executing the following snippet. If i comment out method __repr__ , it works fine.
> 
> class fs(frozenset):
>     def __new__(cls, *data):
>         data = sorted(data)
>         self = frozenset.__new__(cls, data)
>         self.__data = data
>         return self
> 
>     def __repr__(self):
>         return "%s(%r)" % (self.__class__.__name__, self.__data)
> 
> 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'
> 
> Please help me in fixing this.

When posting problems like this, please include the Python version.
If you ran this with 2.6, please run the following:

x = frozenset('abc')
y = frozenset('bcd')
z = x.difference(y)
print(id(x) != id(z))

This should print (True), but if 2.6 has a bug for frozenset similar one 
it has for bytearrays, it will print (False) and that would explain your 
error.

tjr




More information about the Python-list mailing list