Explain Codes LogoExplain Codes Logo

Viewing all defined variables

python
variable-types
variable-inspection
state-management
Alex KataevbyAlex Kataev·Dec 10, 2024
TLDR

Check all current local variables with pprint:

from pprint import pprint pprint(locals()) # the dictionary of local variables: easy, peasy, lemon squeezy

For global scope, use globals(). You can filter out the built-ins by checking for names that do not start with _. And remember: pprint() is your friend when you want your output as clear as mineral water.

If you're in an environment like IPython or Jupyter Notebook, %whos shows the detailed list of variables, %who gives you just names. Use %store to keep variables between sessions like saving your game before the boss fight.

What science does to my variables?

Understand your variables types:

for name in vars(): if not name.startswith('_'): print(f"{name}: {type(eval(name))}") # Bingo! Here is your variable type

Here, type() is used with eval() to determine the type of each variable. However, remember with great eval() comes great responsibility.

Handy tips for those exploring variables

Temporary Variable storage

Need variables for longer than your script's lifetime, like that pack of pasta in your pantry? Use pickle with IPython's %store:

import pickle # Save 'data' to a file: with open('data.pickle', 'wb') as f: pickle.dump(data, f) # Shazam! Your variable is now a pickle. # Load 'data' from the file later like opening a jar of pickles: with open('data.pickle', 'rb') as f: data = pickle.load(f)

State introspection in computational tasks

Working with IPython and Jupyter Notebooks, you know state introspection is gold. %store provides this capability. Add external libraries to level up your state management game like adding a secret sauce to your burger.

Advanced object inspection

Want to know more about your object, like wanting to know what's hidden in your friend's basement? Here you go:

class MyClass: def __init__(self): self.x = 10 self.y = 20 obj = MyClass() pprint(vars(obj)) # This is your x-ray vision for variables

Pass an object to vars() and it tells you what it's made of.

Magic commands in Jupyter Notebooks

Enjoying magic like Harry Potter? Jupyter Notebooks got you covered! Magic commands like %who and %whos allow variable introspection. Remember, getting lost is not an option—check the IPython Documentation.

Fast, faster, fastest

Filtering out noise

Got too much on your plate? Focus on non-dunder (not starting and ending with __) variables:

{key: value for key, value in globals().items() if not (key.startswith('__') and key.endswith('__'))} # Who needs underscores, anyway?

Tasting the variable values

Curious about what your variables hold? Try vars().values(), but prepare for the potential output avalanche!

for value in vars().values(): print(value) # Hold tight! Here come the values like passion fruit rain

Peeking into modules

Want to know everything about a module or class? dir() and vars() are your searchlights.

import math pprint(dir(math)) # math: what's your secret?