April 16 Summary

1. Today we mainly talk about some methods

add @property in front of the function

You can disguise the function as an attribute and run the call without adding ().

2. The function cannot be assigned a value, if you want to change it

@name.setter

   def name  (self,x):

       sef .__name=x

If you want to delete

@name.deleter

      def name(self,x)

      del  self.__name

Create a uniform set of rules with base classes

Polymorphism refers to one thing, multiple forms, and subclasses must follow the standards of the parent class

import abc

class  Animal(metaclass=abc.ABCmeta)

@abc.abstractmethod

def eat(self)

pass

Subsequent subclasses must have this eat() function

3. Two methods are mentioned

classmethod和staticmethod

Binding method: that is, the function in the class is the binding method of the object, and whoever calls it is passed in as the first parameter

Binding to a class: passing the class as the first parameter is to add @classmethod in front of the function in the class

 

There is no need to bind classes and objects in the class, just a simple function, just add @staticmethod in front

Anyone can use it, and it will not pass the value by itself.

 

Duck: That is, you agreed that everyone can use the same attributes. same property name

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324466712&siteId=291194637