[Python-3000] [Fwd: features i'd like [Python 3000] ... #3: fix super()] (original) (raw)

Ben Wing ben at 666.com
Mon Dec 4 11:10:05 CET 2006


on suggestion of someone on python-dev, reposted on python-3000. hope that's ok.

ben

-------- Original Message -------- Subject: features i'd like [Python 3000] ... #3: fix super() Date: Sun, 03 Dec 2006 21:06:07 -0600 From: Ben Wing <ben at 666.com> To: python-dev at python.org

i don't like the current super() at all. having to type super(Foo, self).init(...) is messy, hard to remember, and error-prone. it also introduces an unfortunate dependency in that the name of the class (Foo) has to be hard-coded in the call, and if you change the class name you also have to go and change all super() calls -- more chances for errors. as a result, i imagine there's a strong urge to just hardcode the name of the parent -- a bad idea, and the reason why super() was introduced in the first place.

instead, `super' should work like in other languages. a couple of ideas:

-- super.meth(args) calls the superclass method meth', in the same way that super(Foo, self).meth(args) currently works. (this is the same syntax as in java. this probably requires making super' be a keyword, although it might be possible to hack this by examining the current call stack.) -- as an alternative or in addition, super(args) would work like super.meth(args) if we're currently inside of meth' in a subclass. i suspect that 90% of the time or more, super' is used to chain to the superclass version of an overridden method, and this optimizes for the common case. it makes for one less possibility of misspelling __init__' and one less thing that requires renaming if a method is renamed or code copied to another method (granted, this isn't such a big deal as in the case of class renaming). if the form super.meth(args)' isn't also supported, then a call of this sort would have to be made through the introspection API. (i think it would be a good idea to have this functionality available through the introspection API, in any case. currently i don't see any obvious way in module inspect' to find out which class a call to super(...).meth(...)' is invoked on, although maybe a loop with inspect.getmro() and hasattr() would work. it would be nice to have a function like inspect.getsuper(class, 'meth'), i think.)

ben



More information about the Python-3000 mailing list