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.
source share