8 8python 整理電站

2021-08-26 05:42:36 字數 1831 閱讀 9794

整理電站

# 類定義

classpeople:

# 定義基本屬性

name =''

age = 0#

定義私有屬性,私有屬性在類外部無法直接進行訪問

__weight = 0#

定義構造方法

def__init__

(self

, n, a, w):

self

.name = n

self

.age = a

self

.__weight = w

defspeak(

self

):print

("%s說: 我 %d 歲。"% (

self

.name,

self

.age))

classstudent(people):

grade =''

def__init__

(self

,n,a,w,g):

#呼叫父類的建構函式

people.

__init__

(self

,n,a,w)

self

.grade = g

defspeak(

self

):print

("%s說: 我 %d 歲,我在讀%d年級"% (

self

.name,

self

.age,

self

.grade))

classspeaker():

topic =''

name =''

def__init__

(self

,n,t):

self

.name = n

self

.topic = t

defspeak(

self

):print

("我叫 %s,我是乙個演說家,我演講的主題是 %s"%(

self

.name,

self

.topic))

classsample(speaker,student):

#多重繼承

a =''

def__init__

(self

,n,a,w,g,t):

student.

__init__

(self

,n,a,w,g)

speaker.

__init__

(self

,n,t)

test = sample("tim",25,

80,4,"python")

test.speak()  

#方法名同,預設呼叫的是在括號中排前地父類的方法

Python語法整理

一 python中的變數 python中的標準資料型別一共有五種 數字 字串 元組 列表和字典。1 數字型 在python當中,所有的變數無需宣告便可以直接賦值使用。2 字串 在python中,字串變數的定義和c c 中類似,都是雙引號中間是字串的內容,不同的是,在python中除了用雙引號還可以用...

python模組整理

1 從字串裡提取數字 import re string mysqld3 print re.findall r d string 3 2 執行shell命令 import commands commands.getstatusoutput ls bin ls 0,bin ls commands.get...

python問題整理

新手琳剛剛學python,會有很多小問題,學會記錄。1.expected an indented block問題 可是還報這個錯誤,檢查了好幾遍。才發現,沒有養成好習慣真是難受,1.語句一定要嚴格縮排,有層級感 2.語句後凡是有冒號的一定要補充好,不要因為ta不重要就不管了。2.seed 用於指定隨...