[Python-Dev] Re: super() harmful? (original) (raw)

Phillip J. Eby pje at telecommunity.com
Thu Jan 6 18:44:58 CET 2005


At 02:46 AM 1/6/05 -0500, James Y Knight wrote:

To fix #1, it would be really nice if you could write code something like the following snippet. Notice especially here that the 'bar' argument gets passed through C.init and A.init, into D.init, without the previous two having to do anything about it. However, if you ask me to detail how this could possibly ever work in python, I have no idea. Probably the answer is that it can't.

class A(object): def init(self): print "A" nextmethod class B(object): def init(self): print "B" nextmethod

Not efficiently, no, but it's possible. Just write a 'next_method()' routine that walks the frame stack and self's MRO, looking for a match. You know the method name from f_code.co_name, and you can check each class' dict until you find a function or classmethod object whose code is f_code. If not, move up to the next frame and try again. Once you know the class that the function comes from, you can figure out the "next" method, and pull its args from the calling frame's args, walking backward to other calls on the same object, until you find all the args you need. Oh, and don't forget to make sure that you're inspecting frames that have the same 'self' object.

Of course, the result would be a hideous evil ugly hack that should never see the light of day, but you could do it, if you really really wanted to. And if you wrote it in C, it might be only 50 or 100 times slower than super(). :)



More information about the Python-Dev mailing list