python类class基础
生活随笔
收集整理的這篇文章主要介紹了
python类class基础
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
44、class類: 一、類定義的一般形式: 1、簡(jiǎn)單的形式:實(shí)例化對(duì)象沒有自己獨(dú)有的數(shù)據(jù)屬性。 >>> class fistclass(): ...? ? ?data1 = 'hello world' ### ==>這是類的數(shù)據(jù)屬性或類成員屬性。 ...? ? ?def printdata(self): ###==>這是類的方法,而且這里至少有一個(gè)參數(shù)self!!! ...? ? ? ? ? ? ?print('nihao%s' %self.data1) ... >>> class1 = fistclass() ##==>類實(shí)例化 >>> class1. class1.__class__(? ? ? ? ?class1.__format__(? ? ? ? class1.__le__(? ? ? ? ? ? class1.__reduce_ex__(? ? ?class1.__weakref__ class1.__delattr__(? ? ? ?class1.__ge__(? ? ? ? ? ? class1.__lt__(? ? ? ? ? ? class1.__repr__(? ? ? ? ? class1.data1 class1.__dict__? ? ? ? ? ?class1.__getattribute__(? class1.__module__? ? ? ? ?class1.__setattr__(? ? ? ?class1.printdata( class1.__dir__(? ? ? ? ? ?class1.__gt__(? ? ? ? ? ? class1.__ne__(? ? ? ? ? ? class1.__sizeof__(? ? ? ? class1.__doc__? ? ? ? ? ? class1.__hash__(? ? ? ? ? class1.__new__(? ? ? ? ? ?class1.__str__(? ? ? ? ? ? class1.__eq__(? ? ? ? ? ? class1.__init__(? ? ? ? ? class1.__reduce__(? ? ? ? class1.__subclasshook__(? >>> class1.data1 ##類數(shù)據(jù)屬性調(diào)用 'hello world' >>> class1.printdata() ##類的方法的調(diào)用 nihaohello world 2、定義對(duì)象獨(dú)有的數(shù)據(jù)屬性: >>> class seclass(): ...? ? ?data1 = 'hello seclass' ...? ? ?def setdata(self,x): ...? ? ? ? ? ? ?self.str1 = x ...? ? ?def printdata(self): ...? ? ? ? ? ? ?print(self.str1) ... >>> ins1 = seclass() >>> ins1. ins1.__class__(? ? ? ? ?ins1.__format__(? ? ? ? ins1.__le__(? ? ? ? ? ? ins1.__reduce_ex__(? ? ?ins1.__weakref__ ins1.__delattr__(? ? ? ?ins1.__ge__(? ? ? ? ? ? ins1.__lt__(? ? ? ? ? ? ins1.__repr__(? ? ? ? ? ins1.data1 ins1.__dict__? ? ? ? ? ?ins1.__getattribute__(? ins1.__module__? ? ? ? ?ins1.__setattr__(? ? ? ?ins1.printdata( ins1.__dir__(? ? ? ? ? ?ins1.__gt__(? ? ? ? ? ? ins1.__ne__(? ? ? ? ? ? ins1.__sizeof__(? ? ? ? ins1.setdata( ins1.__doc__? ? ? ? ? ? ins1.__hash__(? ? ? ? ? ins1.__new__(? ? ? ? ? ?ins1.__str__(? ? ? ? ? ? ins1.__eq__(? ? ? ? ? ? ins1.__init__(? ? ? ? ? ins1.__reduce__(? ? ? ? ins1.__subclasshook__(? >>> ins1.data1 'hello seclass' >>> ins1.setdata('abc') >>> ins1.printdata() abc >>> ins2 = seclass() >>> ins2.data1 'hello seclass' >>> ins2.setdata('xy') >>> ins2.printdata() xy 3、__init__()方法: 創(chuàng)建實(shí)例時(shí),python會(huì)自動(dòng)調(diào)用類中的__init__方法,以隱性的為實(shí)例提供屬性。 __init__稱之為構(gòu)造器。 如果類中沒有定義__init__方法,則實(shí)例之初僅僅是創(chuàng)建一個(gè)簡(jiǎn)單的名稱空間。 >>> class thirdclass(): ...? ? ?data3 = 'hello thirdclass' ...? ? ?def __init__(self,who): ...? ? ? ? ? ? ?self.name = who ... >>> ins3 = thirdclass('lucy') >>> ins3. ins3.__class__(? ? ? ? ?ins3.__format__(? ? ? ? ins3.__le__(? ? ? ? ? ? ins3.__reduce_ex__(? ? ?ins3.__weakref__ ins3.__delattr__(? ? ? ?ins3.__ge__(? ? ? ? ? ? ins3.__lt__(? ? ? ? ? ? ins3.__repr__(? ? ? ? ? ins3.data3 ins3.__dict__? ? ? ? ? ?ins3.__getattribute__(? ins3.__module__? ? ? ? ?ins3.__setattr__(? ? ? ?ins3.name ins3.__dir__(? ? ? ? ? ?ins3.__gt__(? ? ? ? ? ? ins3.__ne__(? ? ? ? ? ? ins3.__sizeof__(? ? ? ? ins3.__doc__? ? ? ? ? ? ins3.__hash__(? ? ? ? ? ins3.__new__(? ? ? ? ? ?ins3.__str__(? ? ? ? ? ? ins3.__eq__(? ? ? ? ? ? ins3.__init__(? ? ? ? ? ins3.__reduce__(? ? ? ? ins3.__subclasshook__(? >>> ins3.data3 'hello thirdclass' >>> ins3.name 'lucy' 4、__dict__: >>> ins3.__dict__ 實(shí)例的屬性 {'name': 'lucy'} >>> thirdclass.__dict__ 類的屬性。 mappingproxy({'data3': 'hello thirdclass', '__init__': <function thirdclass.__init__ at 0x7f8e0a440ea0>, '__dict__': <attribute '__dict__' of 'thirdclass' objects>, '__module__': '__main__', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'thirdclass' objects>}) 5、類中的可用變量: >>> class c1(): ...? ? ?var1 = 'hello c1' ###==>類的靜態(tài)變量!!!! ...? ? ?def __init__(self,who): ...? ? ? ? ? ? ?self.insdata = who ###==>實(shí)例變量。 ...? ? ? ? ? ? ?self.name = '123' ###==>局部變量:只屬于這個(gè)方法。 ... >>> ins1 = c1(12) >>> ins1. ins1.__class__(? ? ? ? ?ins1.__format__(? ? ? ? ins1.__le__(? ? ? ? ? ? ins1.__reduce_ex__(? ? ?ins1.__weakref__ ins1.__delattr__(? ? ? ?ins1.__ge__(? ? ? ? ? ? ins1.__lt__(? ? ? ? ? ? ins1.__repr__(? ? ? ? ? ins1.insdata ins1.__dict__? ? ? ? ? ?ins1.__getattribute__(? ins1.__module__? ? ? ? ?ins1.__setattr__(? ? ? ?ins1.name ins1.__dir__(? ? ? ? ? ?ins1.__gt__(? ? ? ? ? ? ins1.__ne__(? ? ? ? ? ? ins1.__sizeof__(? ? ? ? ins1.var1 ins1.__doc__? ? ? ? ? ? ins1.__hash__(? ? ? ? ? ins1.__new__(? ? ? ? ? ?ins1.__str__(? ? ? ? ? ? ins1.__eq__(? ? ? ? ? ? ins1.__init__(? ? ? ? ? ins1.__reduce__(? ? ? ? ins1.__subclasshook__(? >>> ins1.name '123' >>> ins1.var1 'hello c1' >>> ins1.insdata 12 6、類的繼承:python允許多重繼承,屬性搜索方法:從左往右,從下往上。 >>> class PClass(object): ...? ? ?gender = 'male' ...? ? ?def __init__(self,who): ...? ? ? ? ? ? ?self.name = who ... >>> class CClass(PClass): ...? ? ?def displayinfo(self): ...? ? ? ? ? ? ?print(self.gender,self.name) ... >>> ins1 = CClass('lucy') >>> ins1. ins1.__class__(? ? ? ? ?ins1.__format__(? ? ? ? ins1.__le__(? ? ? ? ? ? ins1.__reduce_ex__(? ? ?ins1.__weakref__ ins1.__delattr__(? ? ? ?ins1.__ge__(? ? ? ? ? ? ins1.__lt__(? ? ? ? ? ? ins1.__repr__(? ? ? ? ? ins1.displayinfo( ins1.__dict__? ? ? ? ? ?ins1.__getattribute__(? ins1.__module__? ? ? ? ?ins1.__setattr__(? ? ? ?ins1.gender ins1.__dir__(? ? ? ? ? ?ins1.__gt__(? ? ? ? ? ? ins1.__ne__(? ? ? ? ? ? ins1.__sizeof__(? ? ? ? ins1.name ins1.__doc__? ? ? ? ? ? ins1.__hash__(? ? ? ? ? ins1.__new__(? ? ? ? ? ?ins1.__str__(? ? ? ? ? ? ins1.__eq__(? ? ? ? ? ? ins1.__init__(? ? ? ? ? ins1.__reduce__(? ? ? ? ins1.__subclasshook__(? >>> ins1.gender 'male' >>> ins1.name 'lucy' >>> ins1.displayinfo() male lucy
轉(zhuǎn)載于:https://www.cnblogs.com/cfj271636063/p/5830590.html
總結(jié)
以上是生活随笔為你收集整理的python类class基础的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: canvas学习之API整理笔记(一)
- 下一篇: ActiveMQ的介绍及使用实例.