1

In languages like C++, we can inheritance in two ways.

#include <iostream>

class Base {
public:
   void base_function() {
      std::cout << ("Base-Class function\n");
   }
};

class Derived1: public Base {
 // we can call base_function on object of class Derived1
};
class Derived2 : private Base {
public:
    void derive_function() {
        std::cout << "We can call base_function only inside methods\n";
        base_function();
    }   
};
  • Private inheritance - It means that we inherit the implementation, but we are not a type of BaseClass.

  • Public inheritance - It means that we inherit the interface, so we are a type of BaseClass.

Is there a way in python to actually create something that will act as a private inheritance? Are there any python tricks? Or we must base a solution on Association/Aggregation/Composition?

XYZCODE123
  • 372
  • 3
  • 12
  • 1
    (Of the linked question, look at the answer, the question itself might seem different but IMO it still applies to this case as well) – GPhilo Dec 12 '19 at 10:47
  • Not really. What I want is to make everything that was public in BaseClass private in DerivedClass. So client won't be able to call BaseClass methods on object of class DerivedClass. – XYZCODE123 Dec 12 '19 at 11:00
  • The relevant part of the linked answer is essentially "Python has no privacy model, there are no access modifiers like in C++, C# or Java. There are no truly 'protected' or 'private' attributes". You can't make them private, because there is no such concept. Even if you renamed each of them with the mangling convention, every method will still be accessible from any user determined enough. – GPhilo Dec 12 '19 at 11:02
  • I was interested in finding a GOOD implementation of Adapter(structural design pattern) in Python. I saw implementation based on Aggregation. In C++ adapter is based on private and public inheritance. So I was curious if I can do the same in Python. But I asked more general question, thx for answer. – XYZCODE123 Dec 12 '19 at 11:18
  • 1
    If your goal is to make it clear that your child class is not a subclass of the parent one, then you will indeed have to use composition / delegation instead of inheritence. Use a single leading underscore for the "base class" instance attribute to signal this is an implementation attribute (python's convention), then anyone accessing it directly is on it's own if anything breaks - you warned them, they knew what they were doing, not your problem anymore. – bruno desthuilliers Dec 12 '19 at 11:24
  • @brunodesthuilliers The point of the *class* adapter design pattern (not the *object* adapter design pattern) is to convert the interface (*public* methods) of an Adaptee class into the interface of a Target abstract class by using an Adapter class that inherits *publicly* (*interface* inheritance) from the Target abstract class and *privately* (*implementation* inheritance) from the Adaptee class (cf. the *Design Patterns* book by Erich Gamma et al.). – Géry Ogam Jul 26 '20 at 21:55

0 Answers0