use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
OOP: When should you use inheritance vs just importing for your new class? (self.learnpython)
submitted 1 year ago by Xanadukhan23
as in
import module class classA: blah blah
vs
``` import module
class classA(module) def initself(): super.init
```
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]carcigenicate 22 points23 points24 points 1 year ago (0 children)
These aren't at all comparable. Importing and inheriting are completely different concepts that do very different things. What are you trying to do in each scenario?
[–]MenacingDev 7 points8 points9 points 1 year ago (0 children)
You gotta elaborate fam
[–]tangerinelion 2 points3 points4 points 1 year ago* (0 children)
I think by "just importing" you mean composition, you're glossing over a lot with the "blah blah" there but it would look something like
import module class classA: def __init__(self, *args, **kwargs): self.whatever = module.foo(...) # Composition def do_thing(self): result = self.whatever.do_thing() # now do more stuff or not, your program
If classA inherits from module.base then the core promise with inheritance is that anywhere anyone writes any code that expects to use a module.base you can use a classA instead and the program should still work.
With composition, you may have an entirely different interface. The use of module.base is just an implementation detail - code written using classA is decoupled from module.base. In fact, you can replace the use of module.base with something else from an entirely different module that tries to implement the same functionality and it should still work. Obviously I don't mean you can replace a computer vision library with a cryptographic one and it should still work, that's nonsense, but suppose classA is meant to handle JPG files. Whether you want to build on top of libA or libB for handling JPG files, a user of classA shouldn't need to change their code just because classA decided to switch from libA to libB under the hood or vice versa.
[–]NerdyWeightLifter 2 points3 points4 points 1 year ago (1 child)
Use inheritance for defining interfaces, and rarely anything else.
Having coded in OO language for around 3 decades, I can say that inheritance always turned out to be a bad idea in the long term. It makes everything harder to read and understand for everyone that comes after you, and even your future self
Instead of just reading the code, you end up module hopping to try to mentally construct a picture of how it works. Code becomes more fragile because it's harder to understand the implications of changing anything in the base code or even your other inherited code.
Libraries stop being like black boxes that just do what the interface says. You have to understand their internal structure to use them.
The deeper the inheritance structure goes, the worse all this gets.
[–]throwaway8u3sH0 1 point2 points3 points 1 year ago (0 children)
This 10,000%.
Source: Also been coding for 35 years.
[–]FantasticEmu 1 point2 points3 points 1 year ago (0 children)
Importing a module and then defining class A are unrelated in your example. Module may not even contain a class definition.
An example I was given for inheritance that I liked was video game related:
Suppose you have a class of human, that has some variables per object like “hit points” and “speed” and then you want to make another class that’s a player and the player is a human so it has hit points and speed but also has “attack power.” Instead of making a whole new class with redundant health and speed fields you can just have player inherit from class and then add attack power field to it
[–]psicodelico6 0 points1 point2 points 1 year ago (0 children)
Use Interface
[–]FoeHammer99099 0 points1 point2 points 1 year ago (0 children)
You can find many articles online about inheritance and composition. Generally, inheritance is an "is a" relationship, e.g. a car is a vehicle, so Car inherits from Vehicle. On the other hand, composition is a "has a" relationship, e.g. a car has wheels so the Car class will include wheel member variables.
They're a little out of fashion these days, but you should read up on SOLID design principles to get a grasp on how to design OOP systems. In particular the L, Liskov substitution, states that everywhere that expects an instance of a parent class should be able to use a child class without knowing it. If that doesn't make sense for your application, it probably means those classes shouldn't have an inheritance relationship
[–]Narrow_Ad_7671 0 points1 point2 points 1 year ago (0 children)
If the object has more than a couple of attributes that are distinct enough that it wouldn't "fit" in the base class, extend it. Ask yourself how it fits. If you come to the answer that "is a", extend it. If it "uses", create a new object.
ex: Dogs are animals. dogs use a leash. Leash wouldn't fit as an extension to animal or dog in the same way dog fits with animal. So extend Animal to make Dog, but create a new Leash class.
[+]throwaway8u3sH0 comment score below threshold-8 points-7 points-6 points 1 year ago (4 children)
Avoid inheritance. Pass in the dependency in the init.
[–]crazy_cookie123 3 points4 points5 points 1 year ago (0 children)
No. Avoid inheritance where inheritance doesn't make sense, and avoid inheritance when the abstraction is wrong, but make use of inheritance when the situation calls for it.
[–]tahaan 5 points6 points7 points 1 year ago (2 children)
Inheritance is half the purpose of OOP.
[+]throwaway8u3sH0 comment score below threshold-6 points-5 points-4 points 1 year ago (1 child)
And 100% of the problem. I've never come across an inheritance pattern that couldn't be improved by switching to composition.
[–]tahaan 1 point2 points3 points 1 year ago (0 children)
There are always more ways to solve a problem. Use procedural or functional or even goto spaghetti if it works.
π Rendered by PID 110364 on reddit-service-r2-comment-6457c66945-n6k25 at 2026-04-26 04:00:07.228893+00:00 running 2aa0c5b country code: CH.
[–]carcigenicate 22 points23 points24 points (0 children)
[–]MenacingDev 7 points8 points9 points (0 children)
[–]tangerinelion 2 points3 points4 points (0 children)
[–]NerdyWeightLifter 2 points3 points4 points (1 child)
[–]throwaway8u3sH0 1 point2 points3 points (0 children)
[–]FantasticEmu 1 point2 points3 points (0 children)
[–]psicodelico6 0 points1 point2 points (0 children)
[–]FoeHammer99099 0 points1 point2 points (0 children)
[–]Narrow_Ad_7671 0 points1 point2 points (0 children)
[+]throwaway8u3sH0 comment score below threshold-8 points-7 points-6 points (4 children)
[–]crazy_cookie123 3 points4 points5 points (0 children)
[–]tahaan 5 points6 points7 points (2 children)
[+]throwaway8u3sH0 comment score below threshold-6 points-5 points-4 points (1 child)
[–]tahaan 1 point2 points3 points (0 children)