0

I have a number of declared variables at the start of a long script. A real mix of types. Is there anyway to do this sort of thing

# pseudo code

i = 1 
b = "hello world"

for var in AllDeclaredVariables:
    print (var.name, end='')
    print type(var, end='')
    print (var.value)

And get this

 a<class 'int'>1
 b<class 'str'>hello world
Maxcot
  • 1,513
  • 3
  • 23
  • 51
  • 3
    You don't want to do that. But if you're sure that you know what you're doing, there's [`globals()`](https://docs.python.org/3/library/functions.html#globals) for global scope and [`locals()`](https://docs.python.org/3/library/functions.html#locals) for local. – Olvin Roght May 09 '21 at 20:42
  • 3
    Why would you want to do this? This speaks to a fundamental design flaw in your program. If you need to iterate over some values, then *put those values in a container*, like a `list` or a `dict` – juanpa.arrivillaga May 09 '21 at 20:43
  • @ juanpa.arrivillaga well, call it my learning curve. I'm just a hobbyist programmer, and "good design" is something I learn by failing first. Thanks for the pointer. – Maxcot May 09 '21 at 20:49
  • @Olvin Roght OK, thanks for the pointer, I did not know that. – Maxcot May 09 '21 at 20:50
  • @ norie - Yes, thanks. – Maxcot May 09 '21 at 20:51
  • 1
    @Maxcot seriously, you shouldn't use that. Again, think ahead when you are creating your program if you are going to need to group things together, be it in a built-in container or user-defined object, then do that. Using `globals` and `locals` is a hack. – juanpa.arrivillaga May 09 '21 at 21:10

0 Answers0