When will `if False` run in Python?

While looking at the code, I came across this line:

if False: #shedskin 

I understand that Shedskin is a kind of Python compiler -> C ++, but I cannot understand this line.

Shouldn't if False: execute? What's going on here?

For context:

This is the whole block:

 if False: # shedskin AStar(SQ_MapHandler([1], 1, 1)).findPath(SQ_Location(1,1), SQ_Location(1,1)) 

More context is in Google Code (scroll down).

+4
source share
5 answers

He will not be executed because he should not. if False: is to intentionally prevent the execution of the next line, since the purpose of this code seems to help Shed Skin print information about the argument of the AStar() function.

You can see another example of this in httplib :

 # Useless stuff to help type info if False : conn._set_tunnel("example.com") 
+7
source

It will never be executed. This is one way to temporarily disable some code.

+7
source
Theoretically, it can be performed:
 True, False = False, True if False: print 'foo' 

But usually it will be used to temporarily disable the code.

+3
source

You are right in believing that this will never be evaluated as true. This is sometimes done when the programmer has a lot of debugging code but doesn't want to remove the debugging code in the release, so they just put if False: above all.

+2
source

Not enough reputation to comment, but apparently the correct answer is stone right. Suppose we have a function like this:

 def blah(a,b): return a+b 

now, to execute type inference, there must be at least one blah call, or it becomes impossible to know the types of arguments at compile time.

for a stand-alone program, this is not a problem, since everything that needs to be compiled to run it is called indirectly from somewhere.

for the extension module, calls can come from "outside", so sometimes we need to add a "fake" function call to output the type so that it becomes possible .. therefore, "if False".

The shedskin sample set has several programs that are compiled as extension modules to be combined, for example, with pygame or multiprocessing.

+2
source

All Articles