[Tutor] mutability, __new__
Terry Carroll
carroll at tjc.com
Wed Jan 17 02:32:50 CET 2007
[hijacking the "question about object oriented programming and
inheritance using datetime module" thread here....]
On Tue, 16 Jan 2007, Kent Johnson wrote:
> I think you are missing the line
> datetime.date.__init__(self, year, month, day)
> somewhere in here.
Yeah; sloppy of me.
> I am very surprised that this works, my understanding was that
> datetime.date was immutable and required overriding __new__() rather
> than __init__(). But it does work...
Yeah, come to think of it, that is interesting.
A quick test shows that datetime.date is apparently immutable, because it
can be used as a dictionary key.
But if it's immutable, then __init__ shouldn't work, and, as you say, you
gotta go with __new__.
I have to admit, I don't completely comprehend the use of __new__, but the
discussion at http://www.python.org/download/releases/2.2.3/descrintro/
explained quite a lot.
How can you find if a particular object is immutable or not? Or,
relatedly, if you wanted to define an object that was immutable, how would
you do it?
More information about the Tutor
mailing list