Is it possible to finish importing a python module with something like a return?

I would like to know if there is a way to write the module code below without adding another layer of indentation to the code of the whole module.

# 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 go fine.

+6
source share
3 answers

I donโ€™t know about any solution to this question, but I think you could put all your code in an internal module and import it if the condition is not met.

+5
source

I do not know how to do that. The only thing I could imagine would be return , but it should be inside the function.

+1
source

Itโ€™s hard to say without knowing what your goal is at a higher level. (For example, what is the condition? Why does it matter? SURE YOU have no problem with XY? Can you just tell us what your overall goal is?) It is also very difficult to say without knowing how the module will be called. (How is a script from the command line? Imported by another module?) And this will help to find out (a) why you are trying to avoid indentation (World War II is over, and we do not need, or, more kindly, Python is a language that uses indentation like SYNTACTIC FEATURE, so โ€œI canโ€™t use this syntax functionโ€ amazes many people as a strange restriction. - then tests: theoretically you could work on this restriction, perhaps sometimes, but why do you go into the boxing ring with your hands behind your back ?) and (b) why can't you e throw an exception (no, really: you are absolutely sure that you absolutely do not can against all exceptions?).

Be that as it may, all you really did was ask "how do I do X, given conditions A, B and C?" a question without specifying why you want to make X, or why conditions A, B and C exist, or even if you are 100% sure that they exist and cannot be processed.

If you really say, โ€œI don't want to hit {TAB} 40 times when writing a function,โ€ then the real problem is that you need a better text editor. If what you are actually saying is โ€œI was fortunate enough to find an indentation to be aesthetically unpleasant,โ€ then you should think about (a) that the other side of the argument; that is, why Python people use padding as syntax to be useful; (b) whether your own aesthetic preferences in this regard are more important than the reasons you came up with in (a); and (c), given that Python is the right tool that you personally use to achieve your own large-scale goal. (It's okay not to use indentation as a syntax function, but it is so fundamental to Python that it is philosophically opposed to such an extent that this rule is a strong sign that Python may not be the ideal language for you to execute your programming goal.) If you really say that you will need to use the factoring code, which must be run in two different sets of circumstances, in two modules, then it will be useful for you to reorganize. If you say that you have spaghetti code that ends up completely impossible for refactoring, then this is really the first problem that needs to be solved before trying to abort the import of modules.

0
source

Source: https://habr.com/ru/post/899223/


All Articles