Search code examples
pythonfunctionpython-3.xstatements

Is it possible to examine the inner statements of a function?


Working from the command line I wrote a function called go(). When called it receives input asking the user for a directory address in the format drive:\directory. No need for extra slashes or quotes or r literal qualifiers or what have you. Once you've provided a directory, it lists all the non-hidden files and directories under it.

I want to update the function now with a statement that stores this location in a variable, so that I can start browsing my hierarchy without specifying the full address every time.

Unfortunately I don't remember what statements I put in the function in the first place to make it work as it does. I know it's simple and I could just look it up and rebuild it from scratch with not too much effort, but that isn't the point.

As someone who is trying to learn the language, I try to stay at the command line as much as possible, only visiting the browser when I need to learn something NEW. Having to refer to obscure findings attached to vaguely related questions to rediscover how to do things I've already done is very cumbersome.

So my question is, can I see the contents of functions I have written, and how?


Solution

  • Unfortunately no. Python does not have this level of introspection. Best you can do is see the compiled byte code.

    The inspect module details what information is available at runtime: https://docs.python.org/3.5/library/inspect.html