Property lookup and binding methods

1. Property lookup

Classes contain two types of properties: data properties and function properties

x='global'
class LuffyStudent:
    school='luffycity'  #数据属性

    def __init__(self,name,sex,age):  #函数属性
        self.Name=name
        self.Sex=sex
        self.Age=age

        #stu1.Name='王二丫'
        #stu1.Sex='女'
        #stu1.Age=18

    def learn(self,x):  #函数属性
        print('%s is learning %s' %(self.Name,x))

    def eat(self):  #函数属性
        print('%s is sleeping' %self.Name)


#产生对象
s1=LuffyStudent('王二丫','女',18)
s2=LuffyStudent('李三炮','男',38)
s3=LuffyStudent('张铁蛋','男',48)

1.1 The data attributes of a class are common to all objects


#类的数据属性是所有对象共享的,id都一样
print(id(LuffyStudent.school))

print(id(s1.school)) #4377347328
print(id(s2.school)) #4377347328
print(id(s3.school)) #4377347328

1.2 The function data of the class is bound to the object, which is called the method bound to the object

The function properties of the class are bound to the object, obj.method is called the binding method, and the memory addresses are different

print(OldboyStudent.learn) #<function OldboyStudent.learn at 0x1021329d8>
print(s1.learn) #<bound method OldboyStudent.learn of <__main__.OldboyStudent object at 0x1021466d8>>
print(s2.learn) #<bound method OldboyStudent.learn of <__main__.OldboyStudent object at 0x102146710>>
print(s3.learn) #<bound method OldboyStudent.learn of <__main__.OldboyStudent object at 0x102146748>>

#ps:id是python的实现机制,并不能真实反映内存地址,如果有内存地址,还是以内存地址为准

In obj.name, it will first look for name from obj's own namespace. If it can't find it, it will find it in the class. If the class can't find it, it will find the parent class. If it can't find it, it will throw an exception.


2. Binding method

The functions defined in the class (not decorated by any decorator) are mainly used by objects and are bound to objects. Although all objects point to the same function, binding to different objects is different binding method.

Emphasis: The special feature of the method bound to the object is that whoever is bound to it will be called, and whoever calls it will pass 'who' itself as the first parameter to the method, that is, automatic value transfer (method __init__ is the same)

s1.learn() #等同于OldboyStudent.learn(s1)
s2.learn() #等同于OldboyStudent.learn(s2)
s3.learn() #等同于OldboyStudent.learn(s3)

Note: The automatic value transfer feature of methods bound to objects determines that functions defined in a class must write a parameter self by default. Self can be any name, but it is customary to write self.


3. A class is a type

Everything in python is an object , and in python3, class and type are a concept, and a type is a class

#类型dict就是类dict
>>> list
<class 'list'>

#实例化的到3个对象l1,l2,l3
>>> l1=list()
>>> l2=list()
>>> l3=list()

#三个对象都有绑定方法append,是相同的功能,但内存地址不同
>>> l1.append
<built-in method append of list object at 0x10b482b48>
>>> l2.append
<built-in method append of list object at 0x10b482b88>
>>> l3.append
<built-in method append of list object at 0x10b482bc8>

#操作绑定方法l1.append(3),就是在往l1添加3,绝对不会将3添加到l2或l3
>>> l1.append(3)
>>> l1
[3]
>>> l2
[]
>>> l3
[]
#调用类list.append(l3,111)等同于l3.append(111)
>>> list.append(l3,111) #l3.append(111)
>>> l3
[111]

Guess you like

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