[Python-ideas] method decorators @final and @override in Python 2.4

Guido van Rossum guido at python.org
Sat Mar 28 23:03:50 CET 2009

On Sat, Mar 28, 2009 at 10:21 AM, Mathias Panzenböck
<grosser.meister.morti at gmx.net> wrote:
> Péter Szabó wrote:
>> If Python had method decorators @final (meaning: it is an error to
>> override this method in any subclass) and @override (meaning: it is an
>> error not having this method in a superclass), I would use them in my
>> projects (some of them approaching 20 000 lines of Python code) and
>> I'll feel more confident writing object-oriented Python code. Java
>> already has similar decorators or specifiers. Do you think it is a
>> good idea to have these in Python?
>> I've created a proof-of-concept implementation, which uses
>> metaclasses, and it works in Python 2.4 an Python 2.5. See
>> http://www.math.bme.hu/~pts/pobjects.py and
>> http://www.math.bme.hu/~pts/pobjects_example.py
>> Best regards,
>> Péter
> +1 on the idea.
> however, using a metaclass would be to limiting imho. can you implement it
> in a different way? a lot of things people use metaclasses for work
> perfectly fine without them (instead use a superclass that overrides __new__
> or similar).

While it could be done by overriding __new__ in a superclass I'm not
sure how that would make it easier to use, and it would make it harder
to implement efficiently: this is a check that you would like to
happen once at class definition time rather than on each instance
creation. Of course you could do some caching to do it at the first
instantiation only, but that still sounds clumsy; the metaclass is the
obvious place to put this, and gives better error messages (at import
instead of first use).

But I don't think this idea is ripe for making it into a set of
builtins yet, at least, I would prefer if someone coded this up as a
3rd party package and got feedback from a community of early adopters
first. Or maybe one of the existing frameworks would be interested in
adding this? While this may not be everyone's cup of tea (e.g. Scott
David Daniels' reply), some frameworks cater to users who do like to
be told when they're making this kind of errors.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-ideas mailing list