Printing variable names

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

    Printing variable names

    mylist = [a, b, c]

    I want to print out the names of the variables in mylist (not the
    values of a, b, and c). How do I go about doing this. Thanks.

    Mike
  • Mark McEahern

    #2
    Re: Printing variable names

    Mike wrote:
    [color=blue]
    >mylist = [a, b, c]
    >
    >I want to print out the names of the variables in mylist (not the
    >values of a, b, and c). How do I go about doing this. Thanks.
    >
    >Mike
    >
    >[/color]
    There's no simple answer to this. Consider the fact that you can have
    more than one name bound to any given mutable instance. What if:

    a = range(10)
    b = a

    mylist = [a]

    what name do you want printed for the first item in mylist--'a' or 'b'?

    One idea is to use a dictionary instead. Then:

    for key, value in mydict.iteritem s():
    print '%(key)s = %(value)s' % locals()

    I'm curious what problem you're trying to solve.

    Cheers,

    // m

    Comment

    • Nuff Said

      #3
      Re: Printing variable names

      On Sun, 18 Jan 2004 11:22:08 -0800, Mike wrote:
      [color=blue]
      > mylist = [a, b, c]
      >
      > I want to print out the names of the variables in mylist (not the
      > values of a, b, and c). How do I go about doing this. Thanks.[/color]

      The following example shows that this does not really make sense:

      a = 1; b = 2; c = 3;
      mylist = [a, b, c]
      a = 4; b = 5; c = 6;
      print mylist
      print a, b, c

      Result:
      [1, 2, 3]
      4 5 6

      and *not*:
      [4, 5, 6]
      4 5 6

      You might want to google for 'Python object reference' etc.
      Moreover, having a look (e.g. in the tutorial) at how Python
      passes arguments to functions (mutable and immutable objects)
      might help to get a better understanding of what is going on
      behind the scenes.

      HTH / Nuff

      Comment

      • Dan Bishop

        #4
        Re: Printing variable names

        Mark McEahern <mark@mceahern. com> wrote in message news:<mailman.4 79.1074455535.1 2720.python-list@python.org >...[color=blue]
        > Mike wrote:
        >[color=green]
        > >mylist = [a, b, c]
        > >
        > >I want to print out the names of the variables in mylist (not the
        > >values of a, b, and c). How do I go about doing this. Thanks.[/color]
        >
        > There's no simple answer to this. Consider the fact that you can have
        > more than one name bound to any given mutable instance.[/color]

        Why did you specify "mutable"? The same applies to immutable
        instances.
        [color=blue]
        > I'm curious what problem you're trying to solve.[/color]

        So am I. The question shows up here all the time, but I've never seen
        a reason for it.

        Comment

        • Scott David Daniels

          #5
          Re: Printing variable names

          Mark McEahern wrote:[color=blue]
          > Mike wrote:[color=green]
          >> mylist = [a, b, c]
          >>
          >> I want to print out the names of the variables in mylist (not the
          >> values of a, b, and c). How do I go about doing this. Thanks.
          >> Mike[/color]
          > ... One idea is to use a dictionary instead. Then:
          > for key, value in mydict.iteritem s():
          > print '%(key)s = %(value)s' % locals()
          > I'm curious what problem you're trying to solve.[/color]

          Mark's questions are very much on target. If the purpose is debugging,
          you might be satisfied with something like:

          def vnames(value, *dicts):
          """From a value and some dictionaries and give names for the
          value"""
          result = []
          for d in dicts:
          result.extend([key for key, val in d.iteritems()
          if val is value])
          result.append(r epr(value))
          return result

          Which you might use like:

          a,b,c = 1,2,3
          d,e,f = 5,4,3

          for v in range(10):
          print v, vnames(v, locals(), globals())

          Note: You probably need only locals or globals if you are at the top
          level of an interpreter such as Idle or the python shell.
          You might prefer '==' to 'is', but remember that 0.0 == 0 == 0L.

          -Scott David Daniels
          Scott.Daniels@A cm.Org

          Comment

          • Mike

            #6
            Re: Printing variable names

            Thanks for the info. That does clear up a few things for me.

            This is what I'm trying to accomplish:

            Basically I have a list of pointers to functions (or whaterver it's called in
            Python). Something like this:

            commands = [func1, func2, ...funcN]

            This is in a script that I use to test an embedded system through the comport.
            I call the script with the command number (func1 etc...), which calls the
            corresponding function, which sends a command to the embedded system.

            I'd like to be able to call the script with --help and have it spit out
            the list of commands (the names func1, func2 etc...).


            Mike




            Mark McEahern <mark@mceahern. com> wrote in message news:<mailman.4 79.1074455535.1 2720.python-list@python.org >...[color=blue]
            > Mike wrote:
            >[color=green]
            > >mylist = [a, b, c]
            > >
            > >I want to print out the names of the variables in mylist (not the
            > >values of a, b, and c). How do I go about doing this. Thanks.
            > >
            > >Mike
            > >
            > >[/color]
            > There's no simple answer to this. Consider the fact that you can have
            > more than one name bound to any given mutable instance. What if:
            >
            > a = range(10)
            > b = a
            >
            > mylist = [a]
            >
            > what name do you want printed for the first item in mylist--'a' or 'b'?
            >
            > One idea is to use a dictionary instead. Then:
            >
            > for key, value in mydict.iteritem s():
            > print '%(key)s = %(value)s' % locals()
            >
            > I'm curious what problem you're trying to solve.
            >
            > Cheers,
            >
            > // m[/color]

            Comment

            • Peter Otten

              #7
              Re: Printing variable names

              Mike wrote:
              [color=blue]
              > This is what I'm trying to accomplish:
              >
              > Basically I have a list of pointers to functions (or whaterver it's called
              > in
              > Python). Something like this:
              >
              > commands = [func1, func2, ...funcN]
              >
              > This is in a script that I use to test an embedded system through the
              > comport. I call the script with the command number (func1 etc...), which
              > calls the corresponding function, which sends a command to the embedded
              > system.
              >
              > I'd like to be able to call the script with --help and have it spit out
              > the list of commands (the names func1, func2 etc...).[/color]

              You're lucky, functions "know" their name:
              [color=blue][color=green][color=darkred]
              >>> def func1(): pass[/color][/color][/color]
              ....[color=blue][color=green][color=darkred]
              >>> def func2(): pass[/color][/color][/color]
              ....[color=blue][color=green][color=darkred]
              >>> for f in [func1, func2]:[/color][/color][/color]
              .... print f.__name__
              ....
              func1
              func2

              Peter

              Comment

              • Mark McEahern

                #8
                Re: Printing variable names

                [Me][color=blue]
                > There's no simple answer to this. Consider the fact that you can have
                > more than one name bound to any given mutable instance.[/color]

                [Dan Bishop][color=blue]
                > Why did you specify "mutable"? The same applies to immutable
                > instances.[/color]

                I didn't think through the immutable issue, that's all. So, in short,
                no good reason. <wink>

                Cheers,

                // m


                Comment

                • Mike

                  #9
                  Re: Printing variable names

                  That makes life easy. Problem solved! Thanks all for the help.

                  Mike


                  [color=blue]
                  >
                  > You're lucky, functions "know" their name:
                  >[color=green][color=darkred]
                  > >>> def func1(): pass[/color][/color]
                  > ...[color=green][color=darkred]
                  > >>> def func2(): pass[/color][/color]
                  > ...[color=green][color=darkred]
                  > >>> for f in [func1, func2]:[/color][/color]
                  > ... print f.__name__
                  > ...
                  > func1
                  > func2
                  >
                  > Peter[/color]

                  Comment

                  Working...