[Python-Dev] Re: Writing a mutable object problem with __setattr__
Guido van Rossum
guido@python.org
Tue, 25 Feb 2003 12:31:08 -0500
> > In Python 2.2.2 you can set __class__, as long as __class__ has a
> > compatible instance lay-out (at the C implementation level).
>
> This is the part where I am confused. How do I know if something
> is an old or new style object? Do new-style objects derive from
> built-in types (object/dict..)
Yes. Please re-read http://www.python.org/2.2.2/descrintro.html
> > Use new-style classes and you'll be much happier: you can invoke the
> > superclass __setattr__ to do the magic.
>
> I've tried this, but then I was unable to create any of the
> old-style classes. Is there any solution that would let me mutate
> an object into both? I suspect not. Then the question is:
No, you can never switch an object from classic to new-style.
> - is there a runtime/compiling python flag to force all objects
> into new-style layout? This will be the future, I think I've read
> somewhere.
Almost. You can add __metaclass__=type to the top of each module, and
then classes without a base class will become new-style. Classes
derived from other classes will still be whatever that other class is;
and there's no way to force the whole standard library to use
new-style classes (amongst many reasons, exceptions must be classic
classes).
> - how can I tell the difference between old and new programatically?
type(C) == types.Classtype. Or check if the class has a __class__
attribute (classic classes don't).
--Guido van Rossum (home page: http://www.python.org/~guido/)