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.
--- 14 1162 ch*********@gmail.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/ >
Marius Bernklev wrote: ch*********@gmail.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.
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.
dataangel wrote: Marius Bernklev wrote: ch*********@gmail.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
<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
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("enter 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("enter 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("enter name: ")] = 34
enter name: raw_input globals()[raw_input("enter name: ")] = 34
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable
Andrew da***@dalkescientific.com
<SNIP>
globals()[raw_input("enter name: ")] = 34 enter name: raw_input globals()[raw_input("enter name: ")] = 34 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: 'int' object is not callable
Andrew da***@dalkescientific.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.
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.
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***@dalkescientific.com
Richard Blackwood wrote: <SNIP>
>>> globals()[raw_input("enter name: ")] = 34 enter name: raw_input >>> globals()[raw_input("enter name: ")] = 34 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: 'int' object is not callable >>>
Andrew da***@dalkescientific.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.
Alright, you say he doesn't need a dictionary, and the other guy says he
doesn't actually need eval. What is this mysterious non-exec, non-eval,
non-globals(), non-dictionary solution? ;)
<SNIP> Alright, you say he doesn't need a dictionary, and the other guy says he doesn't actually need eval. What is this mysterious non-exec, non-eval, non-globals(), non-dictionary solution? ;)
Nevermind. My bad. I misread your post. Go with the globals()
function, it will save you some headaches vs. eval() and exec. On the
other hand, you could also use eval w/ the globals dictionary. I don't
think that has any problems but Andrew can correct me on this one.
Richard Blackwood wrote: On the other hand, you could also use eval w/ the globals dictionary. I don't think that has any problems but Andrew can correct me on this one.
I gave an example of assigning to 'raw_input'. Most
likely not something you want people to change.
Andrew da***@dalkescientific.com
<SNIP> I gave an example of assigning to 'raw_input'. Most likely not something you want people to change.
Andrew da***@dalkescientific.com
Indeed.
Andrew Dalke <ad****@mindspring.com> wrote: 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.
Very -- and for no good purpose, because just about every beginner's
desire to "assign variables with names determined at runtime", once
analyzed in a bit more depth, turns out to be susceptible of some far
preferable approach (9 out of 10 involving some dict...).
BTW, any function incuding an 'exec' statement has the optimization you
mentioned turned off -- thus the whole function runs dog-slow...
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |