Python object oriented programming (OOP) -- importing classes

We talked about the method of importing modules earlier( How to import modules? )In fact, a module is a python file in the same root directory, and so is a class. Python allows you to store classes in modules, and then import the required modules in the main program

catalogue

1, Import a single class

2, Storing multiple classes in a module

3, Importing multiple classes from a module

IV. import the whole module

5, Import all classes in the module

1, Import a single class

The following is a class of a car that has been defined. The Python file is named car py

class Car:

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.orometer_reading = 0

    def get_description(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name

    def read_odometer(self):
        print("This car has "+ str(self.orometer_reading) + " miles on it")

    def update_orometer(self,miles):
        if miles >= self.orometer_reading:
            self.orometer_reading = miles
        else:
            print("You can'troll back an odometer")

    def increase(self,miles):
        self.orometer_reading +=miles

Next, let's create another one named my_car.py, then import the Car class and create an instance

from  car import  Car #Import the Car class in the Car module

my_new_car =Car("audi","a6", 2017)
print(my_new_car.get_description())

my_new_car.orometer_reading = 25
my_new_car.read_odometer()

Now review the program we wrote, if we put the Car class into my_car.py this file, so how long does my Python code have to be!

Next, let's look at what we wrote_ Car. Py, that is, the second piece of code. The above import statement opens the module Car and imports the Car class, so that we can use the Car class. Just as it is defined in the file, the output is the same as what we see.

By moving the class into the module and importing it, you can still use the functions in it, so we improve the readability of our program

2, Storing multiple classes in a module

We directly add more classes to the above module

class Car:

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.orometer_reading = 0

    def get_description(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name

    def read_odometer(self):
        print("This car has "+ str(self.orometer_reading) + " miles on it")

    def update_orometer(self,miles):
        if miles >= self.orometer_reading:
            self.orometer_reading = miles
        else:
            print("You can'troll back an odometer")

    def increase(self,miles):
        self.orometer_reading +=miles

class Battery():
    #An attempt to simulate electric vehicle charging
    def __init__(self,battery_size = 70):
        #Initialize electrical frequency properties
        self.battert_size = battery_size

    def describe_battery(self):
        print("This car has a "+ str(self.battert_size) + " -kwh battery")

    def get_range(self):
        #Print a message describing the battery range
        if self.battert_size == 80:
            range = 260
        elif self.battert_size == 85:
            range = 270

    message = "This kind of car can go approximately " + str(range)
    message +=" miles on a full charge"
    print(message)

class ElectricCar(Car):
    #Special features of simulated electric vehicles

    def __init__(self,make,model,year):
        super().__init__(make,model,year)#super is a special function that helps Python associate parent and child classes
        self.battery = Battery() #A new battery class is defined here

Let's create a new python file named my_eletric_car.py

from car import ElectricCar

my_car = ElectricCar("tesa","model's",2019)
print(my_car.get_description())
my_car.battery.describe_battery()
my_car.battery.get_range()

Does this program make people more comfortable?

3, Importing multiple classes from a module

Let's create a Python file named: my again_ cars. Py, the next thing we need to do is import Car and ElectricCar classes at one time

from car import Car,ElectricCar #Class import is the same as function import

my_beetle = Car("Volkswagen",'beetle',2019)
print(my_beetle.get_description())

my_tesla = ElectricCar('tesla','roadster',2019)
print(my_tesla.get_description())

IV. import the whole module

We're directly at my_ cars.py file. We directly import the module and then access the methods in the class

import car  #Import the whole module at one time

my_beetle = car.Car("Volkswagen",'beetle',2019)
print(my_beetle.get_description())

my_tesla = car.ElectricCar('tesla','roadster',2019)
print(my_tesla.get_description())

5, Import all classes in the module

This method is not recommended because the Python interpreter (pychar) will prompt you which classes you can choose during the process of importing classes above. This method of importing may cause problems that are difficult to detect due to duplication with the file name. The import method is as follows

from module_name import *
preferably
from module_name.class_name import * #In this way, the classes used are not listed at the beginning of the file, but you clearly know where the modules are imported in the program, and you can avoid the conflict between the imported classes and names

Added by elum.chaitu on Thu, 09 Dec 2021 15:53:18 +0200