python3匯入類 Python筆記 匯入類

2021-10-11 05:17:27 字數 4310 閱讀 1288

鎮貼圖自攝。(被壓縮的好厲害呀)

在家好無聊啊好無聊啊..匯入單個類

由於模擬較繁瑣,所以我們可以設定文件字串,形如:

'''乙個可用於表示汽車的類'''

class car():

'''一次模擬汽車的簡單嘗試'''

def __init__(self,make,model,year):

self.make = make

self.model = model

self.year = year

self.odometer_reading = 0

def get_descriptive_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 can''t roll back an odometer!')

def increment_odometer(self,miles):

'''將里程表讀數增加指定的量'''

self.odometer_reading += miles

在python中每個檔案都是乙個模組,直接from不帶副檔名的檔名稱,import類就可以了,

相當於繼承

from car import car

my_new_car = car('audi','a4',2016)在乙個模組中儲存多個類

原標有文件字串的**:

'''一組用於表示燃油汽車和電動汽車的類'''

class car():

def __init__(self,make,model,year):

self.make = make

self.nmodel = model

self.year = year

self.odometer_reading = 0

def get_discriptive_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):

self.mileage = mileage

if mileage >= self.odometer_reading:

self.odometer_reading = mileage

else:

print("you can't roll back an odometer!")

def increment_odometer(self,miles):

self.odometer_reading += miles

class battery():

'''一次模擬電動汽車電瓶的簡單嘗試'''

def __init__(self,battery_size = 70):

'''初始化電瓶的屬性'''

self.battery_size = battery_size

def describe_battery(self):

'''列印一條電瓶容量的資訊'''

print("this car has a "+str(self.battery_size) + "-kwh battery.")

def get_range(self):

'''列印一條電瓶續航里程的資訊'''

if self.battery_size == 70:

range = 240

elif self.battery_size == 85:

range = 270

print(message)

class electriccar(car):

'''模擬電動汽車的獨特之處'''

def __init__(self,make,model,year):

'''初始化父類屬性 再初始化電動汽車的特有屬性'''

super().__init__(make,model,year)

self.battery = battery()

def read_odometer(self):

print("there's no need to read it!")

from electric_car import electriccar

my_tesla = electriccar('tesla','model s',2016)

該模組中有多個類,我們可以只從其中選取乙個類從乙個模組中匯入多個類

from electric_car import electriccar,car

my_tesla = electriccar('tesla','model s',2016)

my_beetle = car('volkswagen','beetle',2016)

也就是原來的檔案不只有乙個類,我們從乙個檔案中匯入多個類匯入整個模組

import electric_car

my_tesla = electric_car.electriccar('tesla','model s',2016)

my_beetle = electric_car.car('volkswagen','beetle',2016)

前面要加electric.,類似於import math,如果要用math庫裡面的什麼東西要加math.,顯然類electriccar和car都是屬於electriccar庫的東西,所以要加electri_car匯入模組中的所有類

from module_name import *

題:#晚上有事兒 有時間再寫

單純匯入的話...挺傻的...

9-10 my code

from restaurant import restaurant

9-11 my code

class user:

def __init__(self,first_name,last_name):

self.first_name = first_name

self.last_name = last_name

self.login_attempts = 0

def describe_user(self):

print(self.first_name)

print(self.last_name)

def greet_user(self):

print('welcome to windows 10')

def increment_login_attempts(self):

self.login_attempts +=1

def reste_login_attempts(self):

self.login_attempts = 0

class privileges():

def privilege(self):

self.privileges = ['can add post','can delete post','can ban user']

print(self.privileges)

class admin(user):

def __init__(self,first_name,last_name):

super().__init__(first_name,last_name)

self.privileges = privileges()

def show_privilege(self):

a = privileges().privilege()

print(a)

from admin import user,privileges,admin

9-12 略

python3菜鳥教程100例 Python3

python3 字典 字典是另一種可變容器模型,且可儲存任意型別物件。字典的每個鍵值 key value 對用冒號 分割,每個對之間用逗號 分割,整個字典包括在花括號 中 格式如下所示 鍵必須是唯一的,但值則不必。值可以取任何資料型別,但鍵必須是不可變的,如字串,數字。乙個簡單的字典例項 也可如此建...

python3編譯安裝 編譯安裝Python 3

目前python最新版為python 3,以下是編譯安裝python 3.7.0的方法,根據提示一行乙個命令輸入即可 安裝依賴 yum y install wget gcc gcc c libffi devel zlib devel wget 解壓tar xvjf python 3.7.0.tar....

python3經典例題 經典例題 Python

python python開發 python語言 經典例題 python 經典例題 if巢狀 1.使用者輸入賬號 2.使用者輸入密碼 3.判斷使用者的賬號是不是alex 4.如果賬號是alex在繼續判斷密碼是不是alexdsb 5.賬號和密碼都正確提示使用者alex就是乙個dsb 6.如果賬號正確密...