overriding a tuple's __init__
Andrew Dalke
adalke at mindspring.com
Mon Aug 18 05:30:40 EDT 2003
Simon Burton:
> >>> class pair(tuple):
> ... def __init__(self,a,b):
> ... tuple.__init__(self, (a,b) )
> What gives? (yes it works with a list, but i need immutable/hashable)
The problem is the immutability. This one one of the
new changes in 2.3 I still don't fully understand, but I do
know the solution is __new__
>>> class pair(tuple):
... def __new__(self, a, b):
... return tuple((a, b))
...
>>>
>>> pair(2,3)
(2, 3)
>>> x=_
>>> type(x)
<type 'tuple'>
>>>
That should give you some pointers for additional searches.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list