Ramana
Ramana

Reputation: 253

How to implement a #define like functionality in Python to isolate different pieces of code

I'm struggling to transfer some of the processes that I had followed while developing code in C++ to Python.

One of this is a use of the preprocessing directives #define. (I know it is not a good programming practice but I find it very useful during development when I had to isolate certain pieces of code for testing purposes.

For example, the code for one particular feature could be spread throughout a given file. So I would enclose all of these code snippets within the same #define directive like

#ifdef FEATURE1 ..<code snippet 1>
#ifdef FEATURE1... <code snipper 2> and so on.

Now if I add

#define FEATURE1

at the beginning of the file, then I test feature 1.

And then I can also eliminate feature 1 testing when I'm testing for feature 2.

And in this way, I can isolate bugs which may be appearing due to the code in feature 1 and not due to the code in feature 2.

I cannot find any parallel functionality like this in Python. Individually commenting out different code snippets can lead to bug because sometimes we will miss including some snippets for feature what while testing for feature to.

I'm surviving by marking each snippet of code with comments saying that it belongs to feature 1 or feature 2. But then at the end, I have to move all of these comments before delivering the code.

It will be great to find a constructor a workaround in Python to achieve the same objective.

Thank you for any inputs

Upvotes: 0

Views: 60

Answers (1)

SpghttCd
SpghttCd

Reputation: 10860

I just use simple if statements:

Feature1 = False
Feature2 = True

if Feature1:
    pass # add some code here

if Feature2:
    pass # add some other code here

Upvotes: 2

Related Questions