內(nèi)建函數(shù)Python(H)
- hasattr(object,name)
說明:判斷對象object是否包含名為name的特征(hasattr是通過調(diào)用getattr來判斷的(ojbect,name)是否拋出異常來完成)。
參數(shù)object:目標。
參數(shù)name:特點名字。
示例:
>>> hasattr(list, 'append')
True
>>> hasattr(list, 'add')
False
- hash(object)
說明:如果目標object是哈希表的類型,回到目標object的哈希值。哈希值是整數(shù)。在字典搜索中,哈希值用于快速比較字典的關鍵。如果兩個值相同,哈希值也相同。
參數(shù)object:目標。
示例:
>>> hash(list)
-2115899692
>>> hash(tuple)
-2115901047
>>> hash(int)
-2115899555
>>> hash(1)
1
>>> hash(1.0)
1
- help([object])
說明:調(diào)用內(nèi)部幫助系統(tǒng)。如果不包括參數(shù),互動幫助系統(tǒng)將在控制臺上啟動。如果參數(shù)是字符串,可以是模塊、函數(shù)、類、方法等名稱,幫助頁面會在控制臺上打印。參數(shù)也可以是任意目標。
- hex(x)
說明:將整數(shù)x轉換成16進制字符串。如果你想得到16進制字符串的浮點,你可以使用float.hex(x)。
參數(shù)x:整數(shù)。
示例:
>>> hex(3)
'0x3'
>>> hex(13)
'0xd'
>>> hex(18)
'0x12'
>>> float.hex(3.5)
'0x1.c000000000000p 1'
>>> float.hex(3.0)
'0x1.8000000000000p 1'
- getattr(object,name[,defalut])
說明:獲得目標object名稱name的特性。若object不包含名稱name的特性,則拋出AttributeError異常;如果不包含名稱name的特性,并且提供default參數(shù),則返回default。
參數(shù)object:目標。
參數(shù)name:目標特征名稱。
參數(shù)default:缺少省份的返回值。
示例:
>>> append = getattr(list, 'append')
>>> append
>>> mylist = [3, 4, 5]
>>> mylist
[3, 4, 5]
>>> append(mylist, 6)
>>> mylist
[3, 4, 5, 6]
>>> method = getattr(list, 'add')
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'list' has no attribute 'add'
>>> method = getattr(list, 'add', 'NoMethod')
>>> method
'NoMethod'
- globals()
說明:回到一本關于當前全局符號表的詞典。
示例:
>>> globals()
{'__builtins__': , '__name__': '__main__', '__d
oc__': None, '__package__': None}
- dir([object])
說明:當沒有參數(shù)時,返回當前范圍內(nèi)的變量、方法和定義類型目錄;帶參數(shù)時,返回參數(shù)的屬性和方法目錄。如果參數(shù)包括方法__dir__(),這種方法將被調(diào)用。如參數(shù)不包括__dir__(),這種方法將最大限度地收集參數(shù)信息。
參數(shù)object:目標,變量,類型。
示例:
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import struct
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'struct']
>>> dir(struct)
['Struct', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from']
>>> class Person(object):
... def __dir__(self):
... return ["name", "age", "country"]
...
>>> dir(Person)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce___'____reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> tom = Person()
>>> dir(tom)
['age', 'country', 'name']
- delattr(object,name)
說明:刪除object目標名稱為name的屬性。
參數(shù)object:目標。
參數(shù)name:特殊名字字符串。
示例:
>>> class Person:
... def __init__(self, name, age):
... self.name = name
... self.age = age
...
>>> tom = Person("Tom", 35)
>>> dir(tom)
['__doc__', '__init__', '__module__', 'age', 'name']
>>> delattr(tom, "age")
>>> dir(tom)
['__doc__', '__init__', '__module__', 'name']
- dict([arg])
說明:建立數(shù)據(jù)字典。
示例:
>>> a = dict() #空字典
>>> a
{}
>>> b = dict(one = 1, two = 2)
>>> b
{'two': 2, 'one': 1}
>>> c = dict({'one':1, 'two':2})
>>> c
{'two': 2, 'one': 1}
>>> d= dict([['two', 2], ['one', 1]])
>>> d
{'two': 2, 'one': 1}
>>> e ={'two': 2, 'one': 1}
>>> e
{'two': 2, 'one': 1}
- divmod(a,b)
說明:回到數(shù)據(jù)對,等價于等價。(a//b,a%b)。
參數(shù)a,b:int、long、float。
示例:
>>> divmod(5,3)
(1, 2)
>>> divmod(5.5, 2.2)
(2.0, 1.0999999999999996)
>>> divmod(5.5, 2)
(2.0, 1.5)
本文僅代表作者觀點,版權歸原創(chuàng)者所有,如需轉載請在文中注明來源及作者名字。
免責聲明:本文系轉載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權或非授權發(fā)布,請及時與我們聯(lián)系進行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com


