面向对象-属性查找与绑定方法

1.属性查找

类有两种属性
1.数据属性:类的数据属性是所有对象共享的
2.函数属性:类的函数属性是绑定给对象使用的,称为绑定到对象的方法

为温县等地区用户提供了全套网页设计制作服务,及温县网站建设行业解决方案。主营业务为成都网站设计、做网站、温县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:

    school = 'my_school'
    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age

    def learn(self): #函数属性
        print('is learning')

    def eat(self): #函数属性
        print('is eating')

    def sleep(self):
        print('is sleeping')
#"1.类中的数据属性是所有对象共享的,id都一样"
s1=Student("vita",'女',27)
s2=Student("超超",'男',29)
print("id(Student.school)",id(Student.school))
print("id(s1.school)",id(s1.school))
print("id(s2.school)",id(s2.school))
#"类的函数是绑定给对象用的,称为绑定到对象的方法,内存地址都不一样"
print("Student.learn",Student.learn)
print("s1.learn",s1.learn)
print("s2.learn",s2.learn)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
id(Student.school) 1427361290736
id(s1.school) 1427361290736
id(s2.school) 1427361290736
Student.learn 
s1.learn >
s2.learn >

Process finished with exit code 0

#对于school变量,会先到s1.__dict__中查找,即先到自己的名称空间中查找
# 找不到,去类中查找
# 类中找不到,去父类中查找,直到找到顶级父类,找不到,抛出异常
#并不会到类的外面查找
"验证变量首先到对象空间中找,然后到类中找,再到父类中找"
"1.先从对象空间中查找,即s1.__dict__中"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
school = 'my_school from out'
class Student:

    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age
s1=Student("vita",'女',27)
s2=Student("超超",'男',29)
s1.school="my_school from s1"
print(s1.__dict__)
print(s1.school)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{'name': 'vita', 'sex': '女', 'age': 27, 'school': 'my_school from s1'}
my_school from s1

Process finished with exit code 0

"2.再从类中找"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
school = 'my_school from out'
class Student:

    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age
s1=Student("vita",'女',27)
s2=Student("超超",'男',29)
Student.school="my_school from class"
print(s1.__dict__)
print(s1.school)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{'name': 'vita', 'sex': '女', 'age': 27}
my_school from class

Process finished with exit code 0

"3.再从父类中找,稍后讲继承后,再看"

"4.类中没有,不会到类外找"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
school = 'my_school from out'
class Student:

    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age
s1=Student("vita",'女',27)
s2=Student("超超",'男',29)

print(s1.__dict__)
print(s1.school)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
{'name': 'vita', 'sex': '女', 'age': 27}
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 15, in 
    print(s1.school)
AttributeError: 'Student' object has no attribute 'school'

Process finished with exit code 1

2.绑定方法

类中定义的函数(没有被任何装饰器装饰的)是类的函数属性,主要是给对象使用的,而且是绑定到对象的,虽然所有对象指向的都是相同的功能,但绑定到不同的对象就是不同的绑定方法
因为绑定给哪个对象,就由哪个对象调用,就会把对象自身当做参数传给self,即自动传值(init方法也是生成对象时,自动运行的)
注意:绑定到对象的方法的这种自动传值的特征,决定了在类中定义的函数都要默认写一个self参数,self可以是任意名字,但默认大家都写为self

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:

    school = 'my_school'
    def __init__(self,name,sex,age):
        self.name=name
        self.sex=sex
        self.age=age

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

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

    def sleep(self):
        print('%s is sleeping'%self.name)

s1=Student("vita",'女',27)
s2=Student("超超",'男',29)
# 验证不同的对象调用learn方法,self.name传值不同,即self所代表的的对象也不同
# s1.learn()中self是s1对象, s2.learn()中self是s2对象
s1.learn()#等同于Student.learn(s1)
s2.learn()#等同于Student.learn(s2)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
vita is learning
超超 is learning

Process finished with exit code 0

3.类即类型

Python中的数据类型也是一种类,与类的操作是相同的

#list类型是类list
>>> list

>>> set

>>> dict

>>> str

#实例化三个对象
>>> l1=list()
>>> l2=list()
>>> l3=list()
#三个对象都有绑定方法append,是相同的功能,但内存地址不同
>>> l1.append

>>> l2.append

>>> l3.append

#操作绑定方法l1.append(3)就是往l1添加3,不会影响l2和l3
>>> l1.append(3)
>>> l1
[3]
>>> l2
[]
>>> l3
[]
#调用类list.append(l3,111)等同于l3.append(111)
>>> list.append(l3,111)
>>> l3
[111]

4.面向对象优点小节

4.1优点1:将数据与操作数据的功能整合到一起

"1.在没有学习类概念时,数据与功能是分离的"
def exc1(host,port,db,charset):
    conn=connect(host,port,db,charset)
    conn.execute(sql)
    return xxx

def exc2(host,port,db,charset,proc_name)
    conn=connect(host,port,db,charset)
    conn.call_proc(sql)
    return xxx

#每次调用都需要重复传入一堆参数
exc1('127.0.0.1',3306,'db1','utf8','select * from tb1;')
exc2('127.0.0.1',3306,'db1','utf8','存储过程的名字')
"2.我们后面用到一个解决办法,即全局变量"
HOST=‘127.0.0.1’
PORT=3306
DB=‘db1’
CHARSET=‘utf8’

def exc1(host,port,db,charset):
    conn=connect(host,port,db,charset)
    conn.execute(sql)
    return xxx

def exc2(host,port,db,charset,proc_name)
    conn=connect(host,port,db,charset)
    conn.call_proc(sql)
    return xxx

exc1(HOST,PORT,DB,CHARSET,'select * from tb1;')
exc2(HOST,PORT,DB,CHARSET,'存储过程的名字')
"3.学到类之后,是这样的
将数据与专门操作该数据的功能整合到一起
"

class MySQLHandler:
    def __init__(self,host,port,db,charset='utf8'):
        self.host=host
        self.port=port
        self.db=db
        self.charset=charset
        self.conn=connect(self.host,self.port,self.db,self.charset)
    def exc1(self,sql):
        return self.conn.execute(sql)

    def exc2(self,sql):
        return self.conn.call_proc(sql)

obj=MySQLHandler('127.0.0.1',3306,'db1')
obj.exc1('select * from tb1;')
obj.exc2('存储过程的名字')

4.2优点2:可扩展性高

"定义类并产生三个对象"
class Chinese:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

p1=Chinese('egon',18,'male')
p2=Chinese('alex',38,'female')
p3=Chinese('wpq',48,'female')
"如果我们新增一个类属性,将会反映给所有对象,而对象却无需修改,直接使用新增的属性即可"
class Chinese:
    country='China'
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
    def tell_info(self):
        info='''
        国籍:%s
        姓名:%s
        年龄:%s
        性别:%s
        ''' %(self.country,self.name,self.age,self.sex)
        print(info)

p1=Chinese('egon',18,'male')
p2=Chinese('alex',38,'female')
p3=Chinese('wpq',48,'female')

print(p1.country)
p1.tell_info()

标题名称:面向对象-属性查找与绑定方法
链接地址:http://ybzwz.com/article/pijeds.html