Thanks to visit codestin.com
Credit goes to github.com

Skip to content

more controlled inheritance #91509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vainaixr opened this issue Apr 13, 2022 · 2 comments
Closed

more controlled inheritance #91509

vainaixr opened this issue Apr 13, 2022 · 2 comments

Comments

@vainaixr
Copy link
Contributor

vainaixr commented Apr 13, 2022

is this possible?
if possible. would it be useful?

class A:
    @no_inherit
    def method_one(self, x):
        # implementation of method_one
    
    @no_override
    def method_two(self, x):
        # implementation of method_two
    
    def method_three(self, x):
        # implementation of method_three
class B(A):
    def method_two(self, x):
        # changed implementation of method_two

but method_one does not get inherited,
and
for method_two, the changed implementation does not work, the implementation described in class A for method_two stays for all the child classes.

@wookie184
Copy link
Contributor

For @no_inherit I could be missing something but I don't really see any use. Rather than preventing the method from being inherited, you can just not use it if you don't want/need it. If you want to force a method to be redefined you may want an abc (https://peps.python.org/pep-3119/)).

For @no_override you can use typing.final https://docs.python.org/3/library/typing.html#typing.final, this isn't enforced at runtime, but I don't think that's an issue. I think you could also take advantage of name mangling for some use cases https://docs.python.org/3/tutorial/classes.html#private-variables

If you wanted to enforce either of these you could probably write a metaclass that did, although I can't think of any use cases where it should be necessary.

@zware
Copy link
Member

zware commented Apr 13, 2022

Please direct usage questions to the Users section of discuss.python.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants