python类基础知识注意点
一、類(lèi)的簡(jiǎn)述
? ? ? ?類(lèi)是面向?qū)ο缶幊痰暮诵膬?nèi)容。通常把具有相同特征(數(shù)據(jù)元素)與行為(功能)的事物描述定義為一個(gè)類(lèi),類(lèi)是一個(gè)抽象的概念,把類(lèi)實(shí)例化既可以得到一個(gè)對(duì)象。
因此,對(duì)象的抽象是類(lèi),類(lèi)的具體化就是對(duì)象,也可以說(shuō)類(lèi)的實(shí)例是對(duì)象,類(lèi)實(shí)際上就是一種數(shù)據(jù)類(lèi)型。
類(lèi)具有屬性,它是對(duì)象的狀態(tài)的抽象,用數(shù)據(jù)結(jié)構(gòu)來(lái)描述類(lèi)的屬性。類(lèi)具有操作,它是對(duì)象的行為的抽象,用操作名和實(shí)現(xiàn)該操作的方法來(lái)描述。
對(duì)象具有狀態(tài),一個(gè)對(duì)象用數(shù)據(jù)值來(lái)描述它的狀態(tài)。對(duì)象還有操作,用于改變對(duì)象的狀態(tài),對(duì)象及其操作就是對(duì)象的行為。
比如:把人類(lèi)即為一個(gè)抽象的類(lèi),“老王”則為一個(gè)的人即對(duì)象。每個(gè)對(duì)象都有一些相同的特征,但具體的數(shù)值卻不一定相同。如:每個(gè)人都有“姓名”,“國(guó)籍”,“年齡”等特征。還具有一些相同的行為,如:“吃飯”,“睡覺(jué)”,“工作”等
可以簡(jiǎn)潔的描述為:Person ({"name", "country", "age"}, {"eat", "sleep", "work"})。
在這里可以看到,類(lèi)有兩種屬性:數(shù)據(jù)屬性,行為屬性。在類(lèi)中行為屬性一般稱(chēng)為“方法”。
二、數(shù)據(jù)屬性
屬性有兩種,類(lèi)屬性,實(shí)例屬性(對(duì)象的屬性),通常把所有對(duì)象共同擁有的屬性定義為類(lèi)屬性,如:每個(gè)人都只有兩只眼等,實(shí)例屬性則時(shí)根據(jù)不同的對(duì)象賦值不一樣的值,如:姓名等
下面來(lái)看一個(gè)簡(jiǎn)單的代碼實(shí)現(xiàn):
class Person(object):
? ? eyes = 2? ? ? ? ?
?#這個(gè)表示類(lèi)的全局變量,這個(gè)變量是類(lèi)的屬性,不是實(shí)例的屬性,每個(gè)
#實(shí)例的值都相同,這個(gè)值,除非去改
? ? def __init__(self, name, age):
? ? ? ? self.name = name
? ? ? ? self.age = age
? ? def eat(self, food):
? ? ? ? print("%s 吃:%s" % (self.name, food))
? ? def eye(self):
? ? ? ? print("%s" % (self.eyes))
p1 = Person("張三", 18)
p2 = Person("李四", 19)
print("類(lèi)的屬性:", Person.__dict__)
print("對(duì)象p1的屬性:", p1.__dict__)
print("對(duì)象p2的屬性:", p2.__dict__)? ? ?#中間的逗號(hào)最后以一個(gè)空格輸出
p1.eat("番茄炒雞蛋")
p1.eye()
#輸出結(jié)果
類(lèi)的屬性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000002059BABB6A8>, 'eat': <function Person.eat at 0x000002059BABB730>, 'eye': <function Person.eye at 0x000002059BABBAE8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
對(duì)象p1的屬性: {'name': '張三', 'age': 18}
對(duì)象p2的屬性: {'name': '李四', 'age': 19}
張三 吃:番茄炒雞蛋
2
先輸出,類(lèi), p1, p2 的屬性,得出結(jié)論
(1)實(shí)例化的對(duì)象只具備自己的數(shù)據(jù)屬性(不包括類(lèi)屬性,即全局變量屬性比如 eyes)
(2)類(lèi)的屬性包含:類(lèi)的數(shù)據(jù)屬性(全局的那些)、函數(shù)屬性。
這里要注意幾點(diǎn):
1)方法的第一個(gè)參數(shù)不用傳值,但必須在定義,因?yàn)镻ython解釋器,做了這樣的一件事,自動(dòng)把調(diào)用的對(duì)象當(dāng)作第一個(gè)參數(shù)傳值給方法,通常定義為self(相當(dāng)與c++ this)
2)對(duì)象訪(fǎng)問(wèn)屬性的過(guò)程,查找屬性__dict__字典,找到就訪(fǎng)問(wèn)這個(gè)屬性,當(dāng)對(duì)象的屬性字典不存在該屬性時(shí),則會(huì)去類(lèi)屬性里邊找,類(lèi)里邊也不存在時(shí)則會(huì)報(bào)錯(cuò)
3)類(lèi)屬性所有通過(guò)該類(lèi)創(chuàng)建的對(duì)象都可以訪(fǎng)問(wèn)
1、類(lèi)屬性的增刪該查
class Person(object):
? ? # 類(lèi)屬性添加的第一種方式
? ? eyes = 2
? ? def __init__(self, name):
? ? ? ? self.name = name
? ? def say(self, w):
? ? ? ? print("%s說(shuō)了%s" % (self.name, w))
print("1--類(lèi)的屬性:", Person.__dict__)
# 類(lèi)屬性添加的第二種方式
Person.arm = 2
print("2--添加類(lèi)的屬性:", Person.__dict__)
# 類(lèi)屬性修改
Person.arm = 1
print("3--修改類(lèi)的屬性:", Person.__dict__)
# 類(lèi)屬性刪除
del Person.eyes
print("4--刪除類(lèi)的屬性:", Person.__dict__
#輸出結(jié)果
1--類(lèi)的屬性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
2--添加類(lèi)的屬性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 2}
3--修改類(lèi)的屬性: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 1}
4--刪除類(lèi)的屬性: {'__module__': '__main__', '__init__': <function Person.__init__ at 0x000001A8F769B6A8>, 'say': <function Person.say at 0x000001A8F769B730>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'arm': 1}
看代碼應(yīng)該就差不多明白類(lèi)屬性的操作了。
注:類(lèi)屬性一般一經(jīng)定義,不會(huì)在執(zhí)行的過(guò)程中增加、刪除類(lèi)的屬性
定義類(lèi)屬性一般只用第一種方法,其它的騷操作了解就好,忘了它吧
2、實(shí)例屬性的增刪改查
class Person(object):
? ? def __init__(self, name):
? ? ? ? # 實(shí)例屬性添加第一種方式
? ? ? ? self.name = name
? ? def say(self, w):
? ? ? ? print("%s說(shuō)了%s" % (self.name, w))
p1 = Person("張三")
p2 = Person("李四")
print("1--p1的屬性:", p1.__dict__)
print("1--p2的屬性:", p2.__dict__)
# 實(shí)例屬性添加第二種方式
p1.age = 20
print("2--p1的屬性:", p1.__dict__)
print("2--p2的屬性:", p2.__dict__)
# 刪除實(shí)例屬性
del p1.name
print("3--p1的屬性:", p1.__dict__)
print("3--p2的屬性:", p2.__dict__)
# 修改實(shí)例屬性
p1.age = 10
print("4--p1的屬性:", p1.__dict__)
print("4--p2的屬性:", p2.__dict__)
# 輸出結(jié)果
1--p1的屬性: {'name': '張三'}
1--p2的屬性: {'name': '李四'}
2--p1的屬性: {'name': '張三', 'age': 20}
2--p2的屬性: {'name': '李四'}
3--p1的屬性: {'age': 20}
3--p2的屬性: {'name': '李四'}
4--p1的屬性: {'age': 10}
4--p2的屬性: {'name': '李四'}
實(shí)例屬性跟類(lèi)屬性的操作差不多。從上也可以得出結(jié)論,對(duì)對(duì)象 p1 的操作并不影響 p2 的屬性。
注:實(shí)例屬性一般放在init方法里邊初始化,不會(huì)在執(zhí)行的過(guò)程中增加、刪除對(duì)象的屬性
三、方法
1、普通的方法
上邊沒(méi)有@符號(hào)修飾,供外部實(shí)例調(diào)用,普通的方法也叫實(shí)例方法,但雖然叫實(shí)例方法但卻與類(lèi)相關(guān),存儲(chǔ)在類(lèi)的屬性中
2、類(lèi)方法
上邊有@classmethod修飾,定義時(shí)第一個(gè)參數(shù)必須為"cls",并通過(guò)cls來(lái)調(diào)用類(lèi)屬性,無(wú)法訪(fǎng)問(wèn)實(shí)例屬性
3、靜態(tài)方法()
上邊有@staticmethod,與類(lèi)相關(guān)但不需要訪(fǎng)問(wèn)屬性,無(wú)法調(diào)用其它方法與屬性
class Person(object):
? ? eyes = 2
? ? def __init__(self, name):
? ? ? ? self.name = name
? ? def say(self, w):? # 普通方法
? ? ? ? print("%s say %s" % (self.name, w))
? ? @classmethod
? ? def cls_md(cls):? # 類(lèi)方法
? ? ? ? print("這是類(lèi)方法", cls.eyes)
? ? @staticmethod
? ? def stat():? # 靜態(tài)方法
? ? ? ? print("這是靜態(tài)方法")
p1 = Person("zhangs")
print(Person.__dict__)
p1.say("hello")
p1.cls_md()
p1.stat()
# 輸出結(jié)果
{'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001DF5205B6A8>, 'say': <function Person.say at 0x000001DF5205B730>, 'cls_md': <classmethod object at 0x000001DF5205ABE0>, 'stat': <staticmethod object at 0x000001DF5205AEB8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
zhangs say hello
這是類(lèi)方法 2
這是靜態(tài)方法
4、方法的增(了解即可)
class Person(object):
? ? eyes = 2
? ? def __init__(self, name):
? ? ? ? self.name = name
def say2(self):? # 普通方法
? ? print("%s 增加普通方法" % (self.name))
@classmethod
def ha2(cls):? # 類(lèi)方法
? ? print("增加類(lèi)方法", cls.eyes)
@staticmethod
def stat2():? # 靜態(tài)方法
? ? print("增加靜態(tài)方法")
print("增加前:", Person.__dict__)
Person.say2 = say2
Person.ha2 = ha2
Person.stat2 = stat2
print("增加后:", Person.__dict__)
p1 = Person("zhangs")
p1.say2()
p1.ha2()
p1.stat2()
# 輸出結(jié)果
增加前: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001468207B6A8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
增加后: {'__module__': '__main__', 'eyes': 2, '__init__': <function Person.__init__ at 0x000001468207B6A8>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'say2': <function say2 at 0x0000014681ECC1E0>, 'ha2': <classmethod object at 0x000001468207A390>, 'stat2': <staticmethod object at 0x000001468207AB70>}
zhangs 增加普通方法
增加類(lèi)方法 2
增加靜態(tài)方法
四、私有化
1、xx:公有變量
2、_xx:單前置下劃線(xiàn),私有化屬性或方法,類(lèi)對(duì)象和子類(lèi)可以訪(fǎng)問(wèn),from module import * 禁止導(dǎo)入,但from module import _xx? 或 Import module還可以導(dǎo)入
3、__xx:雙前置下劃線(xiàn),私有屬性或方法,外部無(wú)法訪(fǎng)問(wèn)到(因?yàn)槊种卣?#xff0c;__xx變?yōu)開(kāi)classname__xx),兼具_(dá)xx的特性
4、__xx__:前后雙下劃線(xiàn),用戶(hù)名空間的魔法對(duì)象或?qū)傩?#xff0c;例如:__init__,一般不��自己定義這樣的變量名
5、xx_:單后置下劃線(xiàn),與python關(guān)鍵字重名+_區(qū)分,不要定義這樣的變量名
總結(jié)
以上是生活随笔為你收集整理的python类基础知识注意点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 创建、修改、删除表
- 下一篇: python调用父类对象的几个方法