views:

1132

answers:

4

How to do conditional compilation in Python ?

Is it using DEF ?

+13  A: 

Python isn't compiled in the same sense as C or C++ or even Java, python files are compiled "on the fly", you can think of it as being similar to a interpreted language like Basic or Perl.1

You can do something equivalent to conditional compile by just using an if statement. For example:

if FLAG:
    def f():
        print "Flag is set"
else:
    def f():
        print "Flag is not set"

You can do the same for the creation classes, setting of variables and pretty much everything.

The closest way to mimic IFDEF would be to use the hasattr function. E.g.:

if hasattr(aModule, 'FLAG'):
    # do stuff if FLAG is defined in the current module.

You could also use a try/except clause to catch name errors, but the idiomatic way would be to set a variable to None at the top of your script.

  1. Python code is byte compiled into an intermediate form like Java, however there generally isn't a separate compilation step. The "raw" source files that end in .py are executable.
Ryan
Is there something like ifdef ?
Pragmatically true, but technically false. Python IS compiled in the same sense as Java, however the compilation step is not made explicit to the programmer.
Arafangion
@Arafangion:Straight to the point... why don't you expand this a [tiny] little bit in an answer?
paprika
@rejinacm: ifdef is an old idiom from C anyway. Why should python have it? it doesn't make sense.
hasen j
+4  A: 

Doesn't make much sense in a dynamic environment. If you are looking for conditional definition of functions, you can use if:

if happy:
    def makemehappy():
        return "I'm good"
gimel
thank you for your answer
+5  A: 

There is actually a way to get conditional compilation, but it's very limited.

if __debug__:
    doSomething()

The __debug__ flag is a special case. When calling python with the -O or -OO options, __debug__ will be false, and the compiler will ignore that statement. This is used primarily with asserts, which is why assertions go away if you 'really compile' your scripts with optimization.

So if your goal is to add debugging code, but prevent it from slowing down or otherwise affecting a 'release' build, this does what you want. But you cannot assign a value to __debug__, so that's about all you can use it for.

DNS
+3  A: 

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.

Martin Vilcans