473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing a string

6 New Member
Hi,
If I execute the following code I will get the the output as 2. What I need is to execute the string str. Is there a way that I can execute the string str as it is, so I will get the output as 5.

Expand|Select|Wrap|Line Numbers
  1. def add(a,b):
  2.     return a+b
  3.  
  4. var = 2
  5. str = "var = add(2,3)"
  6. [color=#4080FF]# execute the code snippet inside the str string variable[/color]
  7. print var
  8.  
Apr 23 '08 #1
4 1328
Smygis
126 New Member
Expand|Select|Wrap|Line Numbers
  1. >>> expr = "var = (2+3)+pow(2,2)"
  2. >>> exec(expr)
  3. >>> var
  4. 9
  5.  
Apr 23 '08 #2
jlm699
314 Contributor
As smygis silently pointed out, you should not use reserved words for variable declarations...
In your example you used str = 'var = add(2,3)'... but if you wanted to type-cast a numeric variable you would not be able to do str(some_num) ...
It would behoove you to try and avoid this in your python scripts from now on.
Apr 23 '08 #3
raubana
56 New Member
It looks like you made the command part of the text:

"a+b = var(a,b)"

instead of

"a+b =", var(a,b)

One thing I'd do is convert the result into a string:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def add(a,b):
  3. * * return a+b
  4.  
  5. var = 2
  6. str = "var =" + str( add(2,3) )
  7. [color=#4080FF]# execute the code snippet inside the str string variable[/color]    
  8.  
  9. print var
  10.  
  11.  
That's all there is to it.
Apr 30 '08 #4
jlm699
314 Contributor
It looks like you made the command part of the text:

"a+b = var(a,b)"
That's what he wanted to do.

Using exec() allows him to take a string and execute it as if the string was a command. This function is extremely helpful when working with a very dynamic program, as it allows you to do things similar to this:
Expand|Select|Wrap|Line Numbers
  1. >>> elem1 = 'ab'
  2. >>> elem2 = 'wtf'
  3. >>> elem3 = 'foo'
  4. >>> elem4 = 'bar'
  5. >>> elem5 = 'lol'
  6. >>> elem6 = 'abc'
  7. >>> for idx in range(1, 7):
  8. ...     exec('print elem%d' % idx)
  9. ...     
  10. ab
  11. wtf
  12. foo
  13. bar
  14. lol
  15. abc
  16. >>> 
Additionally the assignment could be taken care of in the same manner:
Expand|Select|Wrap|Line Numbers
  1. txts = ['ab', 'wtf', 'foo', 'bar', 'lawl', 'abc']
  2. for idx in range(1, 7):
  3.     exec( "elem%d = '%s'" % ( idx, txts[idx - 1] ) )
  4.  
  5. for idx in range(1, 7):
  6.     exec( 'print elem%d' % idx )
  7.  
  8. ab
  9. wtf
  10. foo
  11. bar
  12. lawl
  13. abc
  14.  
Apr 30 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2248
by: rvdw | last post by:
Hi All, I've a serious problem with executing stored procedures (SQL2000) from an Access db (version 97). After executing a stored procedure , msaccess hangs. The whole call to the procedure is...
14
10353
by: Jay O'Connor | last post by:
Is there a good way to import python files without executing their content? I'm trying some relfection based stuff and I want to be able to import a module dynamically to check it's contents...
0
1931
by: Jay Bienvenu | last post by:
I'm having a very strange problem that currently defies explanation. I use the code below to send a status report to a client. The intent is to send the user the report via fax using WinFax and/or...
1
3133
by: rvdw | last post by:
Hi All, I've a serious problem with executing stored procedures (SQL2000) from an Access db (version 97). After executing a stored procedure , msaccess hangs. The whole call to the procedure is...
10
3375
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
0
1481
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after...
7
2795
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
3
5386
by: shanmukhi | last post by:
hi all, i got a problem with struts. iam unable to find it. i have written an action class, form bean class and a jsp page on submitting form, request is going to action class but it is not...
7
10018
by: chage | last post by:
Hi, I have been searching around to try adding reference assembly to another assembly during runtime, programatically. Is this possible in .Net? The reason for this is because i am having...
13
5587
by: surlogics | last post by:
The function of the program is to output the longest word in a sentence. #include<stdio.H> #include<string.H> main() {int alphabetic(char); int longest(char ); int i; char line;
0
7226
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,...
0
7125
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...
0
5631
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,...
1
5055
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...
0
3199
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...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
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 ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.