可以使用python 的内置方法 dir() 或者help() 查看 某个对象所拥有的方法和属性, 二者间的区别是:
dir() : 只是得到方法或者属性的名称
help():不但可以得到对象的方法和属性名称, 同时也可以得到这些方法或者属性的使用方式的描述
dir():
print(dir("str")) # 列出字符串的方法名称和属性名称, 也可以传入某个对象变量名, dir(obj_name)
输出:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
help():
print(help("list")) # 列出列表的方法名称和属性名称,并进行使用方式的描述, 也可以传入某个对象变量名, dir(obj_name)
'''遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!'''Help on class list in module builtins:class list(object)| list() -> new empty list| list(iterable) -> new list initialized from iterable's items| | Methods defined here:| | __add__(self, value, /)|Return self+value.| | __contains__(self, key, /)|Return key in self.| | __delitem__(self, key, /)|Delete self[key].| | __eq__(self, value, /)|Return self==value.| | __ge__(self, value, /)|Return self>=value.| | __getattribute__(self, name, /)|Return getattr(self, name).| | __getitem__(...)|x.__getitem__(y) <==> x[y]| | __gt__(self, value, /)|Return self>value.| | __iadd__(self, value, /)|Implement self+=value.| | __imul__(self, value, /)|Implement self*=value.| | __init__(self, /, *args, **kwargs)|Initialize self. See help(type(self)) for accurate signature.| | __iter__(self, /)|Implement iter(self).| | __le__(self, value, /)|Return self<=value.| | __len__(self, /)|Return len(self).| | __lt__(self, value, /)|Return self<value.| | __mul__(self, value, /)|Return self*value.n| | __ne__(self, value, /)|Return self!=value.| | __new__(*args, **kwargs) from builtins.type|Create and return a new object. See help(type) for accurate signature.| | __repr__(self, /)|Return repr(self).| | __reversed__(...)|L.__reversed__() -- return a reverse iterator over the list| | __rmul__(self, value, /)|Return self*value.| | __setitem__(self, key, value, /)|Set self[key] to value.| | __sizeof__(...)|L.__sizeof__() -- size of L in memory, in bytes| | append(...)|L.append(object) -> None -- append object to end| | clear(...)|L.clear() -> None -- remove all items from L| | copy(...)|L.copy() -> list -- a shallow copy of L| | count(...)|L.count(value) -> integer -- return number of occurrences of value| | extend(...)|L.extend(iterable) -> None -- extend list by appending elements from the iterable| | index(...)|L.index(value, [start, [stop]]) -> integer -- return first index of value.|Raises ValueError if the value is not present.| | insert(...)|L.insert(index, object) -- insert object before index| | pop(...)|L.pop([index]) -> item -- remove and return item at index (default last).|Raises IndexError if list is empty or index is out of range.| | remove(...)|L.remove(value) -> None -- remove first occurrence of value.|Raises ValueError if the value is not present.| | reverse(...)|L.reverse() -- reverse *IN PLACE*| | sort(...)|L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*| | ----------------------------------------------------------------------| Data and other attributes defined here:| | __hash__ = NoneNone
如果觉得《python 使用dir() help() 查看一个对象所有拥有的方法和属性》对你有帮助,请点赞、收藏,并留下你的观点哦!