0

I'd like to know if I can somehow list/display the function that I'm importing in Python.
Let's say I have 2 files:

functions.py

def add(a,b):
    c = a + b
    print(c)

calc.py

from functions import *

So I want to see the whole code of the add() function without checking the functions.py file. Is that possible? I found that functions are not iterable, thus I can't print them with a for loop.

Maybe it's silly or useless, but I have a function that's imported in Replit and I'd like to see the code of the function. Thank you.

DeLucasso
  • 49
  • 6
  • https://stackoverflow.com/q/427453/10703473 – Gary Oct 14 '21 at 02:07
  • There's this, https://stackoverflow.com/a/427533/1076480, but perhaps your code has outgrown a repl. – David Ehrmann Oct 14 '21 at 02:07
  • Thanks, @DavidEhrmann! This worked just fine in my code, but not in Replit. `import inspect` `lines = inspect.getsource(foo)` `print(foo)` – DeLucasso Oct 14 '21 at 02:11
  • 1
    Did you see the caveat? I'm guessing you're not really interested in an implementation of `add()`. – David Ehrmann Oct 14 '21 at 02:14
  • @DeLucasso how did it "not work". You really *must* give a proper problem specification. Note, the linked duplicate will not work for various kinds of functions, e.g. built-in functions, functions that are written as C-extensions, basically, only for functions that are written as standard Python modules. – juanpa.arrivillaga Oct 14 '21 at 02:27
  • @juanpa.arrivillaga ok thank you. .inspect worked for my functions that are in separate files, but returned "" in replit. I'm trying to get a code of clear() function in replit. import inspect lines = inspect.getsource(clear) print(clear). Anyway, it's a silly try to see this code source, but I've learned here how to use .inspect outside replit. – DeLucasso Oct 14 '21 at 05:50

0 Answers0