assertionerror python_Python成为专业人士笔记–内置模块Modules和函数Functions
生活随笔
收集整理的這篇文章主要介紹了
assertionerror python_Python成为专业人士笔记–内置模块Modules和函数Functions
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
“專業人士筆記”系列目錄:
創帆云:Python成為專業人士筆記--強烈建議收藏!每日持續更新!?zhuanlan.zhihu.com模塊是一個包含Python定義和語句的文件,而函數是執行邏輯的一段代碼 。
要檢查python中內置的函數,可以使用dir()。如果調用的時候不帶任何參數,則返回當前范圍中的名稱。否則,返回一個按字母順序排列的名稱列表,其中包含(一些)給定對象的屬性,以及從中可以訪問的屬性。
嘗試運行如下命令顯示所有函數:
dir(builtins) 輸出: ['ArithmeticError','AssertionError','AttributeError','BaseException','BufferError','BytesWarning','DeprecationWarning','EOFError','Ellipsis','EnvironmentError','Exception','False','FloatingPointError','FutureWarning','GeneratorExit','IOError','ImportError','ImportWarning','IndentationError','IndexError','KeyError','KeyboardInterrupt','LookupError','MemoryError','NameError','None','NotImplemented','NotImplementedError','OSError','OverflowError','PendingDeprecationWarning','ReferenceError','RuntimeError','RuntimeWarning','StandardError','StopIteration','SyntaxError','SyntaxWarning','SystemError','SystemExit','TabError','True','TypeError','UnboundLocalError','UnicodeDecodeError','UnicodeEncodeError','UnicodeError','UnicodeTranslateError','UnicodeWarning','UserWarning','ValueError','Warning','ZeroDivisionError','debug','doc', 'import','name','package','abs','all','any','apply','basestring','bin','bool','buffer','bytearray','bytes','callable','chr','classmethod','cmp','coerce','compile','complex','copyright','credits','delattr','dict','dir','divmod','enumerate','eval','execfile','exit','file','filter','float','format','frozenset','getattr','globals','hasattr','hash','help','hex','id','input','int','intern','isinstance','issubclass','iter','len','license','list','locals','long','map','max','memoryview','min','next','object','oct','open','ord', 'pow','print','property','quit','range','raw_input','reduce','reload','repr','reversed','round','set','setattr','slice','sorted','staticmethod','str','sum','super','tuple','type','unichr','unicode','vars','xrange','zip' ]要了解任何函數的功能及屬性,我們可以使用內建函數幫助,方法是命令運行:help(函數名)
比如:
help(max) 輸出:Help on built-in function max in module builtin:max(…)max(iterable[, key=func]) -> valuemax(a, b, c, …[, key=func]) -> value在單個可迭代參數中,返回其最大的項。使用兩個或多個參數,返回最大的參數。而內建模塊則包含一些額外的函數。例如,為了得到一個數字的平方根,我們需要包括數學(math)模塊
import mathmath.sqrt(16) # 輸出4.0為了了解模塊中的所有函數,我們可以將函數名稱分配給一個變量,然后打印該變量。
import math print(dir(math)) 輸出: ['doc', 'name', 'package', 'acos', 'acosh','asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign','cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1','fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma','hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10','log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt','tan', 'tanh', 'trunc']除了函數之外,還可以在模塊中提供文檔。如果你有一個像這樣的名為helloWorld.py的文件
""" 這是模塊內函數的定義."""def sayHello():"""這是模塊內函數的代碼"""return 'Hello World'您可以像這樣訪問它:
import helloWorldhelloWorld.__doc__'這是模塊的描述'helloWorld.sayHello.__doc__'這是函數的描述'對于所有用戶定義的類型,都可以使用dir()來檢索其屬性、類的屬性以及遞歸地檢索其類的基類的屬性
比如,創建一個class類:
class MyClassObject(object):pass我們來檢索它的定義:
dir(MyClassObject) 輸出:['class', 'delattr', 'dict', 'doc', 'format', 'getattribute', 'hash','init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr','sizeof', 'str', 'subclasshook', 'weakref']任何數據類型都可以使用名為str的內置函數簡單地轉換為字符串。在將變量傳遞給print時,默認情況下都會調用該函數 ,比如:
str(123)#輸出 "123",因為默認的print打印都會轉成Str字符串打印到屏幕今天分享就到這里,禁止任何形式轉載,違者必究
總結
以上是生活随笔為你收集整理的assertionerror python_Python成为专业人士笔记–内置模块Modules和函数Functions的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: have的过去式
- 下一篇: jwt私钥和公钥怎么获取_jwt 用rs