0

At work, I was given a task to return all global and local variables and their values from the given python code. I tried to do that with a debugger but I don't have enough knowledge and couldn't find sources of how to do that. What can you suggest or maybe you had some similar issue?

I need to write a program that takes as input a python code and returns (in the dictionary, stack, anyway) all local and global variables and their values.

Assel
  • 1
  • 1
  • Does this answer your question? [Viewing all defined variables](https://stackoverflow.com/questions/633127/viewing-all-defined-variables) – trincot Jan 13 '21 at 18:49

1 Answers1

0

You can use dir() function while debugging, it will output any existing name in the environment.

https://docs.python.org/3/library/functions.html#dir

frab
  • 1,162
  • 1
  • 4
  • 14
  • Thanks! I'm not sure how to write new debuggers. Like I need to write class Bdb and then constructor? Is there any example of that? – Assel Jan 13 '21 at 17:56
  • Actually it is not needed to use it in debugging. You can print the output in any part of your code. I don't know your exact use case. – frab Jan 13 '21 at 17:57
  • I need to write program which takes as input a python code and returns variables and their values. – Assel Jan 13 '21 at 17:59
  • As a string? So without running the code? – frab Jan 13 '21 at 18:00
  • In any way and I don't know if the program can run the code by itself. If it can then it's okay – Assel Jan 13 '21 at 18:02
  • If it is a trusted program, you can read the content, append "print(dir())" and eval the string. – frab Jan 13 '21 at 18:03