Python for the .NET Developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Concept: Defining classes in Python

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Time to review creating a base class or super class in Python. Our car class was our super class and we wanted to be abstract so here
0:10 we derive from ABC, Abstract Based Class. Has a couple of features has a constructor and C# terminology, an initializer or sometimes referred to as
0:19 dunder init, as in __init__ Though dunder init, you'll hear that a lot dunder methods for this class of methods. And these are the special built-ins
0:31 like the operator stuff I showed you and so on. So we are going to create one of those and it is going to take whatever
0:36 data we have to pass to the class to get it started model name, engine type, cylinders and base price. We went ahead and used the typing to
0:42 make that really obvious. And to define 'fields' by the same names we are going to say self.model_name self.base_price, self.cylinders.
0:52 Now, we didn't talk about this in the demo but I'm going to mention it here. IF you want this to be a private field
0:57 use the double underscore at the beginning. It is a weird convention Python is all about the underscores and sometimes they are just separators
1:06 like base price. Other times they have important meaning. If the underscore is a single underscore in the beginning it's still publicly visible
1:13 the field or the method that you gave that name but it is indicated, like its you probably should stay away from it. It's kind of an internal thing
1:20 leave it alone. You put a double underscore the runtime will actually change it's value so you effectively can't simply can't easily
1:28 without jumping through a bunch of hoops you can't get to it from the outside. So when you say car.__engine_type should not appear.
1:34 Depending on your editor, but certainly car.__engine_type and working with it won't work from the outside.
1:39 We also defined a method drive, we're overriding. This one is a virtual, it has the ability to be overridden, but we don't say virtual.
1:49 Basically, imagine everything is virtual in Python. We have a property we used a @property decorator to indicate
1:56 that this is meant to act like a computed value not like a function. We also have an abstract method refuel which we indicate with
2:03 the @abstractmethod decorator. This is our base class. If we are going to use it, we just derive from it. class ElectricCar(Car).
2:12 Now we are deriving from it. Here we create a specialized, initialized initializer __init__ that takes only two values, model name and price
2:20 and then it calls the superclasses one and always passes electric and zero so you don't have to worry about that when you use the ElectricCar.
2:27 It overrides drive, and it overrides and must override, refuel. On both of those, there is no key words around it. You just do it, and it happens.
2:37 It's easy to forget, but you really need to call super.__init__, there is no implicit constructor chaining. Like, if I just create a constructor
2:45 it's not going to automatically call the default constructor the bass class, and it's bass class's default constructor.
2:52 And so on. So make sure you always remember to call the bass class init in your initializer. You saw that Pycharm helped out with that as well.
3:01 Overridden methods, you need no modifiers. There's not like override and virtual or anything like that.
3:06 But, as we saw, you must implement the abstract ones or you cannot create an instance of this class. It in itself will be effectively abstract as well.


Talk Python's Mastodon Michael Kennedy's Mastodon