Based on this answer to 'Iterating through globals() dictionary', you can typecast your globals().values()
to a list so that you can iterate on it without the error RuntimeError: dictionary changed size during iteration
, which is presently what your code with for f in globals().values():
gives. Here's that change incorporated:
# Get a list of all user-defined functions and print their definitions
import inspect, sys, types
# sys.stdout.write(inspect.getsource(MyFunction))
#
print(sorted([f.__name__ for f in globals().values() if type(f) == types.FunctionType]))
print()
for f in list(globals().values()):
if type(f) == types.FunctionType:
## %pdef $f.__name__ is only the call signature
print(inspect.getsource(f))
The solution was found by running your code and seeing the error and then searching about errors iterating on globals. So in the future, it is suggested when posting to provide the error you see and what you expect as @juanpa.arrivillaga suggested. Also, please provide any steps you've already tried to fix it. In this case, you may have sorted it yourself by following that process as it was a direct substitution suggested. Granted, when learning it is often harder to understand what to search and how to effectively sort and implement those findings.