473,848 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

storing variable names in a list before they are used?

If I want to have a list like this:

[(first_name, 'First Name:'), (last_name, 'Last Name:').....]

where the first part of each tuple is a variable name and the second
part is a label for the user to see, such as a form like this:

First Name: ________
Last Name: ________

(the variables would store whatever information is entered in the text
boxes to the right of each label. I'm doing it this way so I can write a
loop to construct my GUI components).

how would I go about putting these variable names in a list? I know I
can't leave them as above, but if I put them in as a string, then how do
I later "transform" them into an actual variable for assign, such as:

first_name = widget.get_text ()

Is there some kind of idiom that does this sort of work?

Thanks.
Sep 29 '06
10 7132
"Dennis Lee Bieber" <wl*****@ix.net com.comwrote:
On Sat, 30 Sep 2006 14:33:35 -0400, John Salerno
<jo******@NOSPA Mgmail.comdecla imed the following in comp.lang.pytho n:

Ok, I'm sure you all get the idea by now, but here's a simpler way to
look at it:

Instead of

first_name = wx.TextCtrl(sel f)
last_name = wx.TextCtrl(sel f)
job_title = wx.TextCtrl(sel f)
etc.

and subsequently:

sizer.Add(first _name)
sizer.Add(last_ name)
sizer.Add(job_t itle)
etc.

I want to do something like this:

for name in names:
name = wx.TextCtrl(sel f)
sizer.Add(name)

It's just that I don't know how to handle the "name" variable in the
"names" list.

Short blunt answer... You Don't
Longer, less blunt answer:

You could, if you wanted to, write:

errfile = "name_of_error_ file_for_compil e_statement"
expr = name+" = wx.TextCtrl(sel f)"
eval(compile(ex pr, errfile,'single '))

and the name that reads the same as the text string should be created.

To access it, you can then use:

sizer.Add(eval( name))

Example:

IDLE 1.1.3 ==== No Subprocess ====
>>banana
Traceback (most recent call last):
File "<pyshell#0 >", line 1, in ?
banana
NameError: name 'banana' is not defined
>>name = "banana"
errfile='errf ile'
expr = name+'="A yellow sickle shaped fruit"'
eval(compile( expr,errfile,'s ingle'))
banana
'A yellow sickle shaped fruit'
>>name
'banana'
>>eval(name)
'A yellow sickle shaped fruit'
>>>
*ducks behind asbestos shelter to hide from the predicted flames *

But Seriously - I think eval is good for just this kind of purpose - does what
was wanted, namely creating the variable with the same name as the content of a
string... - its also useful to access a variable if you have been given its name
in a string, instead of being passed the thing itself - then you don't need the
compile bit...

Caveat Emptor

- Hendrik


Oct 1 '06 #11

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

Similar topics

11
2099
by: Harlin Seritt | last post by:
There are certain options for Tkinter widgets that have default values that I don't much care for (borderwidth, font come to mind) and continuously change when I'm building interfaces. With a bit of tweaking I have been able to give the widgets a look that rivals the best of them. However, I get tired of doing this when I'm writing code and would like a way that I could universally change them on my system. I have tried to find where in...
1
3464
by: JustSomeGuy | last post by:
I just spent a few hours trying to find a bug in a friends code. I finally found it, but I'm shocked to see what is wrong. A private variable was defined in the class specification. The constructor also had a variable of the same name defined. We wondered why later on in the program things crashed. Well its obvious now, but there were absolutly no complier warnings about this variable. C++ doesn't seem to mind this and accepts it as...
22
3969
by: opt_inf_env | last post by:
Hello, It is strange to me that html and css use different names for variables which define the same properties. For example if I whant to define text color from html document I write: <body text="#dddddd"> And to define text color from css file I need to write:
15
2273
by: James | last post by:
Hi, I am finding it increasingly difficult to name my variables. I am not able to think in the right way. Expert C programmers please Help. Regards,
5
2581
by: Brakeshoe | last post by:
I would like to find out how to turn on the Auto Complete variable names feature while editing source code in Visual Studio. It seems to appear on intermittantly, and when it does the variable list is incomplete or contains variables that do not even exist in the project being worked on. It seems like there should be a setting to turn this on and keep it up to date but I cannot find anything in MSDN. It is also a complete mystery why it...
16
6751
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n = 3; I know its setting aside storage for the variable itself; does it also use up more storage if the variable name is longer? I realize it would probably take quite a lot of long variable names to make any impact if so, but I was just curious...
5
2644
by: panthera | last post by:
Hi, I have a problem regarding generating variable names in my program.Suppose I have 10 variables b0,b1,b2...b9.They are NOT in an array.And I want to randomly select one of them and get its value.So I pick a random number between 0 and 9 and concat it with 'b' to get the name of this variable.But how do I get the value of this variable? I tried to define a macro using b##num,but it results in bnum instead of b5 for example.
4
3335
by: Victor Lagerkvist | last post by:
Hello, I have the need to parse variable names from a string and save them somewhere safe for future usage. Here's my first attempt (I don't have any rules for valid names yet) - but I have a feeling that it's unnecessary complex? Any input would be greatly appreciated. #include <stdio.h> #include <stdlib.h> int get_ops(char *sen, char ***atom, char limit);
6
1892
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that. Is it possible?
0
9735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10997
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10661
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10347
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9497
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4542
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
2
4134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.