Is there a way to make a class function unoverriddable? Something like Java's final
keyword. I.e, any overriding class cannot override that method.

- 30,738
- 21
- 105
- 131

- 23,991
- 34
- 108
- 149
-
4You haven't even *private* methods in Python, the `__xxx` members are only shadowed. I'd really be surprised, if there would be anything like a `final` keyword. – Boldewyn Mar 11 '10 at 14:26
-
Why? What's the use case for preventing this? People can read your source and rewrite the function. What are you trying to do? – S.Lott Mar 11 '10 at 14:28
-
I don't know why all of you presume I want it for preventing someone else from overriding it - I want it to prevent myself from accidentally overriding it in the future. Assuming I have a relatively deep inheritance tree, and one of the "deeper" classes will accidentally redefine the func (forgetting it even exists) – olamundo Mar 11 '10 at 14:49
-
4you could start by revisiting your design. Do you really need such a deep inheritance tree? If you can flatten it out, it'll help you keep more of it your head at once. – Michael Kristofik Mar 11 '10 at 14:57
-
3"prevent myself from accidentally overriding" That's an odd use case. A feature of OO design is tidy and crisp allocation of responsibility. It should be very, very clear what functions are sensible in a given class. If it isn't crystal clear, you have too many responsibilities built into a single class. Preventing naming problems is secondary to preventing too much responsibility in a single class. – S.Lott Mar 11 '10 at 15:06
-
@S.Lott: In the case of other users' use, one could, theoretically, look at the source code of *anything* and overwrite it. But there are cases where some atomic methods are foundational to the proper functioning of the entire framework. This is still a good idea to prevent users from accidentally meddling in "dragons' territory". – Noob Saibot Jul 21 '14 at 16:42
4 Answers
You could add a comment in there to the effect of:
# We'll fire you if you override this method.
It's surprising how well low-tech solutions like this work in practice.

- 30,738
- 21
- 105
- 131

- 34,290
- 15
- 75
- 125
-
5+1 And if this was discovered from a user bug report, we'll fire the wallies who didn't test the app properly. – John Machin Mar 11 '10 at 21:56
-
19This should be part of the doc strings (i.e. API spec) rather than a comment – vog Oct 16 '10 at 12:55
-
I tend to put "final" in the doc-str, but this does get a bit tedious for projects with more than 20 methods. 9 years since this answer, we now have the `abc` library (Google), that uses functionless decorators to signify inheritance in a more formalised way, while the `typing` library (Rossum) uses an internal `_Final` class that safeguards *accidental* overriding. – c z Feb 08 '19 at 10:06
The issue is you are trying to write in Python using Java philosophies. Some thing carry over, but not all of them. In Python you can do the following and it is perfectly fine, but it completely goes against how Java thinks of objects.
class Thing(object):
x = 1
something = Thing()
something.y = something.x
If you really want it, you can try the code posted here. But as you can see, there is a lot of code there to get it to do what you want. It also should be noted that even the person that posted the code says it can be bypassed using __dict__
or object.__setattr__
.

- 30,738
- 21
- 105
- 131

- 17,141
- 7
- 47
- 64
-
5justifying shortcomings of the langauge with "pilosophy" is the Java philosphy ;). Python has `final` since 3.8 (https://www.python.org/dev/peps/pep-0591/) – 463035818_is_not_an_ai May 14 '20 at 07:38
Using a double underscore before a method in a class is not just a naming convention. By doing so, the method name is mangled with classname(_classname__methodname())
.
By inheriting a class with a method having double underscores in front of it, it becomes difficult for the child class to override the above specified method.
This practice is almost equivalent to final in Java.

- 30,738
- 21
- 105
- 131

- 51
- 5
-
1
-
Is that very idiomatic in python projects, to protect inherited class members by having them originally named like that? – matanster Apr 23 '23 at 09:10
Yes, there is: Don't do it!
Such protection mechanisms are seen by some to go against the ethos of Python, that, "We are all consenting adults here." From whom do you want to protect such functions? And, if a comment will not suffice, why would something 'stronger'?
I would document your expectations and expect other programmers to act responsibly.

- 49,934
- 160
- 51
- 83

- 4,704
- 27
- 38