程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to query the methods (member functions) of a class or an object in Python [using the function dir()]

編輯:Python

How to be in Python Query the methods of a class or an object in ( Member functions )?
answer : Use functions dir()

Example code for querying which methods an object has is as follows :

dict1 = {
'name': 'suwenhao', 'likes': 'reading', 123: 456}
print(dir(dict1))

The operation results are as follows :

C:\Users\Administrator\AppData\Local\Programs\Python\Python39\python.exe E:/Project/PycharmProjects/P-001/Files/P-001/current/001-00- Temporary test script .py
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

The example code of which methods of the query class are as follows :

print(dir(dict))

The operation results are as follows :

C:\Users\Administrator\AppData\Local\Programs\Python\Python39\python.exe E:/Project/PycharmProjects/P-001/Files/P-001/current/001-00- Temporary test script .py
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

so , The running results of the two sample codes are the same .
This shows which methods of the query object are actually the methods of the class of the query object .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved