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

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

2022-06-24 04:26:50Haohong image algorithm

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 :
 Insert picture description here

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 :
 Insert picture description here

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 .

copyright notice
author[Haohong image algorithm],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/175/202206232347596557.html

Random recommended