Python學習系列之物件的建立(二十四)

2022-07-05 19:12:14 字數 993 閱讀 3649

物件的建立

物件的建立又稱為類的例項化

語法:例項名=類名()

物件示意圖:

例子:

#建立student類的例項物件

stu=student()

意義:有了例項,就可以呼叫類中的內容

可以使用內建函式檢視類物件的型別、記憶體位址和內容

stu=student('張三',20)

print(type(stu))

print(id(stu))

print(stu)

print('-------------------')

print(type(student))

print(id(student))

print(student)

執行結果:

物件的使用(在類之外使用類物件,對類的內容進行呼叫)

#這段**要寫在類之外

stu=student('張三',20)

stu.eat()

print(stu.name)

print(stu.age)

執行結果:

還有一種呼叫寫法

stu=student('張三',20)

student.eat(stu)

執行結果:

說明:student.eat(stu)相當於stu.eat(),都是呼叫student中的eat方法,類名.方法名(類的物件)--> 實際上就是方法定義處的self

python系列之numpy模組學習

未完待續 1.nupmy是什麼?numpy是python中用於科學計算的乙個庫。import numpy as np 匯入numpy並命名為np print np.version.version 輸出numpy的版本 print np.version 也可以這樣輸出版本 結果如圖 2.建立陣列 ar...

python學習系列之氣泡排序

created on sat aug 19 10 54 25 2017 bubblesort author fanrupin def bubblesort arr if arr 0 or len arr 1 return 0 for i in range len arr for j in range...

Python學習系列之zip函式

目錄 一 zip函式 內建函式 1.1 定義 基本語法 引數說明 返回值 示例 1.2 處理列表 1.3 處理元組 1.4 處理字典 1.5 處理乙個引數和空列表 python 2 zip 函式用於將可迭代的物件 字典,列表,元組,集合,字串等 作為引數,將物件中對應的元素打包成乙個個元組,然後返回...