Is it possible to inherit a limited set of methods from a super class in Python?

Asked

Viewed 54 times

-1

I would like to build a class that inherits only a few methods from the mother class, but not all. To understand what I mean, suppose I have two classes, Person and Student:

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname
    
    def __str__(self):
        return self.firstname

    def name_double(self):
        return 2*self.firstname

class Student(Person):
    pass


if __name__ == "__main__":
    person=Person("Alan", "Turing")
    student=Student("William","Gosset")
    print(person)
    print(person.name_double())
    print(student)
    print(student.name_double())

My goal is for the Student class to inherit only the methods __init__ and __str__ of the Person class, but not name_double. What I tried about the class __init__ went to use:


class Student(Person):
    def __init__(self, fname, lname):
        Person.__init__(self, fname, lname)

I thought that way the Student class would inherit only the method __init__ class Person, but that’s not what happened. When I run the program up with proper replacement in the class Student, the result is the same:

Alan
AlanAlan
William
WilliamWilliam

It is possible to inherit methods selectively without having to make another class?

  • 1

    It is possible to inherit methods selectively without having to make another class? NO! You can create a common ancestor that contains only certain methods to be inherited by all descendants. Now as you are wanting to do the modeling of student and person is strange, because it gives to understand that you want to do with what a student is not a person, because as idiotic as it may seem that my observation it affects the development of the code and sometimes even prevent the logical development forcing the use of gambiarras to continue to produce.

  • 1

    Perhaps the concept of mixins can be useful to you.

1 answer

3


No, that doesn’t make sense.

When you inherit something you are saying that you want what exists. If you don’t want it then don’t inherit it. Inheritance makes you take over all existing contracts in the base class.

If it is something that has no control. Forget it, if it is something you created and will use. the design is wrong, probably should have smaller classes doing less things.

But here comes another problem because to put it all together would need appropriate mechanisms that Python does not have.

This is explained in Principle of Liskov’s replacement. Something must not always have an inheritance, or whether it must have a meaning in it.

And two things come to mind: contrary to popular belief, Python is not as simple and powerful as people think, it is good for simple things, if you need more than that Python is the wrong language, it takes a lot of work to do right and it is no longer simple and productive; and when it starts having to do something more granular then object orientation is probably the wrong technique to apply.

And another thing I always say: artificial examples are good for explaining mechanisms, but they’re terrible for teaching design. Most people learn wrong because they teach design with artificial example.

If you do not want to inherit a class in classes that cannot have a method, then that method should not be in the upper class. This is a solution.

Another solution is to have this method yes, what problem does the inherited class have? It may be something ill defined.

The base class is a minimum common denominator. If something there is not common to everything that will be inherited, it is wrong. Or else there should be no inheritance relationship between these classes.

But it also thinks whether the base class defines well what a person is. Artificially yes, but concretely it’s likely not, so you’re training the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.