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

Home Posts Topics Members FAQ

help me

Hey everybody. I am writing a program that requires the user to make
the name for variables inside the program. For example:
exec raw_input()+' = 34'


However, I don't want to have to use the "exec" statement. Thanks for
any help!

P.S. For those who have ansewered my post for a similar request, I
thought this would explain myself better.

---
Jul 18 '05 #1
14 1182
ch*********@gma il.com (Chris Patton) writes:
Hey everybody. I am writing a program that requires the user to make
the name for variables inside the program. For example:
exec raw_input()+' = 34'


However, I don't want to have to use the "exec" statement. Thanks for
any help!


Are you sure you don't want to use a plain dict for this? And if so, why?
--
Marius Bernklev
<URL: http://www.ping.uio.no/~mariube/ >
Jul 18 '05 #2
Marius Bernklev wrote:
ch*********@gm ail.com (Chris Patton) writes:
Hey everybody. I am writing a program that requires the user to make
the name for variables inside the program. For example:
>exec raw_input()+' = 34'
>
>

However, I don't want to have to use the "exec" statement. Thanks for
any help!


Are you sure you don't want to use a plain dict for this? And if so, why?

Why don't you want to use exec? Just curious. That's the normal way to
get a string to python code as far as I know.

Jul 18 '05 #3
There is always eval(). His example could be done with out either though.
<SNIP>

Hey everybody. I am writing a program that requires the user to make
the name for variables inside the program. For example:

>> exec raw_input()+' = 34'
>>
>
However, I don't want to have to use the "exec" statement. Thanks for
any help!

Are you sure you don't want to use a plain dict for this? And if so,
why?

Why don't you want to use exec? Just curious. That's the normal way to
get a string to python code as far as I know.


Jul 18 '05 #4
dataangel wrote:
Marius Bernklev wrote:
ch*********@gma il.com (Chris Patton) writes:
Hey everybody. I am writing a program that requires the user to make
the name for variables inside the program. For example:
>> exec raw_input()+' = 34'

However, I don't want to have to use the "exec" statement. Thanks for
any help!

Are you sure you don't want to use a plain dict for this? And if so,
why?

Why don't you want to use exec? Just curious. That's the normal way to
get a string to python code as far as I know.


No, it's not the normal way, it's the crude and insecure way.

The normal way is to use the string as a key to a dictionary,
perhaps locals() or globals(), as Marius appears to be
suggesting.

Like regular expressions, in the hands of a beginner exec
and eval() lead to code that is one or more of dangerous,
unreadable, unmaintainable, awkward, or just plain sloppy.

-Peter
Jul 18 '05 #5
<SNIP>

Like regular expressions, in the hands of a beginner exec
and eval() lead to code that is one or more of dangerous,
unreadable, unmaintainable, awkward, or just plain sloppy.

-Peter


He's got to learn somehow! LOL
Jul 18 '05 #6
dataangel:
Why don't you want to use exec? Just curious. That's the normal way to
get a string to python code as far as I know.


That's not the normal way. Where did you get that idea?
exec raw_input("ente r name: ")+' = 34' enter name: import os; os.system("pwd && echo rm -rf /delete/any/file"); c
/Users/dalke/src
rm -rf /delete/any/file
In other words, without full vetting of the input it's
a hugh security hole.

If you want to set a global variable (why??) then use
the globals() dictionary.
spam Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined globals()[raw_input("ente r name: ")] = 34 enter name: spam spam 34
More than likely you should put the data into a
dictionary of its own. Otherwise, what happens
if someone assigns to the variable 'raw_input'?
globals()[raw_input("ente r name: ")] = 34 enter name: raw_input globals()[raw_input("ente r name: ")] = 34 Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable


Andrew
da***@dalkescie ntific.com
Jul 18 '05 #7
<SNIP>

globals()[raw_input("ente r name: ")] = 34 enter name: raw_input globals()[raw_input("ente r name: ")] = 34 Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable


Andrew
da***@dalkescie ntific.com


exec and eval is dangerous stuff for the inexperienced. Dictionary is
an excellent idea Andrew. However, if what he wants to do is as simple
as the example he gave, he doesn't even need a dictionary.
Jul 18 '05 #8
Richard Blackwood wrote:
<SNIP>

Like regular expressions, in the hands of a beginner exec
and eval() lead to code that is one or more of dangerous,
unreadable, unmaintainable, awkward, or just plain sloppy.

-Peter

He's got to learn somehow! LOL


Well, I remember looking up the exec keyword and it saying executes a
string. Thus, it seemed like the normal way to execute a string, lol.

Using globals() makes sense, but using locals() doesn't because it won't
have the desired effect. locals() returns a _copy_, globals() is an
actual reference.

Jul 18 '05 #9
dataangel wrote:
Well, I remember looking up the exec keyword and it saying executes a
string. Thus, it seemed like the normal way to execute a string, lol.


But why did that seem like it should be the normal way
to do variable assignment to global?

BTW, locals() doesn't work like you want in part because
that prevents a performance optimization where local
variable lookups are done in O(1) time as an offset
into a fixed size table. I suppose it would be possible
to have locals() be able to manipulate that table behind
the scenes, but then that would be complicated and complex.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #10

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

Similar topics

21
6499
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to...
9
4388
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with the microsoft HTML workshop utility, lets call it c:\path\help.chm. My question is how do you launch it from the GUI? What logic do I put behind...
6
4312
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program)...
3
3332
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With numarray, help gives unhelpful responses:
7
5348
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available from clicking on many of the available topics (mostly methods but some properties are also unavailable). This same problem occurs with many, if not...
5
3253
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time using "F1" help within the VB IDE. Is this expectation achievable In trying to test my help file in the IDE, I have a solution with 2 projects:...
8
3208
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both including search the internet for help, but the help is worthless. Any ideas?
10
3340
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
1
6115
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve default property of object Label. Click for more:...
0
2859
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in...
0
7548
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
7472
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...
0
7986
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...
0
7832
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...
1
5391
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...
0
5114
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
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1965
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
1
1083
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.