小猿圈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

你可能感兴趣的:(小猿圈python学习-属性方法property)