Gathering variable names from within a function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xavier

    Gathering variable names from within a function.

    Greetings,

    I was wondering if there was a way to get a list of all the variables which
    were executed/created/modified within a function?

    Here is a simple example that comes to my mind:

    def getVars(func):
    ...
    ...
    ...

    def modVar(arg, arg2):

    Var1 = arg
    Var2 = arg2
    Var3 = arg+arg2
    return Var3

    def main():

    print getVars(modVar( 'test', 'test2'))

    # end

    naturally, "Var1", "Var2" and "Var3" should be printed to the user. Any
    idea?

    -- Xavier

    oderint dum metuant



  • Peter Hansen

    #2
    Re: Gathering variable names from within a function.

    Xavier wrote:[color=blue]
    >
    > I was wondering if there was a way to get a list of all the variables which
    > were executed/created/modified within a function?[/color]

    Have you looked at the "inspect" standard module?

    Comment

    • Michele Simionato

      #3
      Re: Gathering variable names from within a function.

      "Xavier" <sabu@pure-elite.org> wrote in message news:<mailman.1 057315927.28411 .python-list@python.org >...[color=blue]
      > Greetings,
      >
      > I was wondering if there was a way to get a list of all the variables which
      > were executed/created/modified within a function?
      >
      > Here is a simple example that comes to my mind:
      >
      > def getVars(func):
      > ...
      > ...
      > ...
      >
      > def modVar(arg, arg2):
      >
      > Var1 = arg
      > Var2 = arg2
      > Var3 = arg+arg2
      > return Var3
      >
      > def main():
      >
      > print getVars(modVar( 'test', 'test2'))
      >
      > # end
      >
      > naturally, "Var1", "Var2" and "Var3" should be printed to the user. Any
      > idea?
      >[/color]

      What about

      def modVar(arg, arg2):
      Var1 = arg
      Var2 = arg2
      Var3 = arg+arg2
      print vars()
      return Var3

      modVar('test', 'test2')

      =>

      {'arg2': 'test2',
      'arg': 'test',
      'Var1': 'test',
      'Var3': 'testtest2',
      'Var2': 'test2'}
      [color=blue]
      > -- Xavier
      >
      > oderint dum metuant[/color]

      Michele

      Comment

      • Sean Ross

        #4
        Re: Gathering variable names from within a function.

        This will seem excessive, but it`s a little hack I`ve been playing with.
        WARNING: It`s incomplete, and, in general, it should probably *not* be
        used. Still, here it is, if you`re interested:

        import inspect

        class ThisResolutionE rror(Exception) :
        pass

        def this():
        "returns the function object that calls it"
        # get calling function's name from outer frame
        fname = inspect.current frame(1).f_code .co_name
        frameno = 2
        func = None
        while func is None:
        # search the outer frames for a reference to this function
        try:
        func = inspect.current frame(frameno). f_locals.get(fn ame, None)
        frameno += 1
        except ValueError:
        # reached end of call stack
        raise ThisResolutionE rror, "could not resolve %s"%fname
        return func # return this function object

        'this()' can be used to get hold of the function that you're currently in;
        you may then manipulate or inspect that function as you please.

        for example:

        def modvars(arg1, arg2):
        "records it's own modified variables"
        var1 = arg1
        var2 = arg2
        var3 = arg1+arg2
        this().__dict__ = vars()
        print this().__doc__
        return var3

        modvars(1,2)
        print modvars.__dict_ _

        # OUTPUT:
        # records it's own modified variables
        # {'arg1': 1, 'arg2': 2, 'var1': 1, 'var3': 3, 'var2': 2}


        'this()' appears to work for functions, including nested functions, but it
        does not work for bound/unbound methods, or functions stored in a dictionary
        (yet). For instance, you can do the following:

        def f():
        def g():
        print "g says 'Hello'"
        this().g = g
        print f.g
        print f.g()
        # <function g at 0x00958C60>
        # g says 'Hello'

        But you can't do this:

        def f():
        def g():
        "g says 'Hello'"
        print this().__doc__
        this().g = g
        print f.g()
        # ThisResolutionE rror: could not resolve g

        Anyway, it's still pretty neat to be able to grab hold of a function, from
        inside itself, and work with it.
        Sean


        Comment

        Working...