Getting a list of locally-defined functions in python
问题 If I have a script like this: import sys def square(x): return x*x def cube(x): return x**3 How can I return a list of all the functions defined locally in the program ['square', 'cube'] , and not the ones imported. They are included when I try dir() but so are all the variables and other imported modules. I don't know what to put into dir to refer to the locally executing file. 回答1: l = [] for key, value in locals().items(): if callable(value) and value.__module__ == __name__: l.append(key)