overriding a tuple's __init__

Aahz aahz at pythoncraft.com
Mon Aug 18 04:31:57 EDT 2003


In article <pan.2003.08.18.07.37.44.108933 at webone.com.au>,
Simon Burton  <simonb at webone.com.au> wrote:
>
>Python 2.2.2 (#2, Nov 24 2002, 11:41:06) 
>[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>
>>>> class pair(tuple):
>...   def __init__(self,a,b):
>...     tuple.__init__(self, (a,b) )
>... 
>>>> a=pair(1,2)
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>TypeError: tuple() takes at most 1 argument (2 given)
>>>> 
>
>What gives? (yes it works with a list, but i need immutable/hashable)

You need to define __new__(); __init__() gets called *after* object
creation, when it's already immutable.
-- 
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