How do I combine instance+string for variable

M

Marc

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
 
B

Bengt Richter

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

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
 
M

Marc

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
 
T

Terry Reedy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top