property用法的用法主要有两点:
一、将方法当做属性来使用 二、将属性设置为只读
class Flight(object):
def __init__(self,name):
self.flight_name = name
def checking_status(self):
print("connecting airline company/airport 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 cancled...")
elif status == 1:
print("flight is arrived")
elif status == 2:
print("flight has departured already")
else:
print("cannot fonfirm the flight status...,please check later")
@flight_status.setter
def flight_status(self,status):
print("changing...flight status..",status)
self.status = status
f = Flight("A9")
f.flight_status
f.flight_status = 0
结果:
connecting airline company/airport api....
checking flight CA980 status
flight is arrived
changing...flight status.. 0
Python属性方法详细
|