SUPER() in Python explained! 🔴

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ค. 2024
  • super() = Function used in a child class to call methods from a parent class (superclass).
    Allows you to extend the functionality of the inherited methods
    class Shape:
    def __init__(self, color, is_filled):
    self.color = color
    self.is_filled = is_filled
    def describe(self):
    print(f"It is {self.color} and {'filled' if self.is_filled else 'not filled'}")
    class Circle(Shape):
    def __init__(self, color, is_filled, radius):
    super().__init__(color, is_filled)
    self.radius = radius
    def describe(self):
    print(f"It is a circle with an area of {3.14 * self.radius * self.radius}cm^2")
    super().describe()
    class Square(Shape):
    def __init__(self, color, is_filled, width):
    super().__init__(color, is_filled)
    self.width = width
    def describe(self):
    print(f"It is a square with an area of {self.width * self.width}cm^2")
    super().describe()
    class Triangle(Shape):
    def __init__(self, color, is_filled, width, height):
    super().__init__(color, is_filled)
    self.width = width
    self.height = height
    def describe(self):
    print(f"It is a triangle with an area of {self.width * self.height / 2}cm^2")
    super().describe()
    circle = Circle(color="red", is_filled=True, radius=5)
    square = Square(color="blue", is_filled=False, width=6)
    triangle = Triangle(color="yellow", is_filled=True, width=7, height=8)
    circle.describe()
    square.describe()
    triangle.describe()

ความคิดเห็น • 26

  • @BroCodez
    @BroCodez  หลายเดือนก่อน +1

    # super() = Function used in a child class to call methods from a parent class (superclass).
    # Allows you to extend the functionality of the inherited methods
    class Shape:
    def __init__(self, color, is_filled):
    self.color = color
    self.is_filled = is_filled
    def describe(self):
    print(f"It is {self.color} and {'filled' if self.is_filled else 'not filled'}")
    class Circle(Shape):
    def __init__(self, color, is_filled, radius):
    super().__init__(color, is_filled)
    self.radius = radius
    def describe(self):
    print(f"It is a circle with an area of {3.14 * self.radius * self.radius}cm^2")
    super().describe()
    class Square(Shape):
    def __init__(self, color, is_filled, width):
    super().__init__(color, is_filled)
    self.width = width
    def describe(self):
    print(f"It is a square with an area of {self.width * self.width}cm^2")
    super().describe()
    class Triangle(Shape):
    def __init__(self, color, is_filled, width, height):
    super().__init__(color, is_filled)
    self.width = width
    self.height = height
    def describe(self):
    print(f"It is a triangle with an area of {self.width * self.height / 2}cm^2")
    super().describe()
    circle = Circle(color="red", is_filled=True, radius=5)
    square = Square(color="blue", is_filled=False, width=6)
    triangle = Triangle(color="yellow", is_filled=True, width=7, height=8)
    circle.describe()
    square.describe()
    triangle.describe()

  • @omisolveejaym.8885
    @omisolveejaym.8885 หลายเดือนก่อน +2

    Really love your videos man!! Can you make a video about API? It is something I couldn't get my head into and I really love how you explain stuff, I might be able to grasp it this time. Thanksss❤️

  • @S9J4
    @S9J4 หลายเดือนก่อน +3

    Just a simple question, why your every video is a fundraiser and for what?

  • @Hasan10-oh7vl
    @Hasan10-oh7vl หลายเดือนก่อน

    Loveee ittt !!!
    Do you need a video editor?
    I can do a sample video ;)

  • @William23-dt1mr
    @William23-dt1mr หลายเดือนก่อน +1

    Please continue the React course bro :(

  • @lokeshkumaryadav2254
    @lokeshkumaryadav2254 หลายเดือนก่อน

    Pls make a series on flutter

  • @sam_cs_49er
    @sam_cs_49er หลายเดือนก่อน

    Can u do this subject (oop) for JavaScript?

  • @isdispvp__
    @isdispvp__ หลายเดือนก่อน +1

    first to comment

  • @osamapro4135
    @osamapro4135 หลายเดือนก่อน

    Hello im new how i can start

  • @MarwaRanya-ek6re
    @MarwaRanya-ek6re หลายเดือนก่อน

    Code father

  • @ratulmitra347
    @ratulmitra347 หลายเดือนก่อน

    name = input("enter your name: ")
    gender = input("enter your gender: ")
    country = input("enter the country: ")
    print(f"welcome to {country}, {gender} {name}")
    I want to put Mr. Or Mrs. Depending on the gender using if else inside the print() function but couldn't able to do it.... Can you please help me??? Thanks

    • @MAD-SKILLZ
      @MAD-SKILLZ หลายเดือนก่อน +1

      Here's something I worked up for you:
      name = input('...')
      gender = input('...')
      country = input('...')
      if 'male' in gender:
      title = 'Mr.'
      elif 'female' in gender:
      title = 'Mrs.'
      else:
      title = ''
      print(f'Welcome to {country}, {title} {name}!')

    • @ratulmitra347
      @ratulmitra347 หลายเดือนก่อน

      @@MAD-SKILLZ thanks... I did it too.... Actually I wanted to use if else within print function.... Maybe it's not possible... Anyway thanks again

    • @MAD-SKILLZ
      @MAD-SKILLZ หลายเดือนก่อน +1

      @@ratulmitra347 Actually, it is possible, it just doesn't look very nice:
      name = input('...')
      gender = input('...')
      country = input('...')
      print(f'Welcome to {country}, {"Mr." if gender == "male" else ("Mrs." if gender == "female" else "")} {name}!')
      (Also, I made a mistake in my first comment. You should use '==' instead of 'in' because the substring 'male' is also in the string 'female'. Opps haha)

    • @ratulmitra347
      @ratulmitra347 หลายเดือนก่อน

      @@MAD-SKILLZ name = input("enter your name: ")
      gender = input("enter your gender: ")
      country = input("enter the country: ")
      if gender=='male':
      print(f"welcome to {country}, Mr. {name}")
      else:
      print(f"welcome to {country}, Mrs. {name}")
      I did this

    • @ratulmitra347
      @ratulmitra347 หลายเดือนก่อน

      @@MAD-SKILLZ yes it doesn't look cool but I didn't know that if else can be used inside print function.... When, a lot of time ago, I used c and c++ I didn't ever use if else inside printf function.... So that is why I wanted to do it... It looked weird but at the same time little bit cool to me.... Thanks for solving it though.... Now I need to understand how it all works