a short-cut command for globals().clear() ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CapnBearbossa@googlemail.com

    a short-cut command for globals().clear() ??

    hi all,

    forgive me , but the RTFM and Google search approaches are not
    yielding an answer on this question. I need to know if there's a top
    level python interpreter command that clears all user variables (not
    built-ins) from the global namespace. In other words a statement, or
    some_command_or _function(), that does this:
    >>x=3
    >>y=4
    >>z=[]
    >>dir()
    ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
    >>some_command_ or_function()
    >>dir()
    ['__builtins__', '__doc__', '__name__']

    thanks,
    1 desperate snake oil programmer ....
  • Matimus

    #2
    Re: a short-cut command for globals().clear () ??

    On Sep 22, 2:31 pm, CapnBearbo...@g ooglemail.com wrote:
    hi all,
    >
    forgive me , but the RTFM and Google search approaches are not
    yielding an answer on this question.  I need to know if there's a top
    level python interpreter command that clears all user variables (not
    built-ins) from the global namespace.  In other words a statement, or
    some_command_or _function(), that does this:
    >
    >x=3
    >y=4
    >z=[]
    >dir()
    >
    ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
    >
    >some_command_o r_function()
    >dir()
    >
    ['__builtins__', '__doc__', '__name__']
    >
    thanks,
       1 desperate snake oil programmer ....
    I don't think you will find anything. The interpreter is essentially
    the same whether you are in interactive mode or not. That is, there is
    very little use for a method that clears globals in general, so why
    would we add it just so that it could be used by the interpreter.
    There is almost* nothing available to the interactive interpreter
    which isn't part of the core language.

    * The only difference I can think of is the "_" variable, which is
    added to __builtins__ and contains the last value returned in
    interactive mode. If you have ever tried to run code that uses the
    locale module from the interpreter you will see why having any
    differences between the interactive and non-interactive interpreter
    can be a pain.

    Matt

    Comment

    • CapnBearbossa@googlemail.com

      #3
      Re: a short-cut command for globals().clear () ??

      On Sep 22, 5:52 pm, Matimus <mccre...@gmail .comwrote:
      On Sep 22, 2:31 pm, CapnBearbo...@g ooglemail.com wrote:
      >
      >
      >
      hi all,
      >
      forgive me , but the RTFM and Google search approaches are not
      yielding an answer on this question.  I need to know if there's a top
      level python interpreter command that clears all user variables (not
      built-ins) from the global namespace.  In other words a statement, or
      some_command_or _function(), that does this:
      >
      >>x=3
      >>y=4
      >>z=[]
      >>dir()
      >
      ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
      >
      >>some_command_ or_function()
      >>dir()
      >
      ['__builtins__', '__doc__', '__name__']
      >
      thanks,
         1 desperate snake oil programmer ....
      >
      I don't think you will find anything. The interpreter is essentially
      the same whether you are in interactive mode or not. That is, there is
      very little use for a method that clears globals in general, so why
      would we add it just so that it could be used by the interpreter.
      There is almost* nothing available to the interactive interpreter
      which isn't part of the core language.
      >
      * The only difference I can think of is the "_" variable, which is
      added to __builtins__ and contains the last value returned in
      interactive mode. If you have ever tried to run code that uses the
      locale module from the interpreter you will see why having any
      differences between the interactive and non-interactive interpreter
      can be a pain.
      >
      Matt
      ok. thanks! guess i'll be off to define my own function ...

      Comment

      • Terry Reedy

        #4
        Re: a short-cut command for globals().clear () ??

        CapnBearbossa@g ooglemail.com wrote:
        forgive me , but the RTFM and Google search approaches are not
        yielding an answer on this question. I need to know if there's a top
        level python interpreter command that clears all user variables (not
        built-ins) from the global namespace. In other words a statement, or
        some_command_or _function(), that does this:
        >
        >>>x=3
        >>>y=4
        >>>z=[]
        >>>dir()
        ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
        >
        >>>some_command _or_function()
        >
        >>>dir()
        ['__builtins__', '__doc__', '__name__']
        First, a WARNING to other readers deceived by the subject line.
        Globals().clear () clears everything and leaves nothing, so Capn... is
        looking for something that works that is a shortcut for deleting
        bindings one-by-one.

        To your question. The short answer is no.

        In batch mode, only create what you need and delete (unbind) large
        objects that are not automatically deleted (unbound) when you are done
        with them. Remember that only reference-counted implementations will
        guarantee immediate destruction and space-freeing when the last
        reference goes away. Check the gc module (and some posts in the
        archives) for more specialized control.

        In interactive mode, restart the interpreter if you really need a clean
        slate and have too many bindings that you must delete to do something
        quick like 'del x,y,z' as in your example above. In IDLE, cntl-F6
        restarts the shell with a clean slate. I presume IPython has something
        similar.

        tjr

        Comment

        • Aaron \Castironpi\ Brady

          #5
          Re: a short-cut command for globals().clear () ??

          On Sep 22, 5:44 pm, Terry Reedy <tjre...@udel.e duwrote:
          CapnBearbo...@g ooglemail.com wrote:
          forgive me , but the RTFM and Google search approaches are not
          yielding an answer on this question.  I need to know if there's a top
          level python interpreter command that clears all user variables (not
          built-ins) from the global namespace.  In other words a statement, or
          some_command_or _function(), that does this:
          >
          >>x=3
          >>y=4
          >>z=[]
          >>dir()
          ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
          >
          >>some_command_ or_function()
          >
          >>dir()
          ['__builtins__', '__doc__', '__name__']
          >
          First, a WARNING to other readers deceived by the subject line.
          Globals().clear () clears everything and leaves nothing, so Capn... is
          looking for something that works that is a shortcut for deleting
          bindings one-by-one.
          >
          To your question.  The short answer is no.
          >
          In batch mode, only create what you need and delete (unbind) large
          objects that are not automatically deleted (unbound) when you are done
          with them.  Remember that only reference-counted implementations will
          guarantee immediate destruction and space-freeing when the last
          reference goes away. Check the gc module (and some posts in the
          archives) for more specialized control.
          >
          In interactive mode, restart the interpreter if you really need a clean
          slate and have too many bindings that you must delete to do something
          quick like 'del x,y,z' as in your example above.  In IDLE, cntl-F6
          restarts the shell with a clean slate.  I presume IPython has something
          similar.
          >
          tjr
          I guess you have a few hackish options.

          1) Define a wrapper that calls its argument immediately, and use it as
          you would use anonymous blocks.

          @call_imm
          def block( ):
          code
          code
          code

          @call_imm
          def block( ):
          code
          code
          code

          You have no globals when you are done.

          2) If you need them to be global, use some introspection, and delete
          the variables upon exiting the scope.

          3) Declare all your variables in a namespace, and just delete the
          namespace.

          a= type('blank',() ,{})()
          a.varA= 0
          a.varB= 'abc'
          del a

          Comment

          • MRAB

            #6
            Re: a short-cut command for globals().clear () ??

            On Sep 22, 11:07 pm, CapnBearbo...@g ooglemail.com wrote:
            On Sep 22, 5:52 pm, Matimus <mccre...@gmail .comwrote:
            >
            >
            >
            On Sep 22, 2:31 pm, CapnBearbo...@g ooglemail.com wrote:
            >
            hi all,
            >
            forgive me , but the RTFM and Google search approaches are not
            yielding an answer on this question.  I need to know if there's a top
            level python interpreter command that clears all user variables (not
            built-ins) from the global namespace.  In other words a statement, or
            some_command_or _function(), that does this:
            >
            >x=3
            >y=4
            >z=[]
            >dir()
            >
            ['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
            >
            >some_command_o r_function()
            >dir()
            >
            ['__builtins__', '__doc__', '__name__']
            >
            thanks,
               1 desperate snake oil programmer ....
            >
            I don't think you will find anything. The interpreter is essentially
            the same whether you are in interactive mode or not. That is, there is
            very little use for a method that clears globals in general, so why
            would we add it just so that it could be used by the interpreter.
            There is almost* nothing available to the interactive interpreter
            which isn't part of the core language.
            >
            * The only difference I can think of is the "_" variable, which is
            added to __builtins__ and contains the last value returned in
            interactive mode. If you have ever tried to run code that uses the
            locale module from the interpreter you will see why having any
            differences between the interactive and non-interactive interpreter
            can be a pain.
            >
            Matt
            >
            ok. thanks! guess i'll be off to define my own function ...
            How about something like this:

            def clear_workspace ():
            keep_set = set(['__builtins__', '__doc__', '__name__',
            'clear_workspac e'])
            for x in globals().keys( ):
            if x not in keep_set:
            del globals()[x]

            Comment

            • Terry Reedy

              #7
              Re: a short-cut command for globals().clear () ??

              MRAB wrote:
              >
              How about something like this:
              >
              def clear_workspace ():
              keep_set = set(['__builtins__', '__doc__', '__name__',
              'clear_workspac e'])
              For 2.6/3.0, add __package__ to the list to be kept.

              Comment

              • =?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

                #8
                Re: a short-cut command for globals().clear () ??

                Terry Reedy wrote:
                MRAB wrote:
                >>
                >How about something like this:
                >>
                >def clear_workspace ():
                > keep_set = set(['__builtins__', '__doc__', '__name__',
                >'clear_workspa ce'])
                >
                For 2.6/3.0, add __package__ to the list to be kept.
                >
                > for x in globals().keys( ):
                > if x not in keep_set:
                > del globals()[x]

                Or... you might have a script clearWorkspace. py :

                ---------------------------------------------
                initGlobals = globals().keys( )

                def clearWorkspace( ) :
                for gVar in globals().keys( ) :
                if gVar not in initGlobals :
                del globals()[gVar]
                ---------------------------------------------

                Which you run before doing anything else. Then you don't mind if
                additions were made to the list to keep, or if you are using something
                like pyCrust which has its own initial globals, or if you want to keep
                some global vars of your own (in which case you run clearWorkspace AFTER
                you instantiate your global vars).


                Comment

                Working...