Inheriting frozenset gives bug if i overwrite __repr__ method

Mark Dickinson dickinsm at gmail.com
Wed Nov 19 08:56:28 EST 2008


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:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class myint(int): pass
...
>>> a = myint(3)
>>> b = myint(5)
>>> c = a+b
>>> c
8
>>> type(c)
<type 'int'>
>>> class fs(frozenset): pass
...
>>> a = fs([1, 2, 3])
>>> b = fs([3, 4, 5])
>>> c = a - b
>>> c
fs([1, 2])
>>> type(c)
<class '__main__.fs'>
>>>

Mark



More information about the Python-list mailing list