Algorithm for finalizing cycles (Re: [Python-Dev] Garbage collecting closures)
Martin v. Löwis
martin@v.loewis.de
15 Apr 2003 21:50:11 +0200
Guido van Rossum <guido@python.org> writes:
> > > It could make a subclass, though...
>
> > If the type is carefully designed, it can't...
>
> I suppose you have something in mind like this (which is the only way
> I can come up with to implement something like a 'final' class in pure
> Python):
I was actually thinking about impure Python, i.e. by means of omitting
Py_TPFLAGS_BASETYPE.
> But how would you prevent this?
>
> >>> a = C()
> >>> a.__class__ = D
> >>>
For the issue at hand: Assigning __class__ won't change the object
layout, so if the object didn't have an __dict__ before, it won't have
an __dict__ afterwards. Of course, if there are writable slots,
the application could corrupt the underlying resource reference,
making __del__ meaningless, anyway. Here I need to bring up
Python's "we are all consenting adults" attitude...
Regards,
Martin