There's no real reason to do that, since conditionals are already supplied using the if
statement:
if a == True:
print 1
But, if you're just asking for fun, the answer is you can't really. To stop the with
content from executing, conditional
will need to somehow stop execution, in its __enter__
method. But the only way it can do that is raising an exception, which means no other code will run, unless you wrap the with
with a try
statement for handling cases a != True
Edit: seeing I was prosecuted in the comments and votes for using the OP's condition (a == True
) I considered changing it to if a
, which is of course the idiom in Python for testing conditionals. But, we do not know what the OP had in mind, and whether he really does want a
to be a boolean, and doesn't want block to execute if a = [1]
(which will pass if a
) I decided to leave it as is.