Python OOP Tutorial 4: Inheritance - Creating Subclasses

Corey Schafer · 1.5M views · 2016 · 19:40

Open on YouTube

Python, Ruby & scripting languagesEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering or asking about subclasses, super().__init__ and inheritance in Python, often as the clear beginner explanation to watch first.

Recommended by 3 people across 3 different months, 2020 to 2024.

What it is

Part 4 of Corey Schafer's Python object-oriented programming series, covering inheritance and subclassing. He builds Developer and Manager subclasses of an Employee class, using super().__init__ to reuse parent behavior while overriding or adding attributes and methods.

What people said

Strong rec not-a-bot · 2024-03 · Stack Overflow · on “How to store data with lots of subdata but keep easy and simple access in python”

Your approach of organizing the data in classes is good. But you're defining the classes in a rather complex way. A better option would be to use inheritence . If you are unfamiliar with the concepts of inheritence or subclasses , you can check out this excellent video by Corey Schafer [this video] . You may want to do something like this: class Item: def __init__(self, name, description): self.name = name self.description = description class Weapon(Item): def __init__(self, name, description, damage): super().__init__(name, description) self.damage = damage class Armor(Item): def __init__(self, name, description, defense): super().__init__(name, description) self.defense = defense Then you can c

Recommends Tyler Gallenbeck · 2021-02 · Stack Overflow · on “Inherit python datetime and add new attribute for the instance in using super()”

Not 100% sure, so I can’t add an answer for you, but I think it’s because your passing an object that doesn’t take that many arguments. Try passing datetime instead of a method from datetime. Corey Schafer explains this very well here [this video]

Recommends Übermensch · 2023-02 · Stack Overflow · on “Using Gravital discord chatbot and getting "TypeError: __init__() missing 1 required keyword-only argument: 'intents'" error”

Solution You'll need to pass in a value for the intents keyword-only argument: super().__init__(intents=self.intents) or super().__init__(intents=ChatBot.intents) . Note: I'm not sure if the value for the keyword-only argument (i.e. intents ) is supposed to be the class variable intents (i.e. discord.Intents.all() ). But documentation [LINK] says, that the keyword-only argument, intents , is "The intents that you want to enable for the session. This is a way of disabling and enabling certain gateway events from triggering and being sent." Explanation for the TypeError You received a TypeError because super() returns an instance of the inherited class (i.e. Client ) and the constructor in the

Cites Heshan Kumarasinghe · 2020-09 · Stack Overflow · on “Why is the following code in Python about subclasses throw an error?”

I am a beginner in Python and have been coding along with a youtube video about classes, inheritance and subclasses. However, when I typed the following code and ran it, it threw and error even though my code has been identical to the instructor in the youtube. Can someone help me understand what is wrong with the code? (Both a picture of the code and the error is shown below) (the link to the youtube video I watched is - [this video] ) [LINK] [LINK]

Cites Desmond · 2022-07 · Stack Overflow · on “TypeError: Class object is not iterable”

Took the following code chunk from Corey Schafer [this video] . I note this is a common Python question and I've read a few [LINK] threads [LINK] like [LINK] these [LINK] , but have not found any relevant explanations. class Employee: raise_amount = 1.04 def __init__(self, first, last, pay): self.first = first self.last = last self.pay = pay self.email = first + '.' + last + '@company.com' def fullname(self): return '{} {}'.format(self.first, self.last) def apply_raise(self): self.pay = int(self.pay * self.raise_amount) class Developer(Employee): raise_amount = 1.10 def __init__(self, first, last, pay, prog_lang): super().__init__(first, last, pay) self.prog_lang = prog_lang class Manager(Employe

Cites Agustin G. · 2024-05 · Stack Overflow · on “Subclasses init method”

I have a question regarding the init method for subclasses in Python. [LINK] In the image above, a child class Developer is being defined from a parent class Employee. In Developer, the __init__ method is defined using super().__init__ (i.e the __init__ method of the parent class) and an extra attribute prog_lang is being added that is not in the parent class. This I understand. However, in the following example: [LINK] A child class App is being defined form the parent class tk.Tk from the tkinter module. Here, the same super().__init__ method is being used, which gives all the attributes from the parent class to the instances of the child class upon being instanced. However, then, rather t