Python常用的內建函式(物件操作)

2021-10-11 16:19:57 字數 1103 閱讀 8973

class objectoperator(unittest.testcase):

# 返回物件或者當前作用域內的屬性列表

def test_dir(self):

print(dir(math))

# 返回物件的唯一識別符號(位址值)

def test_id(self):

a = 'hello world'

print(id(a))

# 獲取物件的雜湊值

def test_hash(self):

print(hash('hello world'))

# 返回物件的型別,或者根據傳入的引數建立乙個新的型別

def test_type(self):

print(type('hello world'))

# 返回可迭代物件的長度

def test_len(self):

print(len('abcd'))

print(len())

print(len([1, 2, 3, 4]))

# 判斷物件是否是類或者型別元組中任意類元素的例項

def test_isinstance(self):

print(isinstance(1, int)) # true

print(isinstance(1, str)) # false

print(isinstance(1, (int, str))) # true

# 判斷類是否是另外乙個類或者型別元組中任意類元素的子類

def test_issubclass(self):

print(issubclass(bool, int)) # true

print(issubclass(bool, str)) # false

print(issubclass(bool, (str, int))) # true

# 檢測物件是否可被呼叫

def test_callable(self):

print(callable(objectoperator)) # true

o = objectoperator()

print(callable(o)) # true

python 常用的內建函式

filter function,sequence 對sequence 中的item 依次執行 function item 將執行結果為 true 的item 組成乙個 list string tuple 取決於 sequence 的型別 deff x returnx 2 0 printfilter ...

python常用的內建函式

可自定義重寫該方法,這也是python多型的體現 示例 class person def init self,name self.name name def str self return 姓名 format self.name def del self print 物件即將銷毀 當獲取不存在的屬性...

Python 常用的內建函式

build in function,啟動python直譯器,輸入dir builtins 可以看到很多python直譯器啟動後預設載入的屬性和函式,這些函式稱之為內建函式,這些函式因為在程式設計時使用較多,cpython直譯器用c語言實現了這些函式,啟動直譯器 時預設載入。rangerange st...