Python abstract classes 👻

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • Python abstract class example tutorial explained
    #python #abstract #classes
    #abstract class = a class which contains one or more abstract methods.
    #abstract method = a method that has a declaration but does not have an implementation.
    prevents a user from creating an object of that class
    + compels a user to override abstract methods in a child class
    from abc import ABC, abstractmethod
    class Vehicle(ABC):
    @abstractmethod
    def go(self):
    pass
    @abstractmethod
    def stop(self):
    pass
    class Car(Vehicle):
    def go(self):
    print("You drive the car")
    def stop(self):
    print("This car is stopped")
    class Motorcycle(Vehicle):
    def go(self):
    print("You ride the motorcycle")
    def stop(self):
    print("This motorcycle is stopped")
    #vehicle = Vehicle()
    car = Car()
    motorcycle = Motorcycle()
    #vehicle.go()
    car.go()
    motorcycle.go()
    #vehicle.stop()
    car.stop()
    motorcycle.stop()
    music credits 🎼 :
    ===========================================================
    Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
    Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
    Free Download / Stream: bit.ly/2JnDfCE
    Music promoted by Audio Library • Up In My Jam (All Of A...
    ===========================================================

Комментарии • 151

  • @BroCodez
    @BroCodez  3 года назад +45

    TLDR
    # Prevents a user from creating an object of that class
    # + compels a user to override abstract methods in a child class
    # abstract class = a class which contains one or more abstract methods.
    # abstract method = a method that has a declaration but does not have an implementation.
    from abc import ABC, abstractmethod
    class Vehicle(ABC):
    @abstractmethod
    def go(self):
    pass
    @abstractmethod
    def stop(self):
    pass
    class Car(Vehicle):
    def go(self):
    print("You drive the car")
    def stop(self):
    print("This car is stopped")
    class Motorcycle(Vehicle):
    def go(self):
    print("You ride the motorcycle")
    def stop(self):
    print("This motorcycle is stopped")
    #vehicle = Vehicle()
    car = Car()
    motorcycle = Motorcycle()
    #vehicle.go()
    car.go()
    motorcycle.go()
    #vehicle.stop()
    car.stop()
    motorcycle.stop()

  • @shrikanthsingh8243
    @shrikanthsingh8243 Год назад +60

    Your intentions are clear - Simply educate and not show off smartness by complicating the content, which other channels do. Keep up your work.

  • @forrestelliott3267
    @forrestelliott3267 2 года назад +43

    This was really helpful, thank you. Though I think at 4:57 the error arose from the vehicle = Vehicle() line, not from the motorcycle = Motorcycle() line, as your commentary implies at that moment.

  • @yuilhan4275
    @yuilhan4275 3 года назад +9

    Thank you, your explanations are clear and is easy enough to understand.

  • @yevhenliubarskyi1841
    @yevhenliubarskyi1841 2 года назад +7

    Thank you Bro, your explanations are just crystal clear. I've already watched several of your videos. Hope you achieve great success with your channel.

  • @guardmanbob
    @guardmanbob 3 года назад +6

    straight to the point, clear, and fun to watch. will binge all ur videos

  • @ravikanthduvvuri392
    @ravikanthduvvuri392 2 года назад +4

    I got best content thanks for your best class bro☺️☺️

  • @zacky7862
    @zacky7862 2 года назад +2

    Thanks. Finally I understand now the importance usage of abstract

  • @Ronnie9P
    @Ronnie9P 2 года назад +1

    best explanation of abstraction in python on youtube

  • @trippingwithcrujo7708
    @trippingwithcrujo7708 Год назад +1

    Really simple and straight to the point. Makes it easier to understand more complicated applications later on. Good job!

  • @גייסוןחן
    @גייסוןחן 2 месяца назад

    helped a lot on the basic understanding of the abstract class, which is exactly what I needed for my study, thank you! :)

  • @hooman3669
    @hooman3669 2 месяца назад

    Loving these short conceptual videos 😁😁❤❤

  • @user-mq9hf1mm9f
    @user-mq9hf1mm9f 5 месяцев назад

    Omg, can I just say you are saving my life rn? Thank you for this clear and concise lesson - thank you SO much.

  • @davidstefanov9210
    @davidstefanov9210 3 года назад +1

    Thank you for the video dude,It was very helpful and informative. BTW my Teacher linked this video in our Python course as a example

  • @andrewv8350
    @andrewv8350 28 дней назад

    Now, it's clear for me. Thanks a lot, bro!

  • @otsapin
    @otsapin 3 года назад +2

    Great tutorial! Thanks a lot, bro! ;)

  • @jackb4223
    @jackb4223 3 года назад +1

    Nice brief description of the essentials -- thanks!

  • @erickonyanda5570
    @erickonyanda5570 3 года назад +1

    Thank you very much. Wow!! Very simple, very easy, very clear explanation

  • @cesarosantini708
    @cesarosantini708 3 года назад +2

    excellent explanation
    keep it up

  • @Techieinla
    @Techieinla 3 года назад +3

    very understandable

  • @RajatSadanandan
    @RajatSadanandan 3 года назад +2

    Really helpful! Thanks!!

  • @akramal-fageeh8969
    @akramal-fageeh8969 2 месяца назад

    Really nice, concise, and clear

  • @roxykvarman8773
    @roxykvarman8773 3 года назад +2

    Thanks a lot! Great explanation! Subscribed and going to take your python and javascript crash courses!

  • @svensalvatore8702
    @svensalvatore8702 Год назад

    This is soo Good of a video and explanation. Thank you, sir!

  • @softwareengineer8923
    @softwareengineer8923 3 месяца назад

    Awesome tutorial, thanks

  • @83sal
    @83sal 3 года назад +2

    Thank you sir!

  • @davidrowlands8548
    @davidrowlands8548 2 года назад

    Thank you I was able to understand this concept thanks to your video.

  • @anfedres
    @anfedres 3 года назад +2

    cool video bro... ps: Turn of the cc... it is hilarious!

  • @d1jn
    @d1jn 2 года назад +1

    Great explanation bro! Thanks!

  • @beingzero7541
    @beingzero7541 2 года назад +1

    Wow!

  • @simeonbogg3725
    @simeonbogg3725 2 года назад

    Great explanation an example, very easy to understand!

  • @linatroshka
    @linatroshka 2 года назад

    Very clear and concise explanation. Thank you very much for this!

  • @Franco-ss4bu
    @Franco-ss4bu 8 месяцев назад

    I'm really liking your python tutorials

  • @nirutg5130
    @nirutg5130 Год назад

    I find 6-7 sources from internet to understand about Python abstraction but not understand!, just understand from this video. Thank you so much Bro.

  • @horoshuhin
    @horoshuhin 2 года назад

    this is such a concise and to-the-point explanation of ABC. I've yet to find a better one. and probably don't need to anyway. thank you

  • @Muertedertod
    @Muertedertod 2 года назад

    finally someone that is able to explain this clear and short! THX a lot!

  • @liasantos6639
    @liasantos6639 7 месяцев назад

    Thank you for this video! Amazing explanation

  • @henriquefelinimena7433
    @henriquefelinimena7433 3 года назад +2

    Thank you Bro!

  • @timhiggins7072
    @timhiggins7072 10 месяцев назад

    Bless you for making this more clear than my professor.

  • @stevechesney9334
    @stevechesney9334 Год назад

    Nice job! Easy to follow examples!

  • @myoutube8832
    @myoutube8832 5 месяцев назад

    Great discussion Bro

  • @pablocesarlopez7379
    @pablocesarlopez7379 Год назад

    A very clear explanation. Thanks!

  • @user-cd4zb3gk3z
    @user-cd4zb3gk3z 2 месяца назад

    Спасибо, видео помогло разобраться!

  • @lovelyaashik9732
    @lovelyaashik9732 2 года назад +3

    Sir basically what's the use of it , for what. Purpose we're going to use it

  • @nishantrandev9633
    @nishantrandev9633 2 года назад

    Great video. To the point. That's how the content should be created. Thanks!

  • @sterlingevans129
    @sterlingevans129 Год назад +2

    Can someone explain the reason an abstract class would be needed if the child class' methods would be explicitly written anyway?

  • @sirenhanced6248
    @sirenhanced6248 Год назад

    Thanks so much I have a test for my Python OOP course tomorrow on this you saved me.

  • @priyanshagarwal1565
    @priyanshagarwal1565 5 месяцев назад

    You teach nice

  • @hrushikeshsahasrabuddhe5103
    @hrushikeshsahasrabuddhe5103 9 месяцев назад

    Simple & to the point!

  • @hiiexisthiiexist1284
    @hiiexisthiiexist1284 Год назад

    awesome video

  • @streamindigoflags8035
    @streamindigoflags8035 10 месяцев назад

    Hi bro nice video

  • @woltron4o
    @woltron4o 2 года назад

    Very nice and useful video

  • @kemann3815
    @kemann3815 2 года назад

    Lovely

  • @macieks3604
    @macieks3604 Год назад

    Nicely explained! Thanks :)

  • @chriskeo392
    @chriskeo392 Год назад

    More content on abstraction and encapsulation while using dataclasses please

  • @cloudroot7
    @cloudroot7 2 года назад

    It's easy to understand the concept.

  • @digosantista
    @digosantista 2 года назад

    Cool. But it would be nice to comment on the video (or do a voice over) to let people know that error arouse from Vehicle being instatiated not from implementation itself.
    Other than that, great! Thanks!

  • @kreglfromworld
    @kreglfromworld 2 года назад +1

    Why not skip the abstract class completely? You're writing all the code again anyway? What am I missing?

  • @pun15h3r.
    @pun15h3r. 4 месяца назад

    quick and dirty.
    nice, thanks!

  • @user-ew7ri4gk2l
    @user-ew7ri4gk2l 4 месяца назад

    You are very clear and objective. But I can't understand the use of abstract class if you can't instantiate it and the same functions are written bellow in other classes???
    Thanks in advance.

  • @tarikyassa1076
    @tarikyassa1076 3 года назад +1

    Thanks Bro!

  • @SmokyDesperado91
    @SmokyDesperado91 2 года назад +1

    Thank you, great explanation! I have one question though: is there some way to create abstract attributes in analogy to abstract methods? I want to make sure that those attributes will be implemented in all child classes

  • @joelpaquete4152
    @joelpaquete4152 2 года назад

    Thank you for this lessons

  • @samiam.402
    @samiam.402 2 года назад +5

    I might've missed it, did he explain why you don't have to "super()" the methods you are overriding? Is this an inherent part of an abstract class, or am I missing something?

    • @Craulback
      @Craulback 2 года назад +3

      super() gets the child class access to the attributes from the parents method, it doesn't override anything, but is useful when your child class needs the same attributes, plus some new one.
      class Child(Parent)
      def __init__(self,attribute_1,attribute_2,attribute_3)
      super().__init__(attribute_1,attribute_2) # this will pull the first 2 attributes (of the same name) from the parent
      as for overriding, just defining the child's method using the same name as the one in the parent will override it for the child class.
      it looks for the "go()" method in the child class, before looking for the one from the parent.
      so it never actually encounters the abstract version of the method.

    • @sebastianradlmeier8922
      @sebastianradlmeier8922 2 года назад +1

      ​@@Craulback Thanks for the explanation

  • @MrPimleemans
    @MrPimleemans Год назад

    Excellent explanation, thanks!

  • @user-yu1dc7su2r
    @user-yu1dc7su2r 11 месяцев назад

    Thanks alot.🙏

  • @abdullahnasir8535
    @abdullahnasir8535 10 месяцев назад

    You are just amazing bro

  • @kerry7932
    @kerry7932 9 месяцев назад

    Nice video. I think we need some additional information about the purpose of abstract classes as many of the comments have indicated. That is, is if subclasses cannot inherit from their parent, what is the purpose of the parent?

  • @shingaling3315
    @shingaling3315 Год назад +1

    I understand the implementation but what is the purpose of doing this? I’m not following what benefit was gained by going through this versus not defining an abstract class at all

  • @theverynvg
    @theverynvg Год назад

    It's amazing!Thank you

  • @reshmi003
    @reshmi003 Год назад +1

    Thanks alot ❤️

  • @Amirkolo
    @Amirkolo 6 месяцев назад

    you are a genius bro. thanks 🙂

  • @sahilsagwekar
    @sahilsagwekar Год назад +2

    thanks bro

  • @chillydoog
    @chillydoog 10 месяцев назад

    for the algorithms. ty brother

  • @SuccessXcel_
    @SuccessXcel_ 11 месяцев назад

    Best explanation.

  • @jhassee
    @jhassee Год назад

    supporting the channel

  • @SuperMaker.M
    @SuperMaker.M 8 месяцев назад

    great video !

  • @TheHansdampf80
    @TheHansdampf80 7 месяцев назад

    Thank you

  • @santosbogo
    @santosbogo Год назад

    I am doing python at university and this is the best video Ive ever watch

  • @igordjokic
    @igordjokic Год назад

    NIce!

  • @MystikPrae
    @MystikPrae Год назад

    from abc import ABC,abstractmethod
    class Animal(ABC):
    @abstractmethod
    def eat(self):
    print("This is eating something")
    class Rabbit(Animal):
    def eat(self):
    print("This is eating grass")

    obj=Rabbit()
    obj.eat()

  • @rubenc4696
    @rubenc4696 4 месяца назад

    Thanks

  • @soklin
    @soklin Год назад

    Thank you!!!

  • @amenkalai8442
    @amenkalai8442 Год назад +1

    I didn't get this one? what's the point of using abstract classes? why are we preventing a class from creating an object ?? how can this help us and benefits us in real life projects ?

    • @HTWwpzIuqaObMt
      @HTWwpzIuqaObMt 2 месяца назад

      I will present you a practical example: Lets say you are working with sockets and you find that your server class has some common functionality with your client class. You would create an abstract base class which encapsulates the common functionalities of the two classes and defines the required classes for the socket. Ex. A start_socket function and a handle_connection function. These are abstract methods which will be implemented by the children of the class appropriately. Use abstract methods to outline how a child class should work

  • @xBaphometHx
    @xBaphometHx Год назад

    Clear as water!!!

  • @abdallahashraf9940
    @abdallahashraf9940 3 года назад +1

    i'm sorry... beginner question :
    what would be the difference between an abstract class and an interface?

    • @ShubhamSharma-sx2kd
      @ShubhamSharma-sx2kd 3 года назад

      "Classes that are inherited from can still be instantiated
      Classes that are abstract cannot be instantiated, which adds some security"

    • @abdallahashraf9940
      @abdallahashraf9940 3 года назад

      @@ShubhamSharma-sx2kd and the interfaces ?

  • @HussainAli-sb1dv
    @HussainAli-sb1dv 10 месяцев назад

    love u

  • @jimenadelgado7384
    @jimenadelgado7384 4 месяца назад

    thaaaanks

  • @omercagla8970
    @omercagla8970 Год назад

    Best tutorial I have ever seen

  • @i_am_a_real_cat1443
    @i_am_a_real_cat1443 3 года назад +3

    Can you explain me where do i need this an ABC class?

  • @EissaAlahdul
    @EissaAlahdul 2 года назад

    Thanks!

  • @nazarsilchenko8326
    @nazarsilchenko8326 2 года назад

    Instant like for mentioning the need for speed:)

  • @marianotorres4052
    @marianotorres4052 Год назад

    thank you!

  • @jhontriboyke5534
    @jhontriboyke5534 5 месяцев назад

    thank you bro !

  • @shavindasilva
    @shavindasilva 2 года назад

    best BRO :)

  • @lw9954
    @lw9954 2 года назад

    ty bro

  • @joetaff5960
    @joetaff5960 2 года назад

    great explanations but how is this actually used in a real world situation? I assume this would be a tiny chunk of massive code. I've watched all 47 videos up to this point and understand them all but rarely do they truly build on each other as a whole. I hope the rest of the programs in the latter half solve this issue.

  • @Bro-cc8pw
    @Bro-cc8pw Месяц назад

    What's up bro

  • @harithabandara3212
    @harithabandara3212 Год назад

    Thank youu!!!

  • @hangchen
    @hangchen 10 месяцев назад

    Thx bro!