By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,669 Members | 1,377 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,669 IT Pros & Developers. It's quick & easy.

Leaving a .py file before all code is executed

P: 16
I am looking for something that is a crossbreed between exit() and return. Let's say I am importing the file below:

Expand|Select|Wrap|Line Numbers
  1. if variable:
  2.     print 'returning to main program'
  3.     # stop executing more code in this file
  4.     # and return to the main program
  5. else:
  6.     pass
  7. # more code goes here
  8. print 'the rest of the code was executed'
Apr 21 '12 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 626
return is the correct choice if I understand your question. If not, post something more specific.
Expand|Select|Wrap|Line Numbers
  1. def some_func(var):
  2.      if 3==var:
  3.          return
  4.  
  5.      print " executing optional code "
  6.  
  7. for choice in [1, 3, 5]:
  8.     print "\n", choice,
  9.     some_func(choice)
  10.     print 
Apr 21 '12 #2

P: 16
That works if declare all the variables to be global. The file in question is a configuration file for a much larger framework that I do not want to edit, as that would probably create troubles when updating it.

It is pretty ugly having to declare each variable as global (otherwise, the framework crashes), and also initiating the function. I mean, this is what the code ends up like

Expand|Select|Wrap|Line Numbers
  1. var = 1
  2.  
  3. def func():
  4.     global x, y, z, w
  5.     x = 1
  6.     if var:
  7.         w = 42
  8.         return
  9.     y = 2
  10.     z = 3
  11. func()
instead of just

Expand|Select|Wrap|Line Numbers
  1. var = 1
  2.  
  3. x = 1
  4. if var:
  5.     w = 42
  6.     magic_pixie_dust() #leaves file
  7. y = 2
  8. z = 3
(the real function contains over 400 lines)

Alternatively, I could just put the rest of the code under else. But both solutions are not only lacking elegance, but they are also both pretty drastic for something that was meant to be a minor, convenient tweak.

Another option is that I can "undo" the work of the rest of the code at the end of the script, but that is not particularly elegant, either.
Apr 21 '12 #3

Expert 100+
P: 626
(the real function contains over 400 lines)
That is the real problem. It is never fun to try and modify something like that. Usually a dictionary is used to store the variables, which should also be easier than globals.
Expand|Select|Wrap|Line Numbers
  1. def func(vars_dict):
  2.      x = 1
  3.      if var in vars_dict:
  4.          vars_dict["w"] = 42
  5.          return vars_dict
  6.      y = vars_dict["default"][0]
  7.      z = vars_dict["default"][1]
  8.  
  9. vars_dict = func(vars_dict) 
Note that you can store functions in a dictionary as well so you can use something like
Expand|Select|Wrap|Line Numbers
  1. def func1():
  2.     print "func1 called",
  3.  
  4. test_dict={1:func1}
  5.  
  6. for ctr in range(3):
  7.     print "\n", ctr,
  8.     if ctr in test_dict:
  9.         test_dict[ctr]() 
Apr 22 '12 #4

Post your reply

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