I am new in Python. There is a function in R called ls()
. I can easily remove any created objects using ls()
and rm()
functions as bellow.
R Code
# Create x and y
x = 1
y = "a"
# remove all created objects
ls()
# [1] "x" "y"
rm(list = ls())
# Try to print x
x
# Error: object 'x' not found
In this post someone suggested an equivalent of ls()
in python. So, I have tried to do the same operation in python.
Python Code
# Create x and y
x = 1
y = "a"
# remove all created objects
for v in dir(): del globals()[v]
# Try to print x
x
# NameError: name 'x' is not defined
But the problem is when x
is recreated and printed it is throwing error:
# Recreate x
x = 1
# Try to print x
x
Traceback (most recent call last):
File "", line 1, in x
File "C:\SomePath\Anaconda\lib\site-packages\IPython\core\displayhook.py", line 258, in call self.update_user_ns(result)
File "C:\SomePath\Anaconda\lib\site-packages\IPython\core\displayhook.py", line 196, in update_user_ns if result is not self.shell.user_ns['_oh']:
KeyError: '_oh'
I have noticed dir()
give some extra objects other than my objects. Is there any function which will give the same output like R's ls()
?