473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there any way in which the execution of python statements can be made out of order

For example, can we execute the following script

a = 4
b = 3
k = 4 * c
c = a + b
print k

without getting a name error(since c is defined in the 4th line but called for in the 3rd line) i.e. first the 4th line should get executed then the third.
Oct 20 '10 #1
4 1967
dwblas
626 Expert 512MB
first the 4th line should get executed then the third.
There are no "goto" statements in Python. So there is no way for the program to know what other execution path you wish it to take.
Oct 21 '10 #2
Glenton
391 Expert 256MB
The real question is why do you want it to do this? Maybe there's another way. But the short answer is "no"
Oct 25 '10 #3
bvdet
2,851 Expert Mod 2GB
It's possible if you do it like this:
Expand|Select|Wrap|Line Numbers
  1. >>> a=4
  2. >>> b=3
  3. >>> k='4*c'
  4. >>> c=a+b
  5. >>> print eval(k)
  6. 28
Oct 25 '10 #4
Glenton
391 Expert 256MB
Ah, @bvdet, I like it. You could take this further, by making all the lines strings and using the exec command. You could do it inside a try except structure, so that the order is made correct, if possible.

Like this:
Expand|Select|Wrap|Line Numbers
  1. commands=['a=4','b=3','k=4*c','c=a+b']
  2.  
  3. j=0
  4. while True:
  5.     i=len(commands)
  6.     if i==0:
  7.         print "All commands executed"
  8.         break
  9.     if j>1000:
  10.         print "Unable to resolve commands"
  11.         break
  12.     try:
  13.         exec(commands[j%i])
  14.         print commands[j%i], 'was executed successfully'
  15.         commands=commands[:(j%i)]+commands[(j%i)+1:]  #remove executed command
  16.     except:
  17.         print commands[j%i], 'not yet executed'
  18.         j+=1
which gives:
Expand|Select|Wrap|Line Numbers
  1. a=4 was executed successfully
  2. b=3 was executed successfully
  3. k=4*c not yet executed
  4. c=a+b was executed successfully
  5. k=4*c was executed successfully
  6. All commands executed
  7. >>> 
Oct 26 '10 #5

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
1
by: Robert Brewer | last post by:
> > > Can we insert conditional expressions in the > decorator list ? > > > > Not with the current patch; however, that option may be > allowed in the future. > > "In the future"...
9
by: Jaime Wyant | last post by:
I know I've seen this somewhere, but can't seem to google it. Is there a way to use an alternate statement separator, other than the default ';'? jw
0
by: Robert | last post by:
After failing on a yield/iterator-continuation problem in Python (see below) I tried the Ruby (1.8.2) language first time on that construct: The example tries to convert a block callback interface...
10
by: buffi | last post by:
Am I the only one that thinks that python statements should force whitespace before and after them? Right now this is not enforced and for an example these statements are valid print"hello"...
5
by: iheartvba | last post by:
Hi I am getting the following error when i click cmdFFR (See note1): Invalid SQL statement, expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT' or 'UPDATE'. I can't figure out why this is. Spelling...
3
by: SteveD | last post by:
Hi guys, http://luaforge.net/frs/?group_id=327 pgdb.zip is an addition to scite-debug, which adds source debugging to the popular SciTE programmer's editor. gdbpy.zip is a standalone version...
0
by: Gary Herron | last post by:
Ohad Frand wrote: There is no way you can consider 'elif', 'else', 'except', and 'from' statements. However, as someone pointed out, the kwlist from the keyword module is the closest thing we...
0
by: Ohad Frand | last post by:
Hi Thanks a lot for your reply I think the main uses for it is to study the language and to see that I didn't miss anything else or that something is changed from one version to another. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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...
0
isladogs
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.