Viewing all defined variables
Check all current local variables with pprint
:
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:
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
:
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:
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:
Tasting the variable values
Curious about what your variables hold? Try vars().values()
, but prepare for the potential output avalanche!
Peeking into modules
Want to know everything about a module or class? dir()
and vars()
are your searchlights.
Was this article helpful?