We are in the process of coding, the object could have added more and more features, even if we use inheritance, inevitably make the file more and more bloated.
To avoid this, Python allows objects to be stored in a module, and can be introduced in other modules.
In fact, this and namespace in C # is similar.
We first prepared a module called car.py, which contains a number of objects:
class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(self, mileage): if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You cannot do that.") def increase_odometer(self, miles): if miles >= 0: self.odometer_reading += miles else: print("The value is invalid, please input the number which should more than zero.") def fill_gas(self): print("Car is filling gas.") '''生成一个电池类''' class Battery(): def __init__(self, size = 100): self.size = size def describe_battery(self): print("Battery has " + str(self.size) + "-kwh battery. " ) def show_range(self): print("Battery has " + str(self.size * 3) + " killmaters on full charge") '''继承car,生成一个新类''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) self.battery = Battery() def fill_gas(self): print("Electric car no gas tank.")
Next we my_car of a new module, and import and create an instance of the Car class, and call some methods:
#-*- coding:utf-8 -*- from car import Car my_new_car = Car('Volvo', 'V60', '2020') print(my_new_car.get_description_name()) my_new_car.odometer_reading = 200 my_new_car.read_odometer() ''' 输出: 2020 Volvo V60 This car has 200 miles on it. '''
Similarly, we can also store inside a module, introducing a plurality of classes, operating:
#-*- coding:utf-8 -*- from car import Car, ElectricCar my_new_car = Car('Volvo', 'V60', '2020') print(my_new_car.get_description_name()) my_new_car.odometer_reading = 200 my_new_car.read_odometer() my_byd_tang = ElectricCar('BYD', 'Tang', '2020') my_byd_tang.battery.show_range() my_byd_tang.fill_gas() ''' 输出: 2020 Volvo V60 This car has 200 miles on it. Battery has 300 killmaters on full charge Electric car no gas tank. '''
Of course, you can import all of the class module:
#-*- coding:utf-8 -*- from car import * my_new_car = Car('Volvo', 'V60', '2020') print(my_new_car.get_description_name()) my_new_car.odometer_reading = 200 my_new_car.read_odometer() my_byd_tang = ElectricCar('BYD', 'Tang', '2020') my_byd_tang.battery.show_range() my_byd_tang.fill_gas() my_battery = Battery() my_battery.show_range() ''' 输出: 2020 Volvo V60 This car has 200 miles on it. Battery has 300 killmaters on full charge Electric car no gas tank. Battery has 300 killmaters on full charge '''
You can also directly import an entire module, but in this way need to add the module name before the name of the class:
#-*- coding:utf-8 -*- import car my_new_car = car.Car('Volvo', 'V60', '2020') print(my_new_car.get_description_name()) my_new_car.odometer_reading = 200 my_new_car.read_odometer() my_byd_tang = car.ElectricCar('BYD', 'Tang', '2020') my_byd_tang.battery.show_range() my_byd_tang.fill_gas() my_battery = car.Battery() my_battery.show_range() ''' 输出: 2020 Volvo V60 This car has 200 miles on it. Battery has 300 killmaters on full charge Electric car no gas tank. Battery has 300 killmaters on full charge '''
We in the actual encoding process, still recommend a class defines only in a single module, this module size is not too large, but also easy to make calls to other modules.
Also convenient for maintenance.