類的宣告和函式的宣告形式是差不多的,開始都是關鍵字+自定義名稱
定義例項變數:可以在定義方法的時候直接定義,也可以使用例項來定義
可以使用dir(classname) classname.__dict__
來檢視有哪些類屬性
class
classname:
'the discription of this class'
#這個是類文件說明字串
foo='static attribute'
#在定義類的時候直接定義屬性,這個屬性是類屬性,也就是說是靜態的.
defamethod
(self,parameter):
#這裡定義的方法是例項的方法,也就是跟物件例項繫結的
'discription of this method'
#這是方法說明字串
classname.foo='changed static attribute'
#訪問靜態屬性可以使用classname.foo,
#當然也可以使用self.foo來當做例項變數來訪問
self.foo='instance attribute'
#訪問例項屬性
例項1靜態變數:
fo=10
#這個是全域性變數
class
classname
(object):
"""docstring for classname"""
fo=1
deffoun
(self2):
print(classname.fo)#輸出1
def__init__
(self):
global fo #使用global可以訪問到全域性變數
fo+=1
#訪問全域性變數,python中的全域性變數就是檔案中直接定義的變數與函式和類的位置關係都是並列的
print(fo) #輸出11
classname.fo+=1
#類變數加一
# instance2=classname()
# print(instance2.foo)
print(classname().fo)#輸出2
print(classname().fo)#輸出3
x=60
#這個是全域性變數
deffunc
():global x
print(x)
x=90
print(x)
例項2例項變數:
class
classname
(object):
"""docstring for classname"""
fo=1
def__init__
(self):
self.fo+=1
#例項變數加一,類變數並沒有改變
print(classname().fo)#輸出2
print(classname().fo)#輸出2
print(classname.fo)#輸出1,這裡訪問類變數並沒有因為self.fo+=1而改變
print(classname.fo)#輸出1
例項3例項變數:
class
classname
(object):
"""docstring for classname"""
def__init__
(self):
self.fo=1
#例項變數
instance1=classname()
instance1.foo='instance attribute,can\'t be accessed by class'
print(dir(classname))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
並沒有foo和fo
例項4定義例項變數:
class
classname
(object):
"""docstring for classname"""
def__init__
(self):
self.fo=1
#定義類時定義例項變數
deffun
(self):
attribute1='instance1'
#定義類時定義例項變數
instance1=classname()
instance1.foo='instance attribute,can\'t be accessed by class'
#使用例項定義例項變數
print(classname.__dict__)
輸出:print(classname.attribute1)
發生錯誤:
attributeerror: type object 'classname' has no attribute 'attribute1'
2、如何得知類的屬性有哪些?
使用內建函式dir(class)
使用類的字典屬性:class._ _ dict _ _#這裡會返回乙個字典,
3、一些特殊的類屬性:
c.__name__ #類c的名字(字串)
c.__doc__ #類c的文件字串
c.__bases__ #類c的所有父類構成的元組
c.__dict__ #類c的屬性
c.__module__ #類c定義所在的模組(1.5 版本新增)
c.__class__ 例項c對應的類(僅新式類中)#注意這裡指的是例項
4、類定義所在模組
類定義在所執行的檔案中是,這時模組名為__main__
,
classa.py:
class
classname:
pass
instance=classname
() print(type(instance))
輸出:----------
classb.py
from classa import classname
instance=classname
() print(type(instance))
輸出:
Python資料之class類的屬性和方法
靜態方法 通過 staticmethod裝飾器即可把其裝飾的方法變為乙個靜態方法。普通的方法,可以在例項化後直接呼叫,並且在方法裡可以通過self.呼叫例項變數或類變數。靜態方法是不可以訪問例項變數或類變數的,它與類唯一的關聯就是需要通過類名來呼叫這個方法。class dog object def ...
python裡面的property屬性定義和方式
裝飾器 即 在 法上應 裝飾器 類屬性 即 在類中定義值為property物件的類屬性 class goods property def price self print property price.setter def price self,value print price.setter pr...
Python學習筆記 class中的屬性
python3.x中沒有cmp函式了,定製sorted排序時候,不可以直接return cmp self.score,b.score 要進一步寫詳細。限制屬性種類 slots class person object slots name gender def init self,name,gende...