I have already seen:
- python - get list of all functions in current module. inspecting current module does not work?
- Finding what methods a Python object has
... and based on that, I made this example:
#!/usr/bin/env python3
import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import inspect
# https://stackoverflow.com/q/18907712 ; https://stackoverflow.com/q/34439
def get_this_file_functions():
funclist = []
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isfunction(obj):
funclist.append(name)
elif inspect.isclass(obj):
for attrname, attr in inspect.getmembers(obj):
if inspect.ismethod(getattr(obj, attrname, None)):
funclist.append(attrname)
return funclist
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
self.init_GUI() # NB: Blocks, because of self.show()!
def init_GUI(self):
self.setGeometry(50, 50, 850, 650)
self.setWindowTitle("Window Title")
print("get_this_file_functions:\n{}".format(get_this_file_functions()))
self.show() # blocks
################################ MAIN
if __name__== '__main__':
app = QApplication(sys.argv)
myGUI = MyMainWindow()
sys.exit(app.exec_())
... however, when I run this, all I get printed is:
get_this_file_functions:
['get_this_file_functions']
... that is - I don't get MyMainWindow.__init__
nor MyMainWindow.init_GUI
.
So, how can I get all functions defined in a file - including all defined class methods?