Re: [Python-Dev] metaclass insanity

Kevin Jacobs <jacobs@penguin.theopalgroup.com> writes:
Yeah, I noticed that eventually.
I don't think this has a hope of working does it? This approach would need to rebind "Foo" in the last line, no?
I think the error message gives it away: in "cls.__class__ = new_cls", cls.__class__ is MetaBase, new_cls is the new Foo. I guess one way of doing this would be to reimplement mro resolution and so on in Python, which would be annoying and inefficient. Hmm. Cheers, M. -- [3] Modem speeds being what they are, large .avi files were generally downloaded to the shell server instead[4]. [4] Where they were usually found by the technical staff, and burned to CD. -- Carlfish, asr

I guess one way of doing this would be to reimplement mro resolution and so on in Python, which would be annoying and inefficient. Hmm.
Alas, yes. You'd have to be able to write a class's tp_mro slot (corresponding to the __mro__ attribute), but there's no way to do that in Python, because it is too easy to cause mistakes. E.g. all C code that currently uses tp_mro assumes that it is a tuple whose items are either types or classic classes. The best thing to do would perhaps to make __mro__ assignable, but with a check that ensures the above constraint. I think I'd take a patch for that. I'd also take a patch for assignable __bases__. Note that there are constraints between __bases__ and __base__. I'd also take a patch for assignable __name__. --Guido van Rossum (home page: http://www.python.org/~guido/)

I guess one way of doing this would be to reimplement mro resolution and so on in Python, which would be annoying and inefficient. Hmm.
Alas, yes. You'd have to be able to write a class's tp_mro slot (corresponding to the __mro__ attribute), but there's no way to do that in Python, because it is too easy to cause mistakes. E.g. all C code that currently uses tp_mro assumes that it is a tuple whose items are either types or classic classes. The best thing to do would perhaps to make __mro__ assignable, but with a check that ensures the above constraint. I think I'd take a patch for that. I'd also take a patch for assignable __bases__. Note that there are constraints between __bases__ and __base__. I'd also take a patch for assignable __name__. --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (2)
-
Guido van Rossum
-
Michael Hudson