0

I am currently using online python interpreter. The problem is here, there are some pre-loaded variables or functions which do not appear on the interactive editor, just there is an instruction which informs their name and their final output.

The question here is, how to find out the structure of the functions or in other words how to print their scopes?

For instance, instruction indicates the name of a function is get_mse() and It operates Mean Squared Error and also gets three arguments, then It is ready to use but I want to know what It consists of.

Note: These imported functions are not built-in and certainly manipulated and imported from other python files which are not available from the source.

JoeyBright
  • 343
  • 1
  • 3
  • 17
  • 2
    use locals() and globals() dict to get value within scope ; if u want the source use inspect method – Smart Manoj Apr 02 '18 at 14:11
  • Am I understanding correctly you want to get the source of an imported function... or...? – Jon Clements Apr 02 '18 at 14:11
  • If you're looking for the source code, it is unlikely you'll be able to get it. [See this for some methods to try](https://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function). – Norrius Apr 02 '18 at 14:12
  • @JonClements Yes, That is – JoeyBright Apr 02 '18 at 14:17
  • @Norrius, I had seen this question but if you considered, this method worked when the function is accessible from the source. – JoeyBright Apr 02 '18 at 14:19
  • @JonClements, I have seen this question before asking here but there are a little bit differences. In that question, the top rated answer mentioned the condition is true when 'the function is from a source file available on the filesystem'. – JoeyBright Apr 02 '18 at 14:23
  • 1
    you would need the `get_mse.__code__` object and then you could take an approach like [this one](https://stackoverflow.com/a/15432550/3220135). Getting that code object onto an interpreter that you can import `dis` or `uncompyle2` on however isn't going to be terribly straightforward. I guess the first thing to try is importing `dis` on the interpreter you have.. – Aaron Apr 02 '18 at 14:23
  • @JoyeBright then you need to [edit] your question to include all the bits you've tried and why they don't work for you. Heck - an online interpreter could even inject things into its own builtins to do all sorts of things - so there's possibly not even really any code anyway... – Jon Clements Apr 02 '18 at 14:25

0 Answers0