Consider the following simple environment:
x1 = 5
x2 = 1
x3 = 100
y = 'Desk'
deletes = [x for x in dir() if x.lower().startswith('x')]
print(deletes)
for i in deletes:
del i
print(x1)
I want to delete all objects in my current environment that begin with a certain substring, in this case such that only y
remains. However, Python still recognizes x1
when I print
it below the loop, which means it's not yet been deleted. I know I could just do
del x1, x2, x3
, but I don't want to because I want to be able to delete all objects to the n-th degree, and would not want to do del x1...,x100
for every single object in question. How do I fix this?