Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to end a python module import with something like a return?

I would like to know if there is a way of writing the below module code without having to add another indentation level the whole module code.

# module code
if not condition:
    # rest of the module code (big)

I am looking for something like this:

# module code
if condition:
    # here I need something like a `return`
# rest of the module code (big)

Note, I do not want to throw an Exception, the import should pass normally.

like image 286
sorin Avatar asked Oct 13 '11 13:10

sorin


2 Answers

I don't know of any solution to that, but I guess you could put all your code in an internal module and import that if the condition is not met.

like image 115
UncleZeiv Avatar answered Sep 21 '22 15:09

UncleZeiv


I know of no way to do this. The only thing I could imagine that would work would be return but that needs to be inside a function.

like image 45
David Heffernan Avatar answered Sep 18 '22 15:09

David Heffernan