Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i get all methods of a python object?

Tags:

python

I want to get every methods of an object. I know about the function dir but it returns all(method, attr, meta_data).

I tried this:

[x for x in dir(obj) if "_" not in x]

but it does not work correctly.

How can I do it?

like image 663
Tany Avatar asked Apr 01 '26 04:04

Tany


2 Answers

you need see inspect. For example

inspect.getmembers(object, inspect.ismethod)

it returns only method.

like image 113
Anna Avatar answered Apr 02 '26 18:04

Anna


You can filter dir result

[method for method in dir(obj) if callable(getattr(obj, method))]
like image 23
itzMEonTV Avatar answered Apr 02 '26 20:04

itzMEonTV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!