1. Packaging
property is a special property, when it is accessed, it will perform a function (function) and then return the value
BMI index (bmi is calculated, but it is obviously a property of a class rather than a method, if we make it a properties are easier to understand)
1 class People: 2 def __init__(self,name,hight,weight): 3 self.name=name 4 self.hight=hight 5 self.weight=weight 6 7 @property 8 def bim(self): 9 return self.weight/(self.hight**2) 10 11 lzy=People('lzy',1.1,16) 12 lg=People('lg',1.7,75) 13 14 lg.weight=70 15 print (lzy.bim) 16 print (lg.bim)
First of all, it needs to be clear that the calculated bmi is not a fixed value, so a function needs to be written, and a value will be calculated immediately every time the function is called.
So we can add a decorator to the bmi function and disguise it as a data attribute
Because although it is disguised as a data attribute, it is still a method and cannot be directly attached to it
1 class People: 2 def __init__ (self,name): 3 self. __name = name 4 5 6 @property 7 def name(self): # obj.name 8 print ( ' You are accessing the username... ' ) 9 return self .__name 10 11 @name.setter # obj.name='EGON' 12 def name(self,x): 13 # print('================ =',x) 14 iftype(x) is not str: 15 raise TypeError( ' The name must be str type, stupid ' ) 16 self. __name = x 17 18 @name . deleter 19 def name(self): 20 # print('Don't let you delete ') 21 del self .__name 22 23 obj=People( ' egon ' ) 24 25 print (obj.name) 26 print (obj.name()) 27 28 print (obj.name) 29 30 obj.name='EGON' 31 32 print(obj.name) 33 34 obj.name=123 35 36 del obj.name
2. Polymorphism
Polymorphism means multiple forms of the same thing
It is to use the base class to create a set of unified rules, forcing subclasses to follow, so that it can be used without considering the specific type of the object.
Use the methods under the object directly
class Animal:
def eat(self)
pass
def drink(self)
pass
class Cat(Animal)
def eat(self):
print('aaa')
class Dog(Animal):
def eat(self):
print('bbb')
c=cat()
c.eat()
c.drink()
Polymorphism: You can directly use the methods under the object without considering the specific type of the object
import abc
class Animal(metaclass=abc.ABCMeta):
@abc.absstractmethod
def eat(self):
pass
@abc.abstractmethod
def drink(self):
pass
obj=Animal() base class cannot be instantiated
class Cat(Animal):
def eat(self):
print('cat eat')
def drink(self):
print('cat drink')
c=Cat()
c.bark()
def BARK(animal):
animal.bark()
bark(c)
1. Increased program flexibility
In order to keep the same, regardless of the ever-changing object, the user calls it in the same form, such as func(animal)
2. Increased program scalability
A new class is created by inheriting the animal class, and users do not need to change their own code, or use func(animal) to call
Three duck types
Python embraces duck typing, i.e. 'if it looks like, quacks and walks like a duck, then it's a duck'
For example, if you want to write a custom version of an existing object, you can either inherit from that object or create a completely new object that looks and behaves like it but has nothing to do with it, the latter is often used to preserve the loose coupling of program components
class Foo:
def f1(self):
print('from foo.f1')
def f2(self):
print('from bar.f1')
class Bar:
def f1(self):
print('from bar.f1')
def f2(self):
print('from bar.f2')
obj1=Foo()
obj2=Bar()
obj1.f1()
obj1.f2()
obj2.f1()
obj2.f2()
Binding method:
1. Binding method:
The function defined inside the class is used by the object by default, and it is bound to the object, which is called the binding method of the object
The methods of binding objects are special:
Should be called by the object, the object is called, the object will be automatically passed in as the first parameter
Methods bound to classes are special:
It should be called by the class, the class is called, and the class will be automatically passed in as the first parameter
import settings
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def tell(self):
print('%s:%s'%(self.name,self.age))
@classmethod
def from_conf(cls):
return cls(settings.Name,settings.AGE)
staticmethod: unbound method, which is an ordinary function
Features: neither bound to the class nor to the object, which means who can call
Whoever uses it is an ordinary function, which means that there is no automatic value transfer feature.