SUPER() in Python explained! 🔴

Bro Code
24 May 202413:05

Summary

TLDRThis video script explains the concept and utility of the 'super' function in Python, which is instrumental in a child class to invoke methods from a parent class. It demonstrates the function's role in avoiding code redundancy by utilizing inheritance, showcasing how to define constructors and methods in a 'Shape' parent class and its child classes like 'Circle', 'Square', and 'Triangle'. The script also illustrates method overriding and extending functionality of inherited methods using 'super', providing practical examples of object instantiation and method calls to clarify these concepts.

Takeaways

  • 📚 The 'super()' function in Python is used to call methods from a parent class within a child class, promoting code reusability.
  • 👨‍👧 The child class is often referred to as a 'subclass', and the parent class as the 'superclass', which is the origin of the 'super' function's name.
  • 🔄 'super()' allows for extending the functionality of inherited methods, which can be useful for method overriding or extending.
  • 📐 The script provides an example using shape classes like Circle, Square, and Triangle, demonstrating how attributes like color and fill status can be inherited.
  • 🔧 The use of 'super()' eliminates the need to manually assign common attributes in each child class constructor, streamlining the code.
  • 🏗️ By placing shared attributes in a parent class, any changes to these attributes need to be made only once, reducing the potential for errors.
  • 📝 The script illustrates how to use 'super()' to call the parent class constructor, passing necessary arguments to initialize child class objects.
  • 🎨 It demonstrates object instantiation with attributes like color, fill status, and specific shape dimensions, using the 'super()' function for clarity.
  • 📈 The 'super()' function can also be used to extend the functionality of a method, as shown with the 'describe' method in the shape classes.
  • 🛠️ Method overriding is shown where a child class has a similar method to the parent class, and the child's version is used instead of the parent's.
  • 📚 The script concludes by emphasizing the utility of 'super()' in both constructors for attribute assignment and in methods for functionality extension.

Q & A

  • What is the purpose of the 'super' function in Python?

    -The 'super' function in Python is used within a child class to call methods from a parent class. It allows for the extension of functionality of inherited methods and helps in reusing code to avoid redundancy.

  • What is the term used for the class that inherits from another class?

    -The class that inherits from another is called a 'subclass' or 'child class', while the class from which it inherits is referred to as the 'superclass' or 'parent class'.

  • What are some attributes that might be common among different shape classes like Circle, Square, and Triangle?

    -Common attributes among shape classes could include 'color' and 'is filled', which are shared by all shapes regardless of their specific geometric properties.

  • How does the script suggest handling attributes that are unique to each shape class?

    -The script suggests defining unique attributes like 'radius' for Circle, 'width' for Square, and 'width' and 'height' for Triangle within their respective class constructors after calling the superclass constructor with 'super'.

  • What is the advantage of using 'super' in the constructors of child classes?

    -Using 'super' in the constructors of child classes allows the child class to inherit the initialization of common attributes from the parent class, reducing the need for repetitive code and potential for errors.

  • Can you provide an example of how to instantiate a Circle object with the given script?

    -A Circle object can be instantiated by calling `Circle(color='red', is_filled=True, radius=5)`, which passes the color, fill status, and radius to the Circle class constructor.

  • What is the purpose of the 'describe' method in the script?

    -The 'describe' method is used to print out the attributes of a shape, such as its color and whether it is filled or not. It can also be overridden or extended in child classes to include additional information specific to the shape type.

  • How does method overwriting work in the context of the script?

    -Method overwriting occurs when a child class defines a method with the same name as a method in the parent class. In such cases, the child class's version of the method is used instead of the parent's.

  • What is the significance of using 'super' to extend the functionality of a method in a child class?

    -Using 'super' to extend the functionality of a method allows the child class to call the parent class's method and then add additional functionality on top of it, combining the behaviors of both methods.

  • Can you explain how the script demonstrates the concept of inheritance and method overriding with the shape classes?

    -The script demonstrates inheritance by having Circle, Square, and Triangle classes inherit common attributes from a Shape parent class. Method overriding is shown by redefining the 'describe' method in each child class to include specific attributes like area calculations, while still being able to call the parent class's 'describe' method using 'super'.

  • What is the practical application of using 'super' for both constructors and methods in Python classes?

    -Using 'super' for constructors ensures that common attributes are initialized properly across all subclasses, while using it for methods allows for extending or modifying inherited behavior without losing the functionality of the parent class, promoting code reusability and maintainability.

Outlines

00:00

📚 Introduction to Python's Super Function

This paragraph introduces the concept of the 'super' function in Python, which is used in child classes to call methods from a parent class. It explains the terminology 'sub-class' and 'super-class' and how the 'super' function helps in extending the functionality of inherited methods. An example is given to illustrate creating shape objects like Circle, Square, and Triangle, and how they can share common attributes such as 'color' and 'filled'. The paragraph further discusses the redundancy in manually assigning these attributes in each class constructor and highlights the benefits of using inheritance and the 'super' function to avoid repetitive code.

05:02

🔄 Utilizing Super for Constructor Inheritance

The second paragraph delves into the practical application of the 'super' function by demonstrating how to use it in the constructors of child classes to inherit and initialize attributes from a parent class. It shows how to create a 'Shape' parent class with common attributes 'color' and 'is_filled', and how child classes like 'Circle', 'Square', and 'Triangle' can inherit these attributes by calling the 'super' function within their constructors. The paragraph also covers the instantiation of objects for each shape, using both positional and keyword arguments for clarity, and confirms that the 'super' function successfully assigns the inherited attributes.

10:05

📝 Method Overwriting and Extension with Super

The final paragraph discusses advanced uses of the 'super' function, including method overwriting and extending the functionality of inherited methods. It explains how a child class can have a method with the same name as the parent class, and in such cases, the child's method will be used. The paragraph provides an example of adding a 'describe' method to the 'Shape' class and then extending this method in the 'Circle', 'Square', and 'Triangle' classes to include specific calculations for the area of each shape. It also demonstrates how to use the 'super' function to call the parent class's 'describe' method and then add additional functionality, thus combining the inherited behavior with new features.

Mindmap

Keywords

💡super function

The 'super function' in Python is a special method used within a child class to call methods from a parent class. It is essential for inheritance, allowing the child class to extend or reuse code from the parent. In the script, the super function is used to call the constructor of the parent class 'Shape' within the constructors of child classes like 'Circle', 'Square', and 'Triangle', thus avoiding code duplication and promoting code reusability.

💡child class

A 'child class' is a subclass in an object-oriented programming language that inherits properties and methods from another class, known as the parent or superclass. In the video, 'Circle', 'Square', and 'Triangle' are child classes that inherit attributes like 'color' and 'is_filled' from their parent class 'Shape', demonstrating the concept of inheritance.

💡parent class

The 'parent class', also known as the superclass, is the class from which other classes (child classes) inherit properties and methods. It serves as a blueprint for child classes. In the script, 'Shape' is the parent class for 'Circle', 'Square', and 'Triangle', providing common attributes that are then specialized in each child class.

💡inheritance

Inheritance is a fundamental concept in object-oriented programming where a child class can inherit attributes and methods from a parent class. This promotes code reusability and reduces redundancy. The script illustrates inheritance by showing how 'Circle', 'Square', and 'Triangle' inherit attributes from the 'Shape' class and extend its functionality.

💡constructor

A 'constructor' in Python is a special method called `__init__` that is automatically called when an object is created from a class. It is used to initialize the attributes of the class. The script demonstrates the use of constructors in the 'Shape', 'Circle', 'Square', and 'Triangle' classes to set initial values for attributes like 'color', 'is_filled', 'radius', 'width', and 'height'.

💡attributes

In object-oriented programming, 'attributes' are the properties or characteristics of an object, defined by variables within a class. The script discusses attributes such as 'color', 'is_filled', 'radius', 'width', and 'height' that are specific to different shapes and how they are assigned in the constructors of the respective classes.

💡method overwriting

Method overwriting, or method overriding, occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. In the script, the 'describe' method is overwritten in the 'Circle', 'Square', and 'Triangle' classes to provide shape-specific descriptions, while still having the option to call the superclass's 'describe' method using the super function.

💡method extending

Method extending refers to the process of enhancing or adding functionality to an inherited method in a child class. The script shows how the 'describe' method can be extended in the 'Circle' class by first calling the superclass's 'describe' method using the super function and then adding additional information about the circle's area.

💡F-string

An 'F-string' is a feature in Python that allows for easy and readable string formatting. It is denoted by an 'f' or 'F' before the opening quotation mark. In the script, F-strings are used to format the output of the 'describe' method, embedding the values of attributes directly within the string literals.

💡object instantiation

Object instantiation is the process of creating an instance of a class, resulting in an object that has a specific state defined by the class's attributes. The script demonstrates object instantiation with the creation of 'Circle', 'Square', and 'Triangle' objects, specifying their attributes such as color, fill status, and dimensions.

Highlights

Introduction to the 'super' function in Python, which allows a child class to call methods from a parent class.

Explanation of the term 'super' as derived from 'super class' and 'sub class'.

Demonstration of creating shape objects with unique attributes like color, filled status, and specific geometric properties.

The importance of constructors in initializing objects with attributes.

Identifying common attributes among different shape classes to avoid code repetition.

The concept of inheritance and how it simplifies code maintenance by centralizing shared attributes.

Using the 'super' function to call the parent class constructor within child class constructors.

Example of creating a 'Circle' object with color, filled status, and radius, showcasing the use of 'super'.

Creating a 'Square' object emphasizing the use of 'super' for shared attributes and unique attributes like width.

Constructing a 'Triangle' object, highlighting the use of 'super' and specific attributes like width and height.

The practicality of using 'super' to avoid manual attribute assignment in each child class.

Introduction of a 'describe' method in the 'Shape' class to demonstrate method functionality.

Method overwriting explained through the creation of a 'describe' method in child classes.

Extending the functionality of a parent class method using 'super' in child class methods.

Calculating and printing the area of a 'Circle' using the overridden 'describe' method.

Describing a 'Square' with an area calculation in its 'describe' method, showcasing method extension.

Describing a 'Triangle' with an area calculation, further illustrating method extension and overwriting.

The 'super' function's role in simplifying the extension and maintenance of inherited methods.

Transcripts

play00:00

hey everybody so today I got to talk

play00:02

about the super function in Python super

play00:05

is a function it's used within a child

play00:08

class to call methods from a parent

play00:10

class the child class is the subass the

play00:13

parent class is the super class hence

play00:16

why this function is named the super

play00:18

function using the super function it

play00:20

allows you to extend the functionality

play00:22

of the inherited methods here's an

play00:24

example we'll create a few shape objects

play00:27

we'll need to set up the classes though

play00:29

we'll have class

play00:31

Circle for the time being I'll just

play00:33

write pass we'll fill it in later class

play00:39

square and

play00:41

class

play00:45

triangle for each of these classes in

play00:47

order to instantiate objects we'll need

play00:49

a Constructor we will Define our

play00:51

Constructor our init

play00:54

method when creating circles what sorts

play00:57

of attributes should a circle have let's

play01:00

say a color what's the color of the

play01:03

circle is it filled or not filled will

play01:06

be another attribute and a

play01:09

radius then let's assign

play01:12

these self. color equals the color that

play01:16

we receive

play01:18

self. filed equals

play01:21

filled self. radius equals

play01:26

radius let's do this with the square and

play01:29

triangle really I'll just copy our

play01:31

Constructor and paste it squares don't

play01:34

have a radius with a square the width

play01:37

and the height are the same let's

play01:39

replace radius with width we'll also

play01:42

keep the color and filled attributes

play01:45

self. width equals width now with

play01:49

triangles again let's copy our

play01:51

Constructor we'll need a width and a

play01:56

height self. height equals

play02:00

height so with programming we try not to

play02:03

repeat ourselves if we don't have to

play02:05

what do all of these classes have in

play02:07

common they all share the attributes of

play02:10

color and

play02:13

filled the ways in which they are

play02:15

different is that Circle has a radius

play02:17

attribute square has a width triangle

play02:20

has a width and a height if we have to

play02:22

make any changes to one of these

play02:24

attributes we would have to do so

play02:26

manually for example let's replace

play02:28

filled with isil

play02:30

now I need to look throughout my code

play02:32

for any instance of filled and replace

play02:34

it with is

play02:36

filled it's a lot of work and I might

play02:38

make a

play02:40

mistake such as here and here it's

play02:43

better to write your code once and try

play02:45

and reuse it so that's where inheritance

play02:48

and the super function can come in handy

play02:49

we're going to take the attributes of

play02:51

color and is filled and place it within

play02:53

app parent class these children classes

play02:56

will inherit those

play02:58

attributes so class what do they all

play03:00

have in common they're all shapes class

play03:03

shape and for now I'll write pass circle

play03:07

is going to inherit from its parent of

play03:09

shape that also applies with square and

play03:13

triangle we'll set up a Constructor for

play03:16

shape Define

play03:20

init we will pass in the color and is

play03:27

filled then we will assign these add

play03:30

attributes self. color equals

play03:34

color self. is filled equals is filled

play03:41

we don't need to manually assign these

play03:43

attributes within each of these

play03:44

Constructors for the

play03:46

children instead what we have to do is

play03:49

within the Constructor for each of these

play03:50

children classes we have to call the

play03:53

Constructor for the parent also known as

play03:55

the super class of shape so we will

play03:59

eliminate these two lines of

play04:01

code use the super

play04:04

function dot call The Constructor of the

play04:07

parent that is the dunder init method

play04:11

but we need to pass in the color that we

play04:14

receive and is filled this will be a

play04:18

Boolean and let's do this with the

play04:21

square

play04:22

class and the triangle

play04:25

class we still need radius for the

play04:27

circle width for the square width and

play04:30

height for the triangle we're going to

play04:32

call the super function to take care of

play04:34

whatever our attributes all these types

play04:36

of shapes have in common such as color

play04:39

and is filled now let's see if this

play04:41

works let's construct a few objects we

play04:43

will create a circle named

play04:45

Circle call The Constructor for Circle

play04:48

we have to pass in a color a Boolean if

play04:51

it's filled or not and a radius so for

play04:54

the color of the circle let's say

play04:57

red is filled let's say that is true and

play05:01

a radius of five you could even use

play05:04

keyword arguments for better readability

play05:07

although not necessary but for clarity

play05:09

let's say color equals

play05:11

red is filled equals

play05:14

true radius equals

play05:18

5 let's see if this works I will print

play05:22

our circles

play05:27

color it is red

play05:31

print our colors is filled

play05:34

attribute the circle is filled that is

play05:37

true and the

play05:39

radius print

play05:42

circle. rius the radius of the circle is

play05:46

five we could even convert this to an F

play05:49

string I'll add a

play05:54

placeholder then add centimeters

play05:59

5 cm let's construct a square

play06:03

object square equals

play06:06

Square we'll need a color is filled and

play06:10

a width I'll just copy what we have and

play06:11

make a few changes replace radius with

play06:15

width the color will be blue is filled

play06:19

will be false the width will be six we

play06:22

don't need a height because squares have

play06:24

an even width and height if we ever need

play06:27

the height we can assume it's the same

play06:28

as the width in this case six let's

play06:31

check out our square square do color

play06:34

Square do is filled Square do

play06:39

width our square is blue it's not filled

play06:42

in the width is 6

play06:45

cm let's create a triangle object

play06:48

triangle equals

play06:51

triangle pass in our

play06:54

arguments the color will be yellow is

play06:58

filled will be true

play07:00

the width will be seven and the height

play07:04

will be

play07:05

eight let's print our triangle's color

play07:09

is it filled its width and its

play07:16

height our triangle is yellow it's

play07:19

filled in the width is 7 cm the height

play07:23

is 8

play07:24

cm so that's how you can use the super

play07:26

function to reuse the Constructor of a

play07:29

parent CL class we don't need to

play07:30

manually assign each of these attributes

play07:33

within each of the children classes we

play07:35

can do that in just one place when we

play07:37

refer to Super imagine that we're

play07:39

replacing this with the parent class

play07:42

name such as

play07:44

shape that might be a good way to think

play07:46

of

play07:47

it use the Constructor of the parent

play07:50

class of shape and pass these arguments

play07:54

in what you could do as well is extend

play07:56

the functionality of a method So within

play07:59

our shape class let's create a method of

play08:03

describe we will describe the attributes

play08:06

of this shape we will

play08:09

print use an FST string when we want to

play08:12

describe our shape let's say it is at a

play08:16

placeholder self. color what is the

play08:19

color of this shape and is it fil or not

play08:23

and add a placeholder we'll use a tary

play08:26

operator print filled

play08:30

if self. is filled is true else we will

play08:37

print not

play08:41

filled each of these types of shapes

play08:43

circle square and triangle will have

play08:46

access to a describe

play08:48

method let's attempt to use

play08:50

it take our Circle use the describe

play08:54

method that's

play08:55

inherited it is red and filled Square

play09:00

it is blue and not filled

play09:03

triangle it is yellow and

play09:06

filled so then we also have method

play09:09

overwriting what if we create a similar

play09:11

method of describe within circle square

play09:14

and triangle let's do

play09:17

that

play09:19

Define a describe

play09:23

method within our Circle let's calculate

play09:26

the area what's the area of the circle

play09:29

I'll use an F string it is a

play09:32

circle with an area of then we'll

play09:37

calculate the area given the

play09:40

radius to calculate the area of a circle

play09:42

we can take Pi I'll just say 3.14 just

play09:45

to keep it simple times the radius

play09:48

squared self. rius time self.

play09:53

rius if I were to call the describe

play09:55

method will we use the parents version

play09:58

of describe

play09:59

or the

play10:01

child so let's take our Circle use the

play10:05

describe

play10:07

method the

play10:09

result it is a circle with an area of

play10:12

78.5 I should really add cenim squar

play10:15

after

play10:16

that cm

play10:19

squared this is called method

play10:21

overwriting if a child shares a similar

play10:24

method with a parent you'll use the

play10:27

child's version and not the parents this

play10:29

is Method overwriting if you would like

play10:32

to extend the functionality of a method

play10:34

from a parent you can use the super

play10:37

function not only do I want to use the

play10:39

describe method of the child I would

play10:41

also like to use the describe method of

play10:43

the parent So within this function we

play10:45

will use the super function access the

play10:49

describe method of the

play10:51

parent what we're doing is extending the

play10:54

functionality of the describe

play10:58

method if it is a circle with an area of

play11:01

78.5 cm squared the circle is red and

play11:04

it's

play11:05

filled or you can change up the

play11:10

order let's use the parent classes

play11:12

describe method and extend the

play11:14

functionality with our own print

play11:17

statement it is red and filled it is a

play11:20

circle with an area of 78.5 cm squared

play11:23

let's finish this with the square and

play11:25

triangle classes I'll copy what we have

play11:28

for the described method Within the

play11:29

circle

play11:30

class but we'll make a different

play11:34

calculation describe the square it is a

play11:37

square with an area of take self. width

play11:42

times self.

play11:45

width the height and the width are going

play11:47

to be the same if it's a

play11:49

square then describe our

play11:56

triangle it is a triangle

play11:59

with an area of width time height we

play12:02

have a height in this case divided two

play12:05

we've already described our Circle let's

play12:07

describe our

play12:09

Square it is a square with an area of 36

play12:12

CM squar it is blue and not filled let's

play12:16

describe our

play12:18

triangle it is a triangle with an area

play12:20

of 28.0 CM squar it is yellow and

play12:24

filled all right everybody that is the

play12:26

super function it's used in a child

play12:29

class to call the methods from a parent

play12:31

class also known as the super class it

play12:34

allows you to extend the functionality

play12:36

of the inherited methods within a child

play12:39

class you could use it within a

play12:41

Constructor to assign any attributes

play12:44

that all of its siblings have in common

play12:46

such as color or if that shape is filled

play12:49

when used within any other method you

play12:51

can extend the functionality of that

play12:53

method not only are we printing this

play12:56

message from the parent we're tacking on

play12:58

another PR statement before that and

play13:01

well everybody that is the super

play13:03

function in Python

Rate This

5.0 / 5 (0 votes)

Связанные теги
PythonInheritanceSuper FunctionClass ConstructorMethod OverwritingCode ReusabilityObject-Oriented ProgrammingShape ClassesAttribute AssignmentProgramming Tutorial
Вам нужно краткое изложение на английском?