0

I am working on a refactoring a Python program. I have copied and pasted some messy source files into new Python modules and I'm working through the module top-to-bottom cleaning it up etc.

As a result, the bottom part of my module is a mess of code fragments and functions that don't necessarily compile - or if they do compile, they crash at runtime or otherwise misbehave.

However I'd like to test the bits of the module that I have worked on as I go by importing the new module into the program where it is to be used and integrating the bits that are done.

Does Python have an equivalent that could be used similarly to PHP's return (when used in the top level of an imported file, outside of a function) or for this case, even better - __halt_compiler, which tells the compiler to disregard the remainder of the file and treat it as if the file stopped there?

This is not the same as exit, because with exit the entire program terminates, whereas I'd like the code that imports the new module to continue executing as if it had encountered the end-of-file at a given point.

ec2011
  • 570
  • 6
  • 20
  • 1
    If you're sure that you _really, truly_ want to implement his behavior, you could simply raise an exception in the module that you want to stop executing, which can be caught in the importing module. – Brian61354270 Jul 20 '20 at 12:14
  • 1
    @Brian that won't work with `SyntaxError`, nothing will actually get executed, because that is raised *at compile time* – juanpa.arrivillaga Jul 20 '20 at 12:17
  • 2
    Anyway, no I don't think there is anything like this in Python. – juanpa.arrivillaga Jul 20 '20 at 12:17
  • Just comment out all the code you're not interested in. – Roy2012 Jul 20 '20 at 12:24
  • 1
    Multiline comments should do the trick everything between a triple quote or double-quote is a comment until another triple quote or double quote is found, so just write `"""` at the end of the file and `"""` where you want to stop. – Adirio Jul 20 '20 at 12:29
  • @Adirio, I thought of using triple quote, but that's not a simple one-line solution if there are more triple-quoted strings/comments in the remainder of the file - which there are several as it is common practice to put a triple-quoted description of each function in the first line of the function. – ec2011 Jul 20 '20 at 13:01
  • @juanpa.arrivillaga Not sure of Stack-Exchange etiquette regarding what's a comment and what's an answer but this sounds like an answer to me... – ec2011 Jul 20 '20 at 13:01
  • 1
    @ec2011 Are all your comments with single or double quote? You may be able to use the other. In case you have both of them, cut-pasting it to a temporal file seems to be the easiest solution to achieve what you want, provided that there is nothing like the requested directive in Python – Adirio Jul 21 '20 at 07:55
  • Interesting idea, I think they're with triple double-quotes, I didn't realise you could use `'''` in the same way, but I guess it makes sense. Thanks that's really helpful. – ec2011 Jul 21 '20 at 11:17

0 Answers0