導入類
1.1、導入單個類
如何導入單個類
- 以上一節為例,編寫一個car.py程序
- 編寫一個新程序my_car.py
- 希望在my.car.py文件中直接使用car.py中的函數,使用from car import Car
實例,新建一個car.py
1 class Car(): 2 """一次模擬汽車的簡單嘗試""" 3 def __init__(self,make,model,year): 4 self.make = make 5 self.model = model 6 self.year = year 7 self.odometer_reading = 0 8 9 def get_descriptive_name(self): 10 long_name = str(self.year) + " " + self.make + " " + self.model 11 return long_name.title() 12 13 def read_odometer(self): 14 print("This car has" + str(self.odometer_reading) + "mile on it.") 15 16 def update_odomter(self,mileage): 17 if mileage >= self.odometer_reading: 18 self.odometer_reading = mileage 19 20 else: 21 print("You can't roll back an odometer!") 22 23 def increment_odometer(self,miles): 24 self.odometer_reading += miles
新建一個my_car.py
1 from car import Car 2 my_new_car = Car('audi','a8',2018) 3 print(my_new_car.get_descriptive_name()) 4 5 my_new_car.odometer_reading = 23 6 my_new_car.read_odometer()
執行結果:
2018 Audi A8
This car has23mile on it.
1.2、在一個模塊中存儲多個類
如上級例子所示,將car類作為父類,Electricar(car)作為子類

1 class Car(): 2 """一次模擬汽車的簡單嘗試""" 3 def __init__(self,make,model,year): 4 self.make = make 5 self.model = model 6 self.year = year 7 self.odometer_reading = 0 8 9 def get_descriptive_name(self): 10 long_name = str(self.year) + " " + self.make + " " + self.model 11 return long_name.title() 12 13 def read_odometer(self): 14 print("This car has" + str(self.odometer_reading) + "mile on it.") 15 16 def update_odomter(self,mileage): 17 if mileage >= self.odometer_reading: 18 self.odometer_reading = mileage 19 20 else: 21 print("You can't roll back an odometer!") 22 23 def increment_odometer(self,miles): 24 self.odometer_reading += miles 25 26 27 # 將實例用作屬性 28 class Battery(): 29 def __init__(self, battery_size=70): 30 """初始化電瓶的屬性""" 31 self.battery_size = battery_size 32 33 def describe_battery(self): 34 """打印一條描述電瓶容量的信息""" 35 print("This car has a" + str(self.battery_size) + "-KWh battery.") 36 37 #打印一條消息,指出電瓶的續航里程 38 def get_range(self): 39 if self.battery_size == 70: 40 range = 240 41 42 elif self.battery_size == 85: 43 range = 70 44 45 msg = "This car can go approximately" + str(range) 46 msg += "miles on a full charge." 47 print(msg) 48 49 50 class ElectricCar(Car): 51 """電動汽車的獨特""" 52 53 def __init__(self,make,model,year): 54 """初始化父類的屬性""" 55 super().__init__(make,model,year) 56 57 # 給子類定義屬性和方法 58 # self.battery_size = 70 #新屬性 59 #初始化父類的屬性,再初始化電動車特有的屬性 60 self.battery = Battery() 61 62 # #給子類定義屬性和方法 63 def describe_battery(self): 64 """打印一條描述電瓶容量的消息""" 65 print("This car has a " + str(self.battery_size) + "-KWh battery.") 66 67 #重寫父類的方法 68 def fill_gas_tank(): 69 """電動汽車沒有油箱""" 70 print("This car doesn't need a gas tank.")
在新的文件中,直接調用ElectricCar類即可。
1 from car import ElectricCar 2 3 my_tesla = ElectricCar("tesla",'model 2019',2019) 4 print(my_tesla.get_descriptive_name()) 5 my_tesla.battery.describe_battery() 6 my_tesla.battery.get_range()
執行結果:
1 2019 Tesla Model 2019 2 This car has a70-KWh battery. 3 This car can go approximately240miles on a full charge.
1.3、從一個模塊中導入多個類
1 from car import Car,ElectricCar
1 from car import Car,ElectricCar #import語句讓python打開模塊car,並導入其中的Car類 2 3 my_new_car = Car('audi','a4',2016) 4 print(my_new_car.get_descriptive_name()) 5 my_new_car.read_odometer() 6 7 my_tesla = ElectricCar('tesla','roadstater',2016) 8 print(my_tesla.get_descriptive_name()) 9 10 """導入類是一種有效的編程方式。"""
1.4、導入整個模塊
- 導入整個模塊
- 使用句點表示法訪問需要的類
1 import Car
實例
1 #導入整個模塊 2 import car 3 my_beetle = car.Car('volkswagen','beetle',2019) 4 print(my_beetle.get_descriptive_name()) 5 6 my_tesla = car.Car('tesla','roadster',2018) 7 print(my_tesla.get_descriptive_name())
運行結果:
2019 Volkswagen Beetle
2018 Tesla Roadster
1.5、導入模塊中的所有類
from modele_name import *
類編碼風格
- 類名應采用駝峰命名法 ,即將類名中的每個單詞的首字母都大寫,而不使用下划線。實例名和模塊名都采用小寫格式,並在單詞之間加上下划線。
- 對於每個類,都應緊跟在類定義后面包含一個文檔字符串。這種文檔字符串簡要地描述類的功能,並遵循編寫函數的文檔字符串時采用的格式約定。
- 每個模塊也都應包含一個文檔字符串,對其中的類可用於做什么進行描述。
- 可使用空行來組織代碼,但不要濫用。在類中,可使用一個空行來分隔方法;而在模塊中,可使用兩個空行來分隔類。
- 需要同時導入標准庫中的模塊和你編寫的模塊時,先編寫導入標准庫模塊的import 語句,再添加一個空行,然后編寫導入你自己編寫的模塊的import 語句。在包含多條import 語句的程序中,這種做法讓人更容易明白程序使用的各個模塊都來自何方