2012年6月8日

python 物件型态取得使用方法协助-dir 指令


python 物件型态取得使用方法协助-dir 指令
当你对python物件使用上有不清楚或想知道更多使用方法时,可利用dir或help的指令直接对已指定值的变数或方法套上这两个指令即可。
当你呼叫内建的dir函式时,会传回来指定物件可用的属性及方法串列。

>>> S='hello'>>> dir(S)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode','encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']>>>
dir 会显示此物件的所有方法串列,有底线的代表是此物件的实作,前后双底线的是python用于实作细节的命名模式,一般我们是用不到。串列中没有底线的名称是可以对此物件呼叫的方法或属性。

没有评论:

发表评论

BlogAD