Python 內建類屬性

2021-09-14 04:51:54 字數 1431 閱讀 9256

__dict__ : 類的屬性(包含乙個字典,由類的資料屬性組成)

__doc__ :類的文件字串

__name__: 類名

__module__: 類定義所在的模組(類的全名是』__main__.classname』,如果類位於乙個匯入模組mymod中,那麼classname.__module__ 等於 mymod)

__bases__ : 類的所有父類構成元素(包含了以個由所有父類組成的元組)

python內建類屬性呼叫例項如下:

#!

/usr/bin/python

# -*

- coding:

utf-8-

*-class

employee()

:'''所有員工的基類'

'' empcount =

0 def __init__

(self, name, salary)

: self.name = name

self.salary = salary

employee.empcount +=

1

def displaycount

(self)

:print

("total employee %d"

% employee.empcount)

def displayemployee

(self)

:print

("name : "

, self.name,

", salary: "

, self.salary)

print

("employee.__doc__:"

, employee.__doc__)

print

("employee.__name__:"

, employee.__name__)

print

("employee.__module__:"

, employee.__module__)

print

("employee.__bases__:"

, employee.__bases__)

print

("employee.__dict__:"

, employee.__dict__)

輸入結果:

employee.__doc__: 所有員工的基類

employee.__name__: employee

employee.__module__: __main__

employee.__bases__:

(<

class

'object'

>,)

employee.__dict__:

python內建類屬性

name 內建屬性,如果直接執行該模組,name main 如果import乙個模組,該模組的 name 模組名 if name main 判斷是否直接執行的該模組 dict 類的屬性 包含乙個字典,由類的資料屬性組成 doc 類的文件字串 module 類定義所在的模組 類的全名是 main cl...

python 類屬性 用處 Python類屬性詳解

類屬性1.類定義後就存在,而且不需要例項化 2.類屬性使得相同類的不同例項共同持有相同變數 類屬性例項 attrb.py class testcss cssa class attribe def init self self.a 0 self.b 10 def info self print a s...

python限定類屬性的類屬性 slots

slots 由於python是動態語言,任何例項在執行期都可以動態地新增屬性。如果要限制新增的屬性,例如,student類只允許新增 name gender和score 這3個屬性,就可以利用python的乙個特殊的 slots 來實現。顧名思義,slots 是指乙個類允許的屬性列表 class s...