您好,登錄后才能下訂單哦!
類有兩種屬性
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。