Python: Property Lookup and Binding methods

Source: Internet
Author: User

Property Lookup

Class has two properties: Data Properties and Function properties

1. The Data property of a class is shared by all objects

The Data property of the # class is shared by all objects, the ID is the same as print (ID (oldboystudent.school))     # 4830576print (ID (s1.school))        # 4830576print (ID ( S2.school)        # 4830576print (ID (s3.school))        # 4830576

  

2. The function property of a class is bound to an object, and is called a method bound to an object.

The function property of the # class is bound to the object, Obj.method is called the binding method, the memory address is not the same as print (Oldboystudent.learn)      # <function Oldboystudent.learn at 0x0000000002879598>print (S1.learn)                 # <bound method Oldboystudent.learn of <__main__. Oldboystudent object at 0x0000000002866898>>print (S2.learn)                 # <bound method Oldboystudent.learn of <__ main__. Oldboystudent object at 0x00000000028669e8>>print (S3.learn)                 # <bound method Oldboystudent.learn of <__ main__. Oldboystudent object at 0x0000000002866f98>> #ps: ID is the implementation mechanism of Python and does not really reflect memory address, if there is memory address, or memory address

 In the Obj.name will first find the name in Obj's own namespace, can not find the class to find, the class will not find the parent class, and finally can not find the exception thrown

Binding method

Defining a class and instantiating three of objects

Class Oldboystudent:    school = ' Oldboy '    def __init__ (self, name, age, Sex):        self.name = name        Self.age = AG e        self.sex = Sex    def learn (self):        print ('%s is learning '% self.name)     # add Self.name    def eat (self) :        print ('%s is eating '% self.name)    def sleep (self):        print ('%s is sleeping '% self.name) S1 = oldboystudent (' Lee Tank ', ' male ', ') s2 = oldboystudent (' King cannon ', ' Male ', ') s3 = oldboystudent (' Bull harp ', ' female ', 38)

 A function defined in a class (not adorned by any adorner) is a tired function property, which can be used, but must follow the function's parameter rules, and several arguments need to pass several functions

Oldboystudent.learn (S1)         # Lee Tank is learningoldboystudent.eat (S2)            # King Cannon is eatingoldboystudent.sleep (S3)        # Cow harp is sleeping

A function defined in a class (not decorated by any adorner) is actually used by the object, and is bound to the right, although all objects point to the same functionality, but binding to different objects is different binding methods

Emphasis: The special of the method bound to the object is that the binding to who is called by whom, who calls, will be who itself as the first parameter to the method, that is, automatic value (method __init__ is the same reason)

S1.learn ()  # equals Oldboystudent.learn (S1) s2.eat ()    # equals oldboystudent.eat (S2) s3.sleep ()  # Equivalent to Oldboystudent.sleep (S3)

Note: This automatic value-passing feature of a method bound to an object determines that a function defined in the class must write a parameter by default self,self can be any name, but is written in self

Class is type

Everything in Python is an object, and the class and type in Python3 is a concept, and the type is the class

#类型dict就是类dict >>> list<class ' list ' > #实例化的到3个对象l1,l2,l3>>> l1=list () >>> l2=list () >>> l3=list () #三个对象都有绑定方法append, is the same function, but the memory address differs >>> L1.append<built-in method Append of the list object At 0x10b482b48>>>> l2.append<built-in method Append of the list object at 0x10b482b88>>>> L3.append<built-in method Append of List object at 0x10b482bc8> #操作绑定方法l1. Append (3), which is adding 3 to L1, will never add 3 to L2 or l3> >> l1.append (3) >>> l1[3]>>> l2[]>>> l3[] #调用类list. Append (l3,111) equivalent to L3.append (111) >>> list.append (l3,111) #l3. Append (111) >>> l3[111]

  

Section Exercises

# exercise 1: Write a student class, generate a bunch of student objects, (5 minutes) # # Requirements: # # There is a counter (attribute), statistics total instances of how many objects class Student (): School = "Home Squat University" Count = 0 def __in    It__ (self, name, age, Sex): Self.name = name Self.age = Age Self.sex = Sex Student.count + = 1  def learn (self): print ('%s is learning '% self.name) S1 = Student (' Zhang San ', ' Male ', ') s1.learn () s2 = Student (' John Doe ', ' female ', Print (student.count) print (S1.count) # Exercise 2: Imitate the Three Kingdoms to define two hero classes, (10 minutes) # # Requirements: # # Heroes need to have nicknames, attack, health and other attributes; # instantiate two heroic objects; # heroes can beat each other, The beaten side of the blood, blood volume of less than 0 is determined to be dead.         Class Garen:camp = ' Demacia ' def __init__ (self, nickname, Life_value, aggresivity): Self.nickname = Nickname Self.life_value = Life_value self.aggresivity = aggresivity def attack (self, enemy): Enemy.life_v Alue-= self.aggresivity if Enemy.life_value <= 0:print ('%s already dead '% Self.nickname) class Riven:cam p = ' Noxus ' def __init__ (self, nickname, Life_value, aggresivity): Self.nickname = Nickname Self.life_val UE = Life_value self.aggresivity = aggresivity def attack (self, Enemy): enemy.life_value-= self.aggresivity If Enemy.life_value <= 0:print ('%s already dead '% self.nickname) G1 = Garen (' Caocao ', [+]) r1 = Riven (' Liu Bei ', 100, 50 ) print (R1.life_value) g1.attack (R1) g1.attack (R1) g1.attack (R1) g1.attack (R1) print (R1.life_value)

  

Python: Property Lookup and Binding methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.