小猿圈python學習-屬性方法property

定義

屬性方法的作用就是通過@property把一個方法變成一個靜態屬性

class Student(object):

? ? stu_num = 0

? ? def __init__(self,name):

? ? ? ? self.name = name

? ? @property

? ? def fly(self):

? ? ? ? print("%s is flying..." % self.name)

s = Student("Mjj")

s.fly()

調用會出以下錯誤, 說NoneType is not callable, 因為fly此時已經變成一個靜態屬性了, 不是方法了, 想調用已經不需要加()號了,直接s.fly就可以了

Mjj is flying...

Traceback (most recent call last):

? File "/day6_面向對象進階/屬性方法.py", line 14, in

? ? s.fly()

TypeError: 'NoneType' object is not callable

正常調用

s.fly

輸出

Mjj is flying...

property應用場景

好吧,把一個方法變成靜態屬性有什么卵用呢?既然想要靜態變量,那直接定義成一個靜態變量不就得了么?well, 以后你會需到很多場景是不能簡單通過 定義 靜態屬性來實現的, 比如 ,你想知道一個航班當前的狀態,是到達了、延遲了、取消了、還是已經飛走了, 想知道這種狀態你必須經歷以下幾步:

1. 連接航空公司API查詢

2. 對查詢結果進行解析

3. 返回結果給你的用戶

因此這個status屬性的值是一系列動作后才得到的結果,所以你每次調用時,其實它都要經過一系列的動作才返回你結果,但這些動作過程不需要用戶關心, 用戶只需要調用這個屬性就可以,明白 了么?

class Flight(object):

? ? def __init__(self,name):

? ? ? ? self.flight_name = name

? ? def checking_status(self):

? ? ? ? print("connecting airline company api...... " )

? ? ? ? print("checking flight %s status " % self.flight_name)

? ? ? ? return? 1

? ? @property

? ? def flight_status(self):

? ? ? ? status = self.checking_status()

? ? ? ? if status == 0 :

? ? ? ? ? ? print("flight got canceled...")

? ? ? ? elif status == 1 :

? ? ? ? ? ? print("flight is arrived...")

? ? ? ? elif status == 2:

? ? ? ? ? ? print("flight has departured already...")

? ? ? ? else:

? ? ? ? ? ? print("cannot confirm the flight status...,please check later")

f = Flight("CA980")

f.flight_status

cool , 那現在我只能查詢航班狀態, 既然這個flight_status已經是個屬性了, 那我能否給它賦值呢?試試吧

f = Flight("CA980")

f.flight_status

f.flight_status =3

輸出, 說不能更改這個屬性,我擦。。。。,怎么辦怎么辦。。。

Traceback (most recent call last):

connecting airline company api......

? File "/day6_面向對象進階/屬性方法.py", line 41, in

? ? f.flight_status =3

AttributeError: can't set attribute

checking flight CA980 status

flight is arrived...

當然可以改, 不過需要通過@proerty.setter裝飾器再裝飾一下,此時 你需要寫一個新方法, 對這個flight_status進行更改。

class Flight(object):

? ? def __init__(self,name):

? ? ? ? self.flight_name = name

? ? def checking_status(self):

? ? ? ? print("connecting airline company api...... " )

? ? ? ? print("checking flight %s status " % self.flight_name)

? ? ? ? return? 1

? ? @property

? ? def flight_status(self):

? ? ? ? status = self.checking_status()

? ? ? ? if status == 0 :

? ? ? ? ? ? print("flight got canceled...")

? ? ? ? elif status == 1 :

? ? ? ? ? ? print("flight is arrived...")

? ? ? ? elif status == 2:

? ? ? ? ? ? print("flight has departured already...")

? ? ? ? else:

? ? ? ? ? ? print("cannot confirm the flight status...,please check later")

? ? @flight_status.setter # 修改

? ? def flight_status(self,status):

? ? ? ? status_dic = {

? ? ? ? ? ? 0 : "canceled",

? ? ? ? ? ? 1 : "arrived",

? ? ? ? ? ? 2 : "departured"

? ? ? ? }

? ? ? ? print("\033[31;1mHas changed the flight status to \033[0m",status_dic.get(status) )

f = Flight("CA980")

f.flight_status

f.flight_status = 1

輸出

connecting airline company api……?

checking flight CA980 status?

flight is arrived…

Has changed the flight status to?arrived

還可以刪除

? ? @flight_status.deleter? #刪除

? ? def flight_status(self):

? ? ? ? print("status got removed...")

f = Flight("CA980")

# f.flight_status

# f.flight_status = 1

del f.flight_status

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容