How do you implement “#ifdef” in python?

前端 未结 8 2086
广开言路
广开言路 2020-12-02 17:04

Programming in C I used to have code sections only used for debugging purposes (logging commands and the like). Those statements could be completely disabled fo

8条回答
  •  忘掉有多难
    2020-12-02 17:40

    This can be achieved by passing command line argument as below:

    import sys
    
    my_macro = 0
    
    if(len(sys.argv) > 1):
        for x in sys.argv:
            if(x == "MACRO"):
                my_macro = 1
    
    if (my_macro == 1):
        controlled text
    

    Try running the following script and observe the results after this:

    python myscript.py MACRO
    

    Hope this helps.

提交回复
热议问题