类有两种属性
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 <function Student.learn at 0x0000014C555D1D90>
s1.learn <bound method Student.learn of <__main__.Student object at 0x0000014C555D85C0>>
s2.learn <bound method Student.learn of <__main__.Student object at 0x0000014C555D87F0>>
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 <module>
print(s1.school)
AttributeError: 'Student' object has no attribute 'school'
Process finished with exit code 1
类中定义的函数(没有被任何装饰器装饰的)是类的函数属性,主要是给对象使用的,而且是绑定到对象的,虽然所有对象指向的都是相同的功能,但绑定到不同的对象就是不同的绑定方法
因为绑定给哪个对象,就由哪个对象调用,就会把对象自身当做参数传给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
Python中的数据类型也是一种类,与类的操作是相同的
#list类型是类list
>>> list
<class 'list'>
>>> set
<class 'set'>
>>> dict
<class 'dict'>
>>> str
<class 'str'>
#实例化三个对象
>>> l1=list()
>>> l2=list()
>>> l3=list()
#三个对象都有绑定方法append,是相同的功能,但内存地址不同
>>> l1.append
<built-in method append of list object at 0x0000027FEE617408>
>>> l2.append
<built-in method append of list object at 0x0000027FEE617448>
>>> l3.append
<built-in method append of list object at 0x0000027FEE617488>
#操作绑定方法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]
"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('存储过程的名字')
"定义类并产生三个对象"
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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。