user1234440 user1234440 - 4 months ago 7
Python Question

List All functions in file Python

I have a file structure:

A
|
main.py
|_B
|a_funcs.py


A
is the main folder while
B
is a sub-folder inside
A
. In my
main.py
filer, I have
from B.a_funcs import *
. This statement imports all functions from the
a_func.py
file. Is it possible to list out all the functions inside that
a_func.py
file`? How do I do that?

I tried
dir(a_func)
but that says a_func is not defined.

Answer

That is because you didn't explicitly import the name a_func. Instead of from B.a_func import * (which creates potential namespace collisions) do from B import a_func, and then call functions with a_func.. This is better practice, and will let you do what you want.

Comments