property
Property is a special property. When you access it, you will execute a function (function) and then return a value. That is, you can change the access method of a function property into the way of accessing data property.
Let's start with a comparison
Example 1: when calling bmi functions, we need to add parentheses, but we often need another call method -- do not want to add parentheses
class people(): def __init__(self, name, height, weight): self.name = name self.height = height self.weight = weight def bmi(self): return self.weight / (self.height ** 2) p = people('ysg', 1.8, 75) print(p.bmi()) # Results: 23.148148148148145
Example 2: after using property, the call does not need to use brackets
class people(): def __init__(self, name, height, weight): self.name = name self.height = height self.weight = weight @property def bmi(self): return self.weight / (self.height ** 2) p = people('ysg', 1.8, 75) print(p.bmi) # Results: 23.148148148148145 # Errors will be reported when using parenthesized calls: TypeError: 'float' object is not callable
Other uses of property, not commonly used
Prerequisite
class people(): def __init__(self, name): self.__name = name @property def name(self): return self.__name p = people('ysg') print(p.name)
Use of setter and delete
class people(): def __init__(self, name): self.__name = name @property def name(self): return self.__name @name.setter def name(self, val): if not isinstance(val, str): print('Format is not str,Not allowed to be modified') return self.__name = val @name.deleter def name(self): print('Not allowed to be deleted') //modify p = people('ysg') p.name = 'ysging' print(p.name) //Results: parsing p.name = 213 print(p.name) //result: //Format is not str, not allowed to be modified ysg //delete del p.name print(p.name) //result: //Not allowed to be deleted ysg
The meaning of property is: after defining the function of a class as a property, obj.name cannot realize that its name executes a function and then calculates it when the object is used again. The usage of this property follows the principle of unified access
Binding method
Functions defined within a class can be divided into two categories
One: binding method
The caller will be automatically passed in as the first parameter by whom the binding should be called
Method of binding object: defined in the class and not decorated by any decorator
Method bound to class: a method defined within a class and decorated by the decorator classmethod
Two: unbound method
No binding with class or object, no automatic value passing
It is to define a common function or tool in the class. Both the object and the class can be called and need to pass values
Methods for binding objects
class Foo(): def __init__(self, name): self.name = name def tell(self): print('this is %s' % self.name) @classmethod def func(cls): print() f = Foo('ysg') f.tell() # this is ysg print(f.tell) # <bound method Foo.tell of <__main__.Foo object at 0x000001E5BED2F390>> print(Foo.tell) # <function Foo.tell at 0x000001E5BED31268> Foo.tell(f) # Use class to call this is ysg
Methods bound to classes
class Foo(): def __init__(self, name): self.name = name @classmethod def func(cls): print(cls) f = Foo('ysg') print(Foo.func) # <bound method Foo.func of <class '__main__.Foo'>> Foo.func() # <class '__main__.Foo'> print(Foo) # <class '__main__.Foo'>
Unbound method
class Foo(): def __init__(self, name): self.name = name @staticmethod def func(x, y): print(x + y) f = Foo('ysg') print(Foo.func) # <function Foo.func at 0x0000021B45FE1268> print(f.func) # <function Foo.func at 0x0000021B45FE1268> f.func(1, 2) # 3 Foo.func(1, 2) # 3
Use scenario
Summary: which method to use depends on the logic of the function body to determine what parameters should be passed.
Example: binding to an object
class People(): def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def tell(self): print('Full name:{0},Age:{1},Gender:{2}'.format(self.name, self.age, self.sex)) p = People('ysg', 21, 'man') p.tell() # Full name: ysg,Age: 21, gender: man
Example: binding to a class
Precondition, take value from configuration file
configs.py name = 'ysging' age = 15 sex = 'man'
import configs class People(): def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def tell_info(self): print('Full name:{0},Age:{1},Gender:{2}'.format(self.name, self.age, self.sex)) @classmethod def tell(cls): p = cls( configs.name, configs.age, configs.sex ) return p p = People.tell() p.tell_info() #Full name: ysging,Age: 15, gender: man
Example: unbound method, assign id to each instance
import time import hashlib class People(): def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex self.id = self.ids() @staticmethod def ids(): time.sleep(0.01) m = hashlib.md5(str(time.time()).encode('utf-8')) return m.hexdigest() p = People('ysg1', 21, 'man') p2 = People('ysg2', 22, 'man') p3 = People('ysg3', 23, 'man') print(p.id) # 44261d399ba9650c628cb8d189ffdd0b print(p2.id) # 2c7270c27984119c7cf39e9d575f07bd print(p3.id) # 4c386111edf4f641f7c35bb855d1551a