這里提供在使用python進行開發(fā)中常使用到的方法技巧,如有不對歡迎批評指正。
要點:開發(fā)中類、變量特性查詢,類型就是類,斷言的使用,深淺復制判斷等
python腳本文件是使用UTF-8編碼的,所以在發(fā)現(xiàn)中文字符出現(xiàn)亂碼時應當考慮是否文本文件采用UTF-8編碼。
如果想指定不同的編碼需要在源碼文件中開頭處添加這樣的注釋:
# -*- coding: utf-8 -*-
如果python在linux和unix系統(tǒng)中運行,需要在源碼的第一行添加:
#!/usr/bin/python3
如何獲取python中各個模塊,變量,類等的內(nèi)容呢?
python中關(guān)于幫組的查詢可以通過變量__all__,__dict__,函數(shù)help(),dir()來獲取。
如果__all__在類,模塊中定義,一般包含著外部可以調(diào)用的特性,如果定義了沒有賦值,會自動使用非下劃線開頭的特性填充。如果沒有參數(shù)__all__python會提示AttributeError屬性錯誤。
__dict__一般會給出類或者模塊中定義的特性(包括屬性和方法),是以字典的形式輸出的使用元組來表示參數(shù)和參數(shù)值(參數(shù),參數(shù)值或者描述)
list的__dict__查看
>>> list.__dict__2 mappingproxy({'__repr__': <slot wrapper '__repr__' of 'list' objects>, '__hash__': None, '__getattribute__': <slot wrapper '__getattribute__' of 'list' objects>, '__lt__': <slot wrapper '__lt__' of 'list' objects>, '__le__': <slot wrapper '__le__' of 'list' objects>, '__eq__': <slot wrapper '__eq__' of 'list' objects>, '__ne__': <slot wrapper '__ne__' of 'list' objects>, '__gt__': <slot wrapper '__gt__' of 'list' objects>, '__ge__': <slot wrapper '__ge__' of 'list' objects>, '__iter__': <slot wrapper '__iter__' of 'list' objects>, '__init__': <slot wrapper '__init__' of 'list' objects>, '__len__': <slot wrapper '__len__' of 'list' objects>, '__getitem__': <method '__getitem__' of 'list' objects>, '__setitem__': <slot wrapper '__setitem__' of 'list' objects>, '__delitem__': <slot wrapper '__delitem__' of 'list' objects>, '__add__': <slot wrapper '__add__' of 'list' objects>, '__mul__': <slot wrapper '__mul__' of 'list' objects>, '__rmul__': <slot wrapper '__rmul__' of 'list' objects>, '__contains__': <slot wrapper '__contains__' of 'list' objects>, '__iadd__': <slot wrapper '__iadd__' of 'list' objects>, '__imul__': <slot wrapper '__imul__' of 'list' objects>, '__new__': <built-in method __new__ of type object at 0x000000005BBAF530>, '__reversed__': <method '__reversed__' of 'list' objects>, '__sizeof__': <method '__sizeof__' of 'list' objects>, 'clear': <method 'clear' of 'list' objects>, 'copy': <method 'copy' of 'list' objects>, 'append': <method 'append' of 'list' objects>, 'insert': <method 'insert' of 'list' objects>, 'extend': <method 'extend' of 'list' objects>, 'pop': <method 'pop' of 'list' objects>, 'remove': <method 'remove' of 'list' objects>, 'index': <method 'index' of 'list' objects>, 'count': <method 'count' of 'list' objects>, 'reverse': <method 'reverse' of 'list' objects>, 'sort': <method 'sort' of 'list' objects>, '__doc__': "list() -> new empty list/nlist(iterable) -> new list initialized from iterable's items"})
新聞熱點
疑難解答