1

To remove a variable / object in Python I always use:

del variable
del [variable1, variable2] # removing multiple variables

How can I extend this, so I can delete all variables / objects ending with a certain pattern?

The reason I'm asking is that I'd like to remove all my temporary objects. I always name these such that they end with _tmp.

Anonymous
  • 502
  • 4
  • 23
  • 1
    "I'd like to remove all my temporary objects" - but why? Python's garbage collector does this for you. – meowgoesthedog Jul 17 '19 at 10:48
  • Possible duplicate of [Is there a way to delete created variables, functions, etc from the memory of the interpreter?](https://stackoverflow.com/questions/26545051/is-there-a-way-to-delete-created-variables-functions-etc-from-the-memory-of-th) – Thierry Lathuille Jul 17 '19 at 10:48
  • @meowgoesthedog I basically want to do this so the overview in Python's Variable Explorer remains clean. For example, I might create a new dataframe and to do that I first create 5 temporary objects / variables that are then shown in my Variable Explorer. Those 5 objects I would give a name ending with `_tmp` so that I'd like to delete all those again. Does that make sense? – Anonymous Jul 17 '19 at 10:52
  • So you are using an IDE + debugger - should have mentioned that in the question body. Python itself has no such thing as a "Variable Explorer", and the point of using a managed language in the first place is that the programmer would not need to manually free up memory. – meowgoesthedog Jul 17 '19 at 10:56
  • 2
    If they're hanging around making debugging difficult, it suggests you have a much wider scope than you need. For instance make the dataframe in a function, then the variables will be automatically removed from the variable explorer as soon as the function returns. – Holloway Jul 17 '19 at 10:57
  • What is "Python's Variable Explorer " ??? it's not part of the stdlib AFAIK ? – bruno desthuilliers Jul 17 '19 at 10:57
  • @brunodesthuilliers, google suggests it part of the Spyder IDE but it could be any IDE debugging view. – Holloway Jul 17 '19 at 10:59
  • And yet another XY problem. There's certainly a way to show/hide things in your IDE / debugger, and as Holloway mentions, if you have so many variables in a function that it begins to be a readability issue then it's a sure smell that it's refactoring time. – bruno desthuilliers Jul 17 '19 at 11:01
  • @Holloway yep I saw this too... – bruno desthuilliers Jul 17 '19 at 11:01
  • Sorry, I am new to Python and wasn't aware that the Variable Explorer isn't 'standard'. As @Holloway says, I indeed use Spyder's IDE. Also thanks for pointing out a function would make more sense in such a case, I hadn't thought of it. In the meanwhile I found a solution based on the thread that Thierry Lathuille suggested. – Anonymous Jul 17 '19 at 11:03
  • 1
    @Ghassen wasn't me. Although your suggestion doesn't work anyways. – Anonymous Jul 17 '19 at 11:15

1 Answers1

3

As an alternative method, but which would solve your problem. you could make use of a dictionary. So if you keep all your temporary objects in a dictionary, you would need to only delete that.

temp_dict = {'variable1_tmp': 1234, 'variable2_tmp': 'hello'}
# remove only one temp
del temp_dict['variable1_tmp']
# remove all temp
del temp_dict

Although that is not dynamically deleting variables, as you asked. Hopefully that might suit your use case anyway.

ptolemy0
  • 799
  • 1
  • 5
  • 13