How to do conditional compilation in Python ?
Is it using DEF ?
Python compiles a module automatically when you import it, so the only way to avoid compiling it is to not import it. You can write something like:
if some_condition:
import some_module
But that would only work for complete modules. In C and C++ you typically use a preprocessor for conditional compilation. There is nothing stopping you from using a preprocessor on your Python code, so you could write something like:
#ifdef SOME_CONDITION
def some_function():
pass
#endif
Run that through a C preprocessor and you'd have real conditional compilation and some_function will only be defined if SOME_CONDITION is defined.
BUT (and this is important): Conditional compilation is probably not what you want. Remember that when you import a module, Python simply executes the code in it. The def and class statements in the module are actually executed when you import the module. So the typical way of implementing what other languages would use conditional compilation for is just a normal if statement, like:
if some_condition:
def some_function():
pass
This will only define some_function if some_condition is true.
It's stuff like this that makes dynamic languages so powerful while remaining conceptually simple.