python learning DAY10 (attribute method)

class the Test (Object):
     DEF  the __init__ (Self, name): 
        the self.name = name 
        Self. __item = None
 # ------------------------- ------------------------------- 
    @Property             # property methods 
    DEF Test (self):           # static method does not pass the self argument 
        Pring ( " % S% Sxxx " % (the self.name, Self. __item ))
 # ------------------------------ -------------------------- 
t = the Test ( " LBC " ) 
t.test () 
# ***
TypeError: ' NoneType ' Object IS  not a Callable   # can not call 
# *** 
modification: 
t.test    # property method: a method into a static property, so when the call does not require parentheses 

if the method parameter you need to have properties then add the next piece of code :( methods may not require the same method name, but suggested that the same method names and properties) 
    @ test.setter 
    DEF the Test (Self, Item):
         Print ( " the SET to Item: " , Item) 
        . Self __item = item // the purpose of the private variable is the method of variables stored, method variables are passed into the method of variable 
assignment when calling 
t.test = " LBC " 

# ------
Methods can not directly attribute del delete, delete only the corresponding variables del deleter by the function, deleter as defined with setter method
 # ------ 

# ------------ ---------------------------- ------------- example 
flights: enabling users to get their needs, without the need for detailed inquiry step
 class Flight (Object):
     DEF  __init__ (Self, name): 
        self.flight_name = name 


    DEF checking_status (Self):
         Print ( " Checking Flight Status% S " % 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)
        print("%s has changed to %s"%(self.flight_status,status))
    


f = Flight("CA980")
f.flight_status
#f.flight_status=2
#-------------------------------------------------------------
#完善版
class Flight(object):
    def __init__(self,name):
        self.flight_name = name


    def checking_status(self):
        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 = {
: "canceled",
:"arrived",
: "departured"
        }
        print("\033[31;1mHas changed the flight status to \033[0m",status_dic.get(status) )

    @flight_status.deleter  #删除
    def flight_status(self):
        print("status got removed...")

f = Flight("CA980")
f.flight_status
f.flight_status =  2 #触发@flight_status.setter 
del f.flight_status #触发@flight_status.deleter

This code sample reference Alex blog!

Guess you like

Origin www.cnblogs.com/god-for-speed/p/11362431.html
Recommended