Python學習筆記十九 小甲魚第三十九講

2021-09-10 06:26:52 字數 1433 閱讀 6726

0、在python裡組合很簡單,直接在類定義中把需要的類放進去例項化就可以了

1、繼承常常用於乙個類的再分,比如男人是人。而組合用於「有乙個」的場景

2、當類定義完成的時候,類定義就變成類物件

3、當物件的屬性和方法名字相同時,屬性會覆蓋方法

4、num和count是類屬性,x和y是例項屬性

5、python要求方法有例項才能呼叫,這就是python的繫結概念,所以python會把bb物件作為第乙個引數傳入,所以需要0個引數,實際傳入1個引數

0、>>> class c:

count = 0

def __init__(self):

c.count += 1

def __del__(self):

c.count -= 1

>>> a = c()

>>> b = c()

>>> c = c()

>>> c.count

3>>> del a

>>> c.count

2>>>

1、class stack:

def __init__(self,start=):

self.stack =

for x in start:

self.push(x)#呼叫自身的方法初始化(在下面定義)

def isempty(self):

return not self.stack

def push(self,x):

def pop(self):

if not self.stack:

print('警告,我為空')

else:

return self.stack.pop()

def top(self):

if not self.stack:

print('警告,我為空')

else:

return self.stack[-1]

def bottom(self):

if not self.stack:

print('警告,我為空')

else:

return self.stack[0]

執行結果

>>> a = stack()

>>> a.push(1)

>>> a.top()

1>>> b = stack([1,2,3])

>>> b.top()

3

python學習筆記2 小甲魚課程

1.idle是python的外殼 2.idle file new file 輸入程式段 3.快捷鍵 alt n 上一條語句 f5 執行程式段 4.dir builtins 檢視所有內建函式 5.help 檢視具體的某個函式怎麼用,例如 help input 6.python不允許if條件中賦值,例如...

小甲魚Python第十九講課後習題

測試題 0.下邊程式會輸入什麼?def next print 我在next 函式裡 pre def pre print 我在pre 函式裡 answer 我在next 函式裡 我在pre 函式裡 請問以下這個函式有返回值嗎?def hello print hello fishc answer 有,當...

小甲魚python筆記心得1

assert 3 4 assert 斷言 和if 差不多只不過為假的時候會拋異常 for 目標 in 集合 迴圈體good fishc for i in good print i,end 結果f i s h c 加空格列印出 meber 螃蟹 鯊魚,鯨魚 for each in meber prin...