overriding a tuple's __init__

Aahz aahz at pythoncraft.com
Mon Aug 18 10:27:47 EDT 2003


In article <bhq648$s4t$1 at slb3.atl.mindspring.net>,
Andrew Dalke <adalke at mindspring.com> wrote:
>
>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.

This works better:

class pair(tuple):
    def __new__(cls, *args):
        return tuple.__new__(cls, args)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

This is Python.  We don't care much about theory, except where it intersects 
with useful practice.  --Aahz




More information about the Python-list mailing list