[Python-ideas] The prohibition overrides the class method

Chris Angelico rosuav at gmail.com
Mon Nov 16 10:44:45 EST 2015


On Tue, Nov 17, 2015 at 2:03 AM, Иван Спиненко <spinenkoia at gmail.com> wrote:
> Hello, I want to apologize for my English.

It's fine - and a lot better than my Russian! (Or Ukrainian, or any of
the other languages that use a Cyrillic script. See, I can't even tell
one from another - all I know is which script you've used.)

> I propose to make an analogue of the final specifier in C ++. In class
> abc.ABCMeta has decorator abstractmethod, you can add decorator finalmethod.

It's not common in Python to actually enforce this kind of thing.
What's the advantage of stopping people from overriding some methods?
In Java, as I understand it, the compiler optimizes calls to final
methods; but in Python, you can even add methods to individual
instances of a class:

>>> class X:
...     def method(self):
...         print("I am the regular method.")
...
>>> x = X()
>>> x.method()
I am the regular method.
>>> x.method = lambda: print("I am the custom method.")
>>> x.method()
I am the custom method.

Can you show an example of how this decorator would help your project?
If so, it might make a useful recipe, rather than actually a part of
the language or standard library - it can go into a collection of
"here's some neat tricks you can do with function decorators" tips.

ChrisA


More information about the Python-ideas mailing list