Day 5-5 Binding method and non-binding method

Bound and unbound methods:

The binding methods defined inside the class are divided into two categories:

1  # !_*_ coding:utf-8 _*_ 
2  
3  #There are two types of binding methods in the class: whoever is bound to, who will call. By default, the caller is passed in as the first parameter . 
4  
5  # 1. Methods bound to objects: methods not decorated by any decorators. 
6  class F:
 7      def  __init__ (self, name, age):
 8 self.name          = name
 9          self.age = age
 10  
11      def tell(self):
 12          print ( " name is %s " % self.name)
 13  # __init__ and tell methods are both bound methods of object p 
14  
15  
16 p = F(" JACK " , 18 )
 17  print (p.tell)    # <bound method F.tell of <__main__.F object at 0x0044F9B0>> is a method bound to F. 
18  
19  
20  
21  
22  # 2. bound to Class methods: methods decorated with the classmethod decorator. 
23  class F:
 24      def  __init__ (self, name, age):
 25 self.name          = name
 26          self.age = age
 27  
28      def tell(self):
 29          print ( " name is %s " % self.name)
 30 
31      @classmethod
 32      def func(cls):
 33          print ( " from cls bound " )
 34  
35 p = F( " JACK " , 18 )
 36  
37  print (F. func) # <bound method F. func of <class ' __main__.F'>> method bound to the class. 
38  
39  
40  #unbound method: 
41  
42  class F:
 43      def  __init__ (self, name, age):
 44 self.name          = name
 45          self.age = age
46  
47      def tell(self):
 48          print ( " name is %s " % self.name )
 49  
50      @classmethod
 51      def func(cls):
 52          print ( " from cls bound " )
 53  
54      @staticmethod     #added this Decorator, it becomes a common tool (function) in a class, both objects and classes can call 
55      def func1(x,y):
 56          print (x * y)
 57  
58  
59  
60 p = F( " JACK " , 18 )
61  
62 p.func1(5,7 )
 63 F.func1(9,8) #Classes   and objects can be called. Parameters are passed normally. 
64  
65  
66  
67  
68  class F:
 69      def  __init__ (self, name, age):
 70 self.name          = name
 71          self.age = age
 72  
73      def tell(self):
 74          print ( " name is %s " % self.name )
 75  
76      @classmethod
 77      def func(cls):
 78          print (" from cls bound " )
 79  
80      @staticmethod #With     this decorator, it becomes a common tool (function) in a class, both objects and classes can call 
81      def func1(x,y):
 82          return   x * y
 83  
84  
85  
86 p = F( " JACK " , 18 )
 87  
88  print (p.func1(5,7 ))
 89  print (F.func1(9,8))

 

Application Scenario:

#!_*_ coding:utf-8 _*_
import settings
import time
import hashlib
class People:
    def __init__(self, name, age, sex):
        self.id = self.creat_id() #The   class calls this unbound method. 
        self.name = name
        self.age = age
        self.sex = sex

    def tell_info(self): #The   method bound to the object 
        print ( " Name is %s Aage is %s Sex is %s, ID is %s. " % (self.name,self.age,self.sex, self .id) )


    @classmethod
    def from_settings(cls): #The   method bound to the class 
        obj = cls(
            settings.name,
            settings.age,
            settings.sex
        )
        return obj
    @staticmethod
    def creat_id(): #Unbound   method , anyone can call 
        time.sleep(0.00000000000000001) #Add a slight delay, otherwise the generated IDs are the same 
        m = hashlib.md5()
        m.update(bytes(str(time.time()),encoding="utf-8"))
        return m.hexdigest()




p = People("Lu", 20 ,"")
p.tell_info() #Bind   to the object, it will be called by the object, pass the object as the first parameter 
p = People.from_settings() #Bind to the class, called by the class, take the class as the first parameter The parameters are passed in. 
p.tell_info()

p1 = People("Lucy", 18 ,"")
p2 = People("Lili", 22 ,"")
p3 = People("Luma", 38 ,"")
print(p1.id)
print(p2.id)
print(p3.id)

 

Guess you like

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