属性查找与绑定方法
Posted 0b0s
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了属性查找与绑定方法相关的知识,希望对你有一定的参考价值。
# class Student:
# school=‘oldboy‘
# def __init__(self,name,age,gender):
# self.Name=name
# self.Age = age
# self.Gender=gender
# def learn(self):
# print(‘%s is learning‘%self.Name)
# def choose_course(self):
# print(‘choose course‘)
# stu1=Student(‘李三炮‘,‘18‘,‘男‘)
# stu2=Student(‘张美丽‘,‘18‘,‘女‘)
# stu3=Student(‘二狗子‘,‘18‘,‘男‘)
‘‘‘
对象的名称空间里值存放着对象独有的属性,而对象相似的属性是存放于类中的
对象在访问属性时,会优先从对象本身的__dict__中查找,未找到,则去类的__dict__中查找
如果类中也没有就会报错
‘‘‘
# print(Student.school,id(Student.school))
# print(stu1.school,id(stu1.school))
# print(stu2.school,id(stu2.school))
# print(stu3.school,id(stu3.school))
‘‘‘
oldboy 2426462576880
oldboy 2426462576880
oldboy 2426462576880
oldboy 2426462576880
‘‘‘
# print(Student.school,id(Student.school))
# stu1.school=‘love‘
# print(stu1.school,id(stu1.school))
# print(stu2.school,id(stu2.school))
# print(stu3.school,id(stu3.school))
‘‘‘
oldboy 2662001647856
love 2662001881008
oldboy 2662001647856
oldboy 2662001647856
‘‘‘
# Student.school=‘love‘
# print(Student.school,id(Student.school))
# print(stu1.school,id(stu1.school))
# print(stu2.school,id(stu2.school))
# print(stu3.school,id(stu3.school))
‘‘‘
love 2173913608112
love 2173913608112
love 2173913608112
love 2173913608112
‘‘‘
‘‘‘
类中定义的变量是类的数据属性,是共享给所有对象用的,指向相同的内存地址
‘‘‘
# print(Student.learn)
# print(stu1.learn)
# print(stu2.learn)
# print(stu3.learn)
‘‘‘
<function Student.learn at 0x000002554E9B4EE8>
<bound method Student.learn of <__main__.Student object at 0x000002554E9C2648>>
<bound method Student.learn of <__main__.Student object at 0x000002554E9C2688>>
<bound method Student.learn of <__main__.Student object at 0x000002554E9C26C8>>
‘‘‘
‘‘‘
类中定义的函数是绑定给对象使用,不同对象就是不同绑定方法
绑定给谁就由谁来调用,谁来调用就会把谁当作第一个参数传给对应的函数
‘‘‘
# stu1.learn()
# stu2.learn()
# stu3.learn()
‘‘‘
李三炮 is learning
张美丽 is learning
二狗子 is learning
‘‘‘
# class Student:
# school=‘oldboy‘
# def __init__(self,name,age,gender):
# self.Name=name
# self.Age = age
# self.Gender=gender
# def learn(self,x,y):
# print(‘%s is learning‘%self.Name)
# print(x,y)
# def choose_course(self):
# print(‘choose course‘)
# stu1=Student(‘李三炮‘,‘18‘,‘男‘)
# stu2=Student(‘张美丽‘,‘18‘,‘女‘)
# stu3=Student(‘二狗子‘,‘18‘,‘男‘)
# stu1.learn()
# stu2.learn()
# stu3.learn() # 报错
# stu1.learn(1,2)
# stu2.learn(5,2)
# stu3.learn(2,0)
‘‘‘
李三炮 is learning
1 2
张美丽 is learning
5 2
二狗子 is learning
2 0
‘‘‘
# print(Student.learn)
‘‘‘
<function Student.learn at 0x000002A827A64EE8>
‘‘‘
# Student.learn(stu1,1,2)
‘‘‘
李三炮 is learning
1 2
‘‘‘
# class Student:
# school=‘oldboy‘
# def __init__(self,name,age,gender):
# self.Name=name
# self.Age = age
# self.Gender=gender
# def learn(self,x,y):
# print(‘%s is learning‘%self.Name)
# print(x,y)
# def choose_course(self):
# print(‘choose course‘)
# def commit_hw():
# print(‘commit homework‘)
# stu1=Student(‘李三炮‘,‘18‘,‘男‘)
# stu2=Student(‘张美丽‘,‘18‘,‘女‘)
# stu3=Student(‘二狗子‘,‘18‘,‘男‘)
#
# stu1.commit_hw()
‘‘‘
TypeError: commit_hw() takes 0 positional arguments but 1 was given
‘‘‘
# class Student:
# school=‘oldboy‘
# count=0
# def __init__(self,name,age,gender):
# self.Name=name
# self.Age = age
# self.Gender=gender
# Student.count+=1
# def learn(self):
# print(‘%s is learning‘%self.Name)
# def choose_course(self):
# print(‘choose course‘)
# stu1=Student(‘李三炮‘,‘18‘,‘男‘)
# stu2=Student(‘张美丽‘,‘18‘,‘女‘)
#
# print(stu1.count)
# print(stu2.count)
‘‘‘
2
2
‘‘‘
以上是关于属性查找与绑定方法的主要内容,如果未能解决你的问题,请参考以下文章
面向对象,为对象定制自己独有的属性, 属性查找, 绑定方法, 类即类型, 继承与派生, 继承的应用