Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:15:07+00:00 2026-05-16T10:15:07+00:00

I am looking for something similar to ‘clear’ in Matlab: A command/function which removes

  • 0

I am looking for something similar to ‘clear’ in Matlab: A command/function which removes all variables from the workspace, releasing them from system memory. Is there such a thing in Python?

EDIT: I want to write a script which at some point clears all the variables.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:15:08+00:00Added an answer on May 16, 2026 at 10:15 am

    The following sequence of commands does remove every name from the current module:

    >>> import sys
    >>> sys.modules[__name__].__dict__.clear()
    

    I doubt you actually DO want to do this, because “every name” includes all built-ins, so there’s not much you can do after such a total wipe-out. Remember, in Python there is really no such thing as a “variable” — there are objects, of many kinds (including modules, functions, class, numbers, strings, …), and there are names, bound to objects; what the sequence does is remove every name from a module (the corresponding objects go away if and only if every reference to them has just been removed).

    Maybe you want to be more selective, but it’s hard to guess exactly what you mean unless you want to be more specific. But, just to give an example:

    >>> import sys
    >>> this = sys.modules[__name__]
    >>> for n in dir():
    ...   if n[0]!='_': delattr(this, n)
    ... 
    >>>
    

    This sequence leaves alone names that are private or magical, including the __builtins__ special name which houses all built-in names. So, built-ins still work — for example:

    >>> dir()
    ['__builtins__', '__doc__', '__name__', '__package__', 'n']
    >>> 
    

    As you see, name n (the control variable in that for) also happens to stick around (as it’s re-bound in the for clause every time through), so it might be better to name that control variable _, for example, to clearly show “it’s special” (plus, in the interactive interpreter, name _ is re-bound anyway after every complete expression entered at the prompt, to the value of that expression, so it won’t stick around for long;-).

    Anyway, once you have determined exactly what it is you want to do, it’s not hard to define a function for the purpose and put it in your start-up file (if you want it only in interactive sessions) or site-customize file (if you want it in every script).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for something similar to the table creation form from phpmyadmin, but it
I just switched over from iPhone to Android and am looking for something similar
I'm looking for something similar to Groovy's every() method, which tests every element of
Looking for something similar to xerces for parsing an xml file in ruby. I
I am looking for something similar to array_chunk() but using an empty value as
I'm looking for something similar to this: Active navigation with jQuery - can't apply
I'm looking for something similar to facebook suggestion list with cities and countries. You
i'm looking into implementing something similar to the li's in chrome extension page .
I'm really looking for something very similar to the way SO is setup where
I am looking for a way to use a UILabel (or something similar) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.