Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: Motorcycle Class, Untyped

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In addition to being really passionate about programming in Python, I'm also really into motorcycle writing and So we're gonna use
0:08 Motorcycles as the basis for a lot of our demos in this course when I talk about our adventure motorcycle and other kinds of motorcycles
0:16 This is great for typing. This is great for inheritance
0:19 You know motorcycles can be vehicles, but can the cars also fit into that object hierarchy? We'll see kind of yes kind of no
0:26 So we're going to go and write our first demo, this is going to be just plain old school
0:33 untyped Python, you know, this kind you might have written before taking this course or certainly before Python three, five came along.
0:41 So here in PyCharm, we now have a pi no types. So this is the old school original style of Python.
0:49 And what we're going to do is we're just going to write some code here, play with some ideas.
0:53 And then, as we go through this chapter, we're going to add types to it, compare it to TypeScript, C#, Swift, and those kinds of things.
1:02 So let's get started. We have a class motorcycle. And this class is going to have an initializer a constructor.
1:11 We need to pass some information to it because not all motorcycles are the same.
1:15 Some have some are off road, some are on road, some are both have different size engines.
1:22 So we'll start with a model, a style, engine size in cubic centimeters, and whether or not it can go off-road.
1:34 So first of all, let's just look at this here. What do these things mean? So model, that's probably clear.
1:41 We should be safe to go ahead and add that to our code here, to our class.
1:45 model. This is like Yamaha Tenere KX250 whatever right KX maybe would be the right thing then
1:53 250 would be the engine size right. Style though is that a string is that an enumeration.
2:01 We don't know. This is just a dynamic language so we're just going to hang on to it there
2:04 and that's fun. Engine size probably probably an integer but it could it be a float. Are Are there partial CCs measured in these things?
2:14 I'm not sure that there are, but maybe there are. We don't know. It's probably some kind of number thing. Let's add that.
2:19 Off-road, yes, no, true, false. I don't know. We'll just add that as well.
2:25 All right, so here's our motorcycle, and it's something that we can create, right?
2:29 We could go and say MC equals motorcycle, and PyCharm will say, okay, it takes a model like a Tenere, the style be Adventure, let's say.
2:40 size of 700. And true. Now, now that we're using it, we get a lot more information about
2:47 what the types we might expect here, assuming this usage is correct. Okay, so string string
2:53 number, Boolean integer. Right, so but just looking at the code up here, you don't know
2:59 that there's nothing at all about what's up there other than your expectations, hopefully
3:03 some good naming, but you know, no guarantees there. So we're going to go with this and
3:10 fill it out a little bit more. Let's ask the question whether it can jump. Is this a motorcycle
3:15 that can jump? Yes or no. Street bikes, while technically you might be able to make them
3:19 jump is probably a bad idea. So let's suppose a motorcycle jumps if it's off road. So we
3:24 can create a property here just can jump. And let's just say it can jump if and only
3:31 if offroad is true. So we'll say self dot offroad. If we know this is actually a Boolean,
3:37 this is fine. It's not we might want to, you know, wrap that into a Boolean condition or
3:43 check offroad equal equal Yes, we don't know really what we need to do yet, because we're
3:48 lacking the type information to be very clear about that. But this is a decent start. We're
3:53 going to be printing out a lot of motorcycles. So let's give it a stir representation. We'll
3:59 just say use a couple of strings model like in this case, 10 array 700 type adventure off road, I don't know, can jump true or false.
4:10 Okay, so that's going to be this piece. And then the last thing let's because we want to work with different aspects, you can see
4:16 here's a standard function. Here's a initializer type function. Here's a property, let's get one more type of functions that we might work with here
4:26 and we'll make a class method and we'll call this createAdventure. It's going to be a shortcut from what we normally pass up here.
4:37 So let's go along and copy that. The style is always going to be adventure, so we don't need to specify that.
4:43 We'll just say return motorcycle of model and then what comes next, the style, so adventure. I don't like that I have to keep typing this.
4:53 What if I misspell it? Is this always uppercase? lowercase, we don't know that's a problem. Engine size and off road. Yes or no. Actually,
5:03 I would say this is always true. So we're going to pass just those two things in. So
5:07 it's a nice simple way to create a simpler one. And it's a static method or a class method,
5:11 which is like a static method effectively. So this is our motorcycle class here. Right?
5:17 Again, we already explored like we don't know really how it's supposed to be used, we might
5:21 have a way in mind as we're creating it what these types are supposed to be, but there's not information that anyone consuming it would know about.
5:29 But this is standard, untyped, traditional, OG, original Python.


Talk Python's Mastodon Michael Kennedy's Mastodon