Attributes:
Public properties (belonging to the class, one for each class)
Ordinary properties (belonging to objects, one per object)
Private properties (belonging to objects, similar to ordinary properties, but cannot be accessed directly through the object)
Method: (by function)
Construction method
destructor
Method: (by type)
common method
Private methods (prefix the method with two underscores)
static method
class method
property method
static method
@staticmethod
static method, called directly through the class, does not need to create an object, does not implicitly pass self
class method
@classmethod
class method, the self in the method is the class itself, and the value passed when calling the method must also be the public attribute of the class, that is to
say, the class method can only operate the public fields of the class itself
class Dog(object): food = "gutou" age = "1" def __init__(self, name): self.NAME = name @classmethod def eat(self,age): # can only be a variable in a class # print(self.NAME) print(age) print(self.food) @classmethod def eat1(self, age): # can only be a variable in a class # print(self.NAME) age = "2" self.food = "tang" @staticmethod def print_1(): print(Dog.food, Dog.age) d = Dog("labuladuo") d.eat(Dog.age) #Called by object Dog.eat(Dog.age) #Called by class print("-----1-----") d.eat1(Dog.age) Dog.print_1() print("--------2-------") Dog.eat1(Dog.age) Dog.print_1()
property method
Properties become private properties, add assertions
class Cycle(object): def __init__(self,x,y,radius): self.x = x self.y = y self.radius = radius @property def radius(self): return self.__radius @radius.setter def radius(self,radius): assert radius > 0, "radius must be nonzero and non-negative" self.__radius = radius @radius.deleter def radius(self): del self.__radius def __str__(self): return "({0},{1},{2})".format(self.x,self.y,self.radius) c = Cycle(1,1,7) c.radius = 9 print(c) del c.radius print(c.radius) #(1,1,9) #AttributeError: 'Cycle' object has no attribute '_Cycle__radius'
class Dog(object): def __init__(self, name): self.name = name self.__food = None # def eat(self, food): original way # self.__food = food # print('%s eat %s' %(self.name, food)) @property def eat(self): print('%s eat %s' %(self.name,self.__food)) @eat.setter def eat(self, food): self.__food = food @eat.deleter def eat(self): del self.__food print("Deleted") d = Dog("labuladuo") # d.eat("baozi") #Original way d.eat d.eat = "baozi" d.eat #The calling method has not changed, just changing the incoming parameters will change the result #Can delete the __food attribute of the d.eat d.eat # error ''' output labuladuo eat None labuladuo eat baozi deleted AttributeError: 'Dog' object has no attribute '_Dog__food' '''
Attribute method application scenarios
Well, what's the use of making a method a static property? Since you want a static variable, why not just define it as a static variable? well, in the future you will need many scenarios that cannot be achieved simply by defining static properties. For example, you want to know the current status of a flight, whether it has arrived, delayed, canceled, or has flown away. In this state you must go through the following steps:
1. Connect to airline API query
2. Parse the query results
3. Return the result to your user
Therefore, the value of the status attribute is the result obtained after a series of actions, so every time you call it, it actually has to go through a series of actions before returning your result, but these actions do not need the user to care, the user only needs to call This property can be, understand?
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") f = Flight("CA980") f.flight_status
cool , now I can only query the flight status. Since this flight_status is already an attribute, can I assign a value to it? Try it
f = Flight("CA980") f.flight_status f.flight_status = 2
The output is: found cannot be changed
checking flight CA980 status flight is arrived... Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python basics/automation day7 object-oriented advanced/property methods.py", line 58, in <module> f.flight_status = 2 AttributeError: can't set attribute
Of course, you can change it, but you need to decorate it again through the @proerty.setter decorator. At this time, you need to write a new method to change the flight_status.
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 #Modify 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 #delete def flight_status(self): print("status got removed...") f = Flight("CA980") f.flight_status f.flight_status = 2 #trigger @flight_status.setter del f.flight_status #Trigger @flight_status.deleter