Don’t worry, you won’t need to do any physical work — the constructor will do it for you.

Posted: November 1st, 2022

Place your order now for a similar assignment and have exceptional work written by our team of experts, At affordable rates

For This or a Similar Paper Click To Order Now

(Please send answer for me in 5 hours. lab 8, I need use terminal to run test and take screenshots for each question. So question all need pass. Thanks )
OOP Example: Car Class
Professor DeNero is running late, and needs to get from San Francisco to Berkeley before lecture starts. He’d take BART, but that will take too long. It’d be great if he had a car. A monster truck would be best, but a car will do — for now…
In, you’ll find a class called Car. A class is a blueprint for creating objects of that type. In this case, the Car class statement tells us how to create Car objects.
Let’s build Professor DeNero a car! Don’t worry, you won’t need to do any physical work — the constructor will do it for you. The constructor of a class is a function that creates an instance, or a single occurrence, of the object outlined by the class. In Python, the constructor method is named __init__. Note that there must be two underscores on each side of init. The Car class’ constructor looks like this:
def __init__(self, make, model):
self.make = make
self.model = model
self.color = 'No color yet. You need to paint me.'
self.wheels = Car.num_wheels
self.gas = Car.gas
The __init__ method for Car has three parameters. The first one, self, is automatically bound to the newly created Car object. The second and third parameters, make and model, are bound to the arguments passed to the constructor, meaning when we make a Car object, we must provide two arguments. Don’t worry about the code inside the body of the constructor for now.
Let’s make our car. Professor DeNero would like to drive a Tesla Model S to lecture. We can construct an instance of Car with ‘Tesla’ as the make and ‘Model S’ as the model. Rather than calling __init__ explicitly, Python allows us to make an instance of a class by using the name of the class.
>>> deneros_car = Car('Tesla', 'Model S')Here, ‘Tesla’ is passed in as the make, and ‘Model S’ as the model. Note that we don’t pass in an argument for self, since its value is always the object being created. An object is an instance of a class. In this case, deneros_car is now bound to a Car object or, in other words, an instance of the Car class.
So how are the make and model of Professor DeNero’s car actually stored? Let’s talk about attributes of instances and classes. Here’s a snippet of the code in with the instance and class attributes in the Car class:
class Car(object):
num_wheels = 4
gas = 30
headlights = 2
size = 'Tiny'
def __init__(self, make, model):
self.make = make
self.model = model
self.color = 'No color yet. You need to paint me.'
self.wheels = Car.num_wheels
self.gas = Car.gas
def paint(self, color):
self.color = color
return self.make + ' ' + self.model + ' is now ' + color
def drive(self):
if self.wheels < Car.num_wheels or self.gas <= 0:
return self.make + ' ' + self.model + ' cannot drive!'
self.gas -= 10
return self.make + ' ' + self.model + ' goes vroom!'
def pop_tire(self):
if self.wheels > 0:
self.wheels -= 1
def fill_gas(self):
self.gas += 20
return self.make + ' ' + self.model + ' gas level: ' + str(self.gas)
In the first two lines of the constructor, the name self.make is bound to the first argument passed to the constructor and self.model is bound to the second. These are two examples of instance attributes. An instance attribute is a quality or variable that is specific to an instance, and not the class itself! Instance attributes are accessed using dot notation (separating the instance and attribute with a period) with an instance. In this case, self is bound to our instance, so self.model references our instance’s model.
Our car has other instance attributes too, like color and wheels. As instance attributes, the make, model, and color of deneros_car do not affect the make, model, and color of other cars.
On the other hand, a class attribute is a quality that is shared among all instances of the class. For example, the Car class has four class attributes defined at the beginning of a class: num_wheels = 4, gas = 30, headlights = 2 and size = ‘Tiny’. The first says that all cars have 4 wheels.
You might notice in the __init__ method of the Car class, the instance attribute gas is initialized to the value of Car.gas, the class attribute. Why don’t we just use the class attribute, then? The reason is because each Car’s gas attribute needs to be able to change independently of each other. If one Car drives for a while, it should use up some gas, and that Car instance should reflect that by having a lower gas value. However, all other Cars shouldn’t lose any gas, and changes to a class attribute will affect all instances of the class.
Dot NotationClass attributes can also be accessed using dot notation, both on an instance and on the class name itself. For example, we can access the class attribute size of Car like this:
>>> Car.size
And in the following line, we access deneros_car’s color attribute:
>>> deneros_car.color
'No color yet. You need to paint me.'
Looks like we need to paint deneros_car!
Let’s use the paint method from the Car class. Methods are functions that are specific to a class; only an instance of the class can use them. We’ve already seen one method: __init__! Think of methods as actions or abilities of objects. How do we call methods on an instance? You guessed it, dot notation!
>>> deneros_car.paint('black')
'Tesla Model S is now black'
>>> deneros_car.color
Awesome! But if you take a look at the paint method, it takes two parameters. So why don’t we need to pass two arguments? Just like we’ve seen with __init__, all methods of a class have a self parameter to which Python automatically binds the instance that is calling that method. Here, deneros_car is bound to self so that the body of paint can access its attributes!
You can also call methods using the class name and dot notation; for example,
>>> Car.paint(deneros_car, 'red')
'Tesla Model S is now red'
Notice that unlike when we painted Professor DeNero’s car black, this time we had to pass in two arguments: one for self and one for color. This is because when you call a method using dot notation from an instance, Python knows what instance to automatically bind to self. However, when you call a method using dot notation from the class, Python doesn’t know which instance of Car we want to paint, so we have to pass that in as well.
Professor DeNero’s red Tesla is pretty cool, but he wants a bigger car! How about we create a monster truck for him instead? In, we’ve defined a MonsterTruck class. Let’s look at the code for MonsterTruck:
class MonsterTruck(Car):
size = 'Monster'
def rev(self):
print('Vroom! This Monster Truck is huge!')
def drive(self):
Wow! The truck may be big, but the source code is tiny! Let’s make sure that the truck still does what we expect it to do. Let’s create a new instance of Professor DeNero’s monster truck:
>>> deneros_truck = MonsterTruck('Monster Truck', 'XXL')Does it behave as you would expect a Car to? Can you still paint it? Is it even drivable?
Well, the class MonsterTruck is defined as class MonsterTruck(Car):, meaning its superclass is Car. Likewise, the class MonsterTruck is a subclass of the Car class. That means the MonsterTruck class inherits all the attributes and methods that were defined in Car, including its constructor!
Inheritance makes setting up a hierarchy of classes easier because the amount of code you need to write to define a new class of objects is reduced. You only need to add (or override) new attributes or methods that you want to be unique from those in the superclass.
>>> deneros_car.size
>>> deneros_truck.size
Wow, what a difference in size! This is because the class attribute size of MonsterTruck overrides the size class attribute of Car, so all MonsterTruck instances are ‘Monster’-sized.
In addition, the drive method in MonsterTruck overrides the one in Car. To show off all MonsterTruck instances, we defined a rev method specific to MonsterClass. Regular Cars cannot rev! Everything else — the constructor __init__, paint, num_wheels, gas — are inherited from Car.

For This or a Similar Paper Click To Order Now

Expert paper writers are just a few clicks away

Place an order in 3 easy steps. Takes less than 5 mins.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price: