How do I combine instance+string for variable

Discussion in 'Python' started by Marc, Aug 1, 2003.

  1. Marc

    Marc Guest

    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
     
    Marc, Aug 1, 2003
    #1
    1. Advertising

  2. On 1 Aug 2003 14:05:19 -0700, (Marc) wrote:

    >Hi all,
    >
    >I can't remember how to do this.
    >
    >I have several instances of telnet connections that I label

    How do you "label" them? By assigning, like conn2 = someSourceOfConn(...)?

    >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):

    ^^^-- BAD name!! you are shadowing the builtin int.
    > use... conn+str(int) {I'm passing it into another
    >function}

    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>


    >
    >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.


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

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 1, 2003
    #2
    1. Advertising

  3. Marc

    Marc Guest

    > >I have several instances of telnet connections that I label
    > How do you "label" them? By assigning, like conn2 = someSourceOfConn(...)?


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

    conn2 = telnetlib...
    ....
    conn8 = telnetlib...

    > 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?


    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.command, 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
     
    Marc, Aug 2, 2003
    #3
  4. Marc

    Terry Reedy Guest

    > <snippett>
    > conns = [conn2, conn3, conn4, conn5, conn6, conn7, conn8]
    > ...
    > for int in range(2,9):
    > qput(key.command, conns[int-2], act_user("user-ID" + str(int),

    "CTAG",
    > "t*sting" + str(int) ) )
    > <end of snippett>


    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
     
    Terry Reedy, Aug 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerry Sutton
    Replies:
    1
    Views:
    582
    Peter Otten
    Apr 16, 2005
  2. David C

    Combine variable and #bind

    David C, Jun 21, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    439
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Jun 22, 2007
  3. David Garamond
    Replies:
    5
    Views:
    276
    Ara.T.Howard
    Jun 8, 2004
  4. Pat Kiatchaipipat

    combine array(string) to string?

    Pat Kiatchaipipat, Mar 20, 2008, in forum: Ruby
    Replies:
    10
    Views:
    196
    Robert Klemme
    Mar 22, 2008
  5. Leon Bogaert
    Replies:
    19
    Views:
    355
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page