overriding a tuple's __init__
Michele Simionato
mis6 at pitt.edu
Mon Aug 18 17:43:06 EDT 2003
"Andrew Dalke" <adalke at mindspring.com> wrote in message news:<bhq648$s4t$1 at slb3.atl.mindspring.net>...
> 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
<nitpick mode> Actually this was a change in 2.2 </nitpick mode>
__new__ is needed to acts on the creation of immutable objects and this
is the right way to use it; unfortunaly it gives room to plenty of abuses:
class YouThinkIamAString(str):
def __new__(cls,arg):
return 42
print YouThinkIamAString("What's the answer?")
Yes, in Python you cannot modify the builtins, but still you have plenty
of rope to shoot in your foot ;)
Michele
More information about the Python-list
mailing list