Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Python class properties

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Properties are a wonderful addition to classes.
0:03 Older versions of object oriented programming
0:05 like C++ and Java in the early days
0:08 you had to have a get value and a set value
0:11 or a get value if it was computed.
0:13 For example, here we have a boolean property
0:16 which is whether or not the car's electric
0:19 and we can compute that based off of the other information.
0:22 So here's our read-only computed property, that we can use
0:24 and then over here we can say car.is_electric. And this is great
0:30 we treat it like a field, like it's just data
0:33 and we don't have to treat it like it's behavior.
0:35 Especially if you're modifying it
0:37 doing like ++ or something like that.
0:39 Obviously it doesn't make sense for booleans
0:41 but you know, if it was a number or something like that.
0:44 Can we do that in Python? How do we do it?
0:46 Well, we don't have this get, set syntax
0:49 but we do have the ability to have a property.
0:52 Let's do that in the very base car, here.
0:56 And it has to do with these decorators.
0:57 So, the way we use a decorator is to change what this does.
1:00 We also can use a decorator in a shortcut as well
1:05 to create a method that is actually a property.
1:08 Same thing that we had in C#.
1:10 Let's go, right is_electric, now we can compare.
1:14 It will return self.engine_type == 'electric'.
1:21 That's it, same thing.
1:22 So over here we have, going to say this is a read
1:25 like, this implements the get, basically.
1:27 The set is a little bit different.
1:29 It implements something that we can treat like a field
1:32 or like data, but is actually a method that runs.
1:34 So let's, over here, and we'll just print out.
1:47 We'll say, the car.model_name is electric question mark
1:50 and then we'll just print out this property, like that.
1:53 And we run this, and bet is going to say
1:56 you know, False, False, True, True,
1:58 or something to that effect.
2:00 Like, Corvette is electric, false.
2:02 Winstar is electric, false.
2:03 Tesla electric, true.
2:05 Bolt electric, true.
2:07 How cool is that? So just like we have properties in C#
2:10 exactly the same thing, we have
2:12 properties over here in these classes.
2:14 And you define them like this.
2:16 You say, app property on a regular method.
2:19 That defines the get.
2:20 Obviously, it has to be a void method, right?