printing variables

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

    #1

    printing variables

    hi
    say i have variables like these

    var1 = "blah"
    var2 = "blahblah"
    var3 = "blahblahbl ah"
    var4 = "...."
    var5 = "..."..

    bcos all the variable names start with "var", is there a way to
    conveniently print those variables out...
    eg print var* ??
    i don't want to do :

    print var1, var2, var3, var4 ......etc...
    thanks

  • John Machin

    #2
    Re: printing variables


    s99999999s2003@ yahoo.com wrote:
    hi
    say i have variables like these
    >
    var1 = "blah"
    var2 = "blahblah"
    var3 = "blahblahbl ah"
    var4 = "...."
    var5 = "..."..
    >
    bcos all the variable names start with "var", is there a way to
    conveniently print those variables out...
    eg print var* ??
    i don't want to do :
    >
    print var1, var2, var3, var4 ......etc...
    thanks
    | >>var1 = 1
    | >>var2 = 2
    | >>variant = 3
    | >>variegated = 4
    | >>' '.join(str(v) for k, v in locals().iterit ems() if
    k.startswith('v ar'))
    | '1 2 3 4'

    *BUT* why do you start off with those things in separate variables
    instead of in some container (list, dict, object simulating a "record"
    or "struct", ...?

    Comment

    • Fredrik Lundh

      #3
      Re: printing variables

      s99999999s2003@ yahoo.com wrote:
      say i have variables like these
      >
      var1 = "blah"
      var2 = "blahblah"
      var3 = "blahblahbl ah"
      var4 = "...."
      var5 = "..."..
      >
      bcos all the variable names start with "var", is there a way to
      conveniently print those variables out...
      do you often (or always) treat these as a collection ? why not just put
      the values in a list (or tuple) instead ?

      </F>

      Comment

      • Fredrik Lundh

        #4
        Re: printing variables

        s99999999s2003@ yahoo.com wrote:
        say i have variables like these
        >
        var1 = "blah"
        var2 = "blahblah"
        var3 = "blahblahbl ah"
        var4 = "...."
        var5 = "..."..
        >
        bcos all the variable names start with "var", is there a way to
        conveniently print those variables out...
        do you often (or always) treat these as a collection ? why not just put
        the values in a list (or tuple) instead ?

        </F>

        Comment

        • Gary Herron

          #5
          Re: printing variables

          s99999999s2003@ yahoo.com wrote:
          hi
          say i have variables like these
          >
          var1 = "blah"
          var2 = "blahblah"
          var3 = "blahblahbl ah"
          var4 = "...."
          var5 = "..."..
          >
          bcos all the variable names start with "var", is there a way to
          conveniently print those variables out...
          eg print var* ??
          i don't want to do :
          >
          print var1, var2, var3, var4 ......etc...
          thanks
          >
          >
          If you really don't want an array (list, or tuple) of these (i.e.,
          var[1], var[2], var[3], ...), then you can get at a dictionary that
          contains all the local variables -- names and values -- like this:

          >>var1 = "blah"
          >>var2 = "blahblah"
          >>var3 = "blahblahbl ah"
          >>var4 = "...."
          >>locals()
          {'var4': '....', 'var1': 'blah', 'var3': 'blahblahblah', 'var2':
          'blahblah', '__builtins__': <module '__builtin__' (built-in)>,
          '__file__': '/home/gherron/.startup.py', '__name__': '__main__',
          '__doc__': None}
          >>for v,k in locals().items( ):
          .... if v.startswith('v ar'):
          .... print k
          ....
          .....
          blah
          blahblahblah
          blahblah
          >>>

          Gary Herron

          Comment

          • Dan Bishop

            #6
            Re: printing variables

            On Oct 5, 9:47 pm, s99999999s2...@ yahoo.com wrote:
            hi
            say i have variables like these
            >
            var1 = "blah"
            var2 = "blahblah"
            var3 = "blahblahbl ah"
            var4 = "...."
            var5 = "..."..
            >
            bcos all the variable names start with "var", is there a way to
            conveniently print those variables out...
            eg print var* ??
            i don't want to do :
            >
            print var1, var2, var3, var4 ......etc...
            def print_vars(var_ dict, prefix=''):
            var_names = sorted(name for name in var_dict if
            name.startswith (prefix))
            for name in var_names:
            print var_dict[name],
            print

            def example():
            var1 = 'blah'
            var2 = 'blahblah'
            var3 = 'spam'
            var4 = 'eggs'
            ignored_var = 'foo'
            print_vars(loca ls(), 'var')

            Comment

            • s99999999s2003@yahoo.com

              #7
              Re: printing variables


              Fredrik Lundh wrote:
              s99999999s2003@ yahoo.com wrote:
              >
              say i have variables like these
              >
              var1 = "blah"
              var2 = "blahblah"
              var3 = "blahblahbl ah"
              var4 = "...."
              var5 = "..."..
              >
              bcos all the variable names start with "var", is there a way to
              conveniently print those variables out...
              >
              do you often (or always) treat these as a collection ? why not just put
              the values in a list (or tuple) instead ?
              >
              </F>
              Thanks all for the answers. Yup, i think i will use dicts/tuples/lists
              instead...

              Comment

              • MonkeeSage

                #8
                Re: printing variables

                s99999999s2003@ yahoo.com wrote:
                Thanks all for the answers. Yup, i think i will use dicts/tuples/lists
                instead...
                Even though you should be using some kind of container, you can still
                do what you origianlly asked for with relative ease...you just have to
                use the evil eval function (gasp!):

                for i in xrange(5):
                if i == 0: continue
                print eval("var%d" % i)

                Regards,
                Jordan

                Comment

                • Gerrit Holl

                  #9
                  Re: printing variables

                  On 2006-10-06 04:50:33 +0200, s99999999s2003@ yahoo.com wrote:
                  say i have variables like these
                  >
                  var1 = "blah"
                  var2 = "blahblah"
                  var3 = "blahblahbl ah"
                  var4 = "...."
                  var5 = "..."..
                  >
                  bcos all the variable names start with "var", is there a way to
                  conveniently print those variables out...
                  eg print var* ??
                  i don't want to do :
                  >
                  print var1, var2, var3, var4 ......etc...
                  Don't do this:
                  >>import fnmatch
                  >>var1, var2, var3 = "foo", "bar", "baz"
                  >>for k in fnmatch.filter( locals(), "var*"):
                  .... print locals()[k]
                  ....
                  foo
                  baz
                  bar

                  This is evil.
                  It's unpythonic.
                  This is yet another way to do it - QED.

                  Gerrit.

                  Comment

                  • hanumizzle

                    #10
                    Re: printing variables

                    On 10/6/06, Gerrit Holl <gerrit@nl.linu x.orgwrote:
                    >import fnmatch
                    >var1, var2, var3 = "foo", "bar", "baz"
                    >for k in fnmatch.filter( locals(), "var*"):
                    ... print locals()[k]
                    ...
                    foo
                    baz
                    bar
                    >
                    This is evil.
                    It's unpythonic.
                    It's so evil, Perl 4 would look upon it in scorn.

                    -- Theerasak

                    Comment

                    Working...