How do I combine instance+string for variable

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

    How do I combine instance+string for variable

    Hi all,

    I can't remember how to do this.

    I have several instances of telnet connections that I label
    conn2,conn3, etc. Later when I want to scroll through all of these I
    wanted to do something like this:

    for int in range(2, 9):
    use... conn+str(int) {I'm passing it into another
    function}

    I can't get it to work. I've tried using setattr and eval, but nothing
    seems to work. Can I get a little help.

    Thanks ahead of time,
    Marc
  • Bengt Richter

    #2
    Re: How do I combine instance+string for variable

    On 1 Aug 2003 14:05:19 -0700, mnations@airmai l.net (Marc) wrote:
    [color=blue]
    >Hi all,
    >
    >I can't remember how to do this.
    >
    >I have several instances of telnet connections that I label[/color]
    How do you "label" them? By assigning, like conn2 = someSourceOfCon n(...)?
    [color=blue]
    >conn2,conn3, etc. Later when I want to scroll through all of these I
    >wanted to do something like this:
    >
    > for int in range(2, 9):[/color]
    ^^^-- BAD name!! you are shadowing the builtin int.[color=blue]
    > use... conn+str(int) {I'm passing it into another
    >function}[/color]
    Do you want to pass a name in the form of a string, like 'conn2' or do you
    want the conn2 that you assigned before?

    <untested>
    If you know the names, you could just write

    for aConn in (conn2, conn3, ..., conn8): # filling in the rest in place of '...'
    usingFunc(aConn )

    If you had stored the conn's in a list instead of individual names, you could write

    for aConn in theList:
    usingFunc(aConn )

    If you want to pick them up by generated string name from the local namespace, you could write
    something like you started with, e.g.,

    for i in range(2, 9):
    name = 'conn%s' % i
    aConn = vars()[name] # or use globals() in place of vars() if not in local namespace
    usingFunc(aConn )

    or in one line
    for i in range(2, 9): usingFunc(vars( )['conn%s'%i]) # ditto re globals() vs vars()

    </untested>

    [color=blue]
    >
    >I can't get it to work. I've tried using setattr and eval, but nothing
    >seems to work. Can I get a little help.[/color]

    Post the code that creates the conn2 etc bindings if the above does not work for you.

    Regards,
    Bengt Richter

    Comment

    • Marc

      #3
      Re: How do I combine instance+string for variable

      > >I have several instances of telnet connections that I label[color=blue]
      > How do you "label" them? By assigning, like conn2 = someSourceOfCon n(...)?[/color]

      Exactly. They are each separate instances of a telnet connection:

      conn2 = telnetlib...
      ....
      conn8 = telnetlib...
      [color=blue]
      > Do you want to pass a name in the form of a string, like 'conn2' or do you
      > want the conn2 that you assigned before?[/color]

      Actually right now I'm using the list method:

      <snippett>
      conns = [conn2, conn3, conn4, conn5, conn6, conn7, conn8]
      ....
      for int in range(2,9):
      qput(key.comman d, conns[int-2], act_user("user-ID" + str(int), "CTAG",
      "t*sting" + str(int) ) )
      <end of snippett>

      I didn't include the code originally because it goes off in a lot of
      directions. I'm putting it into a queue with function qput that later gets
      executed with a function in file key.

      The list method works for me with a small number of connections, but if I
      ever need a lot of connections it will be a little klunky. Therefore I was
      trying to find a fix similar to using setattr if I was using classes. So
      basically I want to be able to use the conn2 that I assigned before, but
      referencing it using a string.

      I think the method you mentioned:

      for i in range(2, 9):
      name = 'conn%s' % i
      aConn = vars()[name] # or use globals() in place of vars() if not
      in local namespace
      usingFunc(aConn )

      is what I'm looking for as it appears to concatenate "conn" with a number to
      create the instance name. I hadn't come across the built-in function "vars"
      yet, but I was trying to accomplish the same thing using "eval".

      Thanks for the tip,
      Marc


      Comment

      • Terry Reedy

        #4
        Re: How do I combine instance+string for variable

        > <snippett>[color=blue]
        > conns = [conn2, conn3, conn4, conn5, conn6, conn7, conn8]
        > ...
        > for int in range(2,9):
        > qput(key.comman d, conns[int-2], act_user("user-ID" + str(int),[/color]
        "CTAG",[color=blue]
        > "t*sting" + str(int) ) )
        > <end of snippett>[/color]

        And what happens if you follow 'snippett' with anything like
        type(var)==int # or
        var=int(num/3) # or
        num = int(somestring)
        or preceed it with something like
        for str in stringlist: .....
        ?

        Please don't use type names (or other builtins) as variables in posted
        code.
        Experts may survive it, but it's a bad example for beginners. ;-)

        Terry J. Reedy


        Comment

        Working...