dir() is a built-in function to store all the variables inside a program along with the built-in variable functions and methods. It creates a list of all declared and built-in variables. There are two different ways to view all defined variables using dir( ).
Variables The Variables pane enables you to examine the values stored in the objects of your application. When a stack frame is selected in the Frames pane, the Variables pane displays all data within its scope (method parameters, local and instance variables).
The only reliable way to know what type a given object is is to inspect it at runtime - which is usually done using a step debugger (either python's builtin pdb or PyCharm's own debugger). Note that you'll have to check the various code path that can lead to the portion of the code you want to inspect though.
The variable list is available in the python console Tools --> Run Python Console... as shown in the screen shot below. Similar functionality for showing variables and watched variables is available in the debugger console.
I like Spyder for interacting with my variables and PyCharm for editing my scripts. Alternative Solution: use both simultaneously. As I edit in PyCharm (on Mac OS), the script updates live in spyder. Best of both worlds!
For your second question: you can also select your code and press shift + alt + E
to run a part of your script on to the python console
If you want to inspect variables that contain arrays or datasets, like Spyder and see them as a nice table, you can do bellow :
1- Put a breakpoint after the variable that you like to inspect ( in my case, it's dataset
) :
2- Run the debugger ( the little bug on the top-right side of pyCharm).
The debugger will then stop on the line and you'll see something like below in your debugger window at the bottom of the pyCharm.
3- Right click on the variable and select View As DataFrame
4- You then will be presented by a nice table like below :
PyCharm has SciView for exploring variables in almost exactly the same manner as Spyder. Simply execute the selection or cell in console, then click View as Array in the Special Variable pane. Special Variable Pane
The simple trick:
put any dummy line at the end of your code, e.g.
print('hi')
and set break point at this line.
Then, run your code in debug mode. Enjoy! The screenshots of editor and debug window are shown for your reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With