top | item 38411045

(no title)

closed | 2 years ago

Having had the great pleasure of refactoring a few python libraries, I've had to encouraged people to switch away from inheritance.

You often get a ton of methods / attributes on the child class, which gets hard to keep tabs on. You have to be careful not to use the same names for things. (The worst is that someone uses this broken encapsulation and relies on things across parent classes via the child).

I get that inheritance can go okay for simple cases, but have seen enough chaotic uses of it that I'd just encourage people to write the boilerplate for forwarding methods.

discuss

order

lostdog|2 years ago

Yup. You're trying to debug something, and you're deep in ParentClass.some_method. some_method is pretty complex, so it's tough going, but each time it calls self.other_method() or self.yet_another_method(), is that call going to the methods of ParentClass or some sub class? And 15 minutes in, do you even remember whether it was SubClass or OtherSubclass?

Inheritance turns the functional structure of your code into mud. Unless inheritance is absolutely the best design, better to drop it.

adr1an|2 years ago

Wouldn't this be solved by using private names in the parent class? No one overwrites class.__this__ without knowing that it's a deviation. To me that tabs are kept on just by using the double pair of underscores...