473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
3 2698
On 1 Aug 2003 14:05:19 -0700, mn******@airmai l.net (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 = someSourceOfCon n(...)?
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
Jul 18 '05 #2
> >I have several instances of telnet connections that I label
How do you "label" them? By assigning, like conn2 = someSourceOfCon n(...)?
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.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
Jul 18 '05 #3
> <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>


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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1123
by: Steven Bethard | last post by:
I'd like to be able to have an instance variable that can sometimes be accessed as a property, and sometimes as a regular value, e.g. something like: py> class C(object): .... def usevalue(self, x): .... self.x = x .... def usefunc(self, func, *args, **kwds): .... self._func, self._args, self._kwds = func, args,...
1
2412
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using either the list.extend method or even by having the subclass create a whole new list in the variable. The following example illustrates the...
4
3506
by: Sagaert Johan | last post by:
Hi Consider the following : Myclass myvar=new Myclass (); is there a way to get the name of the instance of the variable inside the class ? so i know in my class that the variables name is myvar ?
4
1001
by: wietse | last post by:
Hello, I have written the following script to illustrate a problem in my code: class BaseClass(object): def __init__(self): self.collection = class MyClass(BaseClass): def __init__(self, name, collection=):
6
3312
by: thomson | last post by:
Hi All, In a Singleton pattern , if we create an instance variable, and return it, whether this one also be a static variable , Can anyone give me insights on the Memory allocation Thanks in Advance thomson
3
1420
by: Janos | last post by:
Hi, I am a newbie, learning C#. I try to map the concepts I have learned earlier in other OO languages to the concepts used here in C# . In Smalltalk you have class variable, which is here the static one, existing only once, and the same for all instances of the class and its sublasses. But there you have also class instance variable,...
1
2395
by: David C | last post by:
I have a DataList that displays photos and is bound to a list of files. Is there a way to combine the value of a variable (strpath) with the # Bind(...) in the ImageURL field. Below is what I have now. <asp:ImageButton ID="ImgBtn1" runat="server" CausesValidation="false" CommandName="Select" AlternateText='<%# Bind("Name") %>'
1
1262
by: hhhhhhhh | last post by:
What is a class variable or instance variable? I am still confused. Suppose I have the code like this: public class SavingAccount { private double annualInterestRate = 0; private double savingsBalance; }
4
1933
by: Ed | last post by:
Hi, guys, I am wondering if I could change the static variable value in one static function? If I have a class: class Apple { public: static Apple* Instance()
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.