Jim Jewett wrote:
On 11/13/07, Oleg Broytmann firstname.lastname@example.org wrote:
On Tue, Nov 13, 2007 at 12:23:40AM -0700, Neil Toronto wrote:
I've got a working prototype metaclass and class instance (require_super) and decorator (super_required).
Is this restricted to __init__ (and __new__?) or could it be used on any method?
It can be used on any method.
Is there (and should there be?) a way around it, by catching the TypeError? By creating a decoy object to call super on?
Definitely should be, and I made one because I plan on using this myself. :) Currently, you can set self.<method>_super = True (or self.__<method>__super = True) instead of doing the superclass method call. (Yes, it currently litters the class instance with flags, but that's an implementation detail.) If you're not going to call the superclass method, you need to state that explicitly.
class C(B): def __init__(self): self.__init__super = True
c = C() # No problem
I've fiddled with the idea of having a redecoration with @super_required remove the requirement from the current method but place it back on future overrides. Maybe a @super_not_required could remove it completely.
Chicken and egg problem, in my eyes. If the user is clever enough to use the class and the decorator isn't she clever enough to call inherited __init__?
It may not be the same user.
A library or framework writer would create the base class and use the decorator to (somewhat) ensure that subclasses meet the full interface requirements.
A subclass writer should call the super.__init__ because it is in the API, but Neil's metaclass makes it easier to debug if they forget.