Leaving a .py file before all code is executed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nihilium
    New Member
    • Mar 2008
    • 16

    Leaving a .py file before all code is executed

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

    Code:
    if variable:
        print 'returning to main program'
        # stop executing more code in this file
        # and return to the main program
    else:
        pass
    # more code goes here
    print 'the rest of the code was executed'
    Last edited by nihilium; Apr 21 '12, 05:52 PM. Reason: sp.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    return is the correct choice if I understand your question. If not, post something more specific.
    Code:
    def some_func(var):
         if 3==var:
             return
     
         print " executing optional code "
    
    for choice in [1, 3, 5]:
        print "\n", choice,
        some_func(choice)
        print

    Comment

    • nihilium
      New Member
      • Mar 2008
      • 16

      #3
      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

      Code:
      var = 1
      
      def func():
          global x, y, z, w
          x = 1
          if var:
              w = 42
              return
          y = 2
          z = 3
      func()
      instead of just

      Code:
      var = 1
      
      x = 1
      if var:
          w = 42
          magic_pixie_dust() #leaves file
      y = 2
      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.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        (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.
        Code:
        def func(vars_dict):
             x = 1
             if var in vars_dict:
                 vars_dict["w"] = 42
                 return vars_dict
             y = vars_dict["default"][0]
             z = vars_dict["default"][1]
        
        vars_dict = func(vars_dict)
        Note that you can store functions in a dictionary as well so you can use something like
        Code:
        def func1():
            print "func1 called",
        
        test_dict={1:func1}
        
        for ctr in range(3):
            print "\n", ctr,
            if ctr in test_dict:
                test_dict[ctr]()

        Comment

        Working...