HMS Surprise <jo**@datavoiceint.comwrote:
I am trying to understand the 'if' statement and the exec statement in
the code below.
from PyHttpTestCase import PyHttpTestCase
from com.bitmechanic.maxq import Config
global validatorPkg
if __name__ == 'main':
validatorPkg = Config.getValidatorPkgName()
# Determine the validator for this testcase.
exec 'from '+validatorPkg+' import Validator'
'global' inside a function makes a name have global scope in the function
where it would otherwise have been local. 'global' at file scope is
completely pointless.
In particular what is the purpose of the
surrounding plus signs?
The plus sign are simply concatenating strings.
exec should generally be avoided for several reasons. Here it is being used
simply to import from a module whose name has been determined at runtime: a
call to the __import__ builtin could be used for the same thing.
May I assume the if statement overrides an
imported assignment statement.
There isn't anything in the code you pasted which could be assigning to the
name validatorPkg. If the code is run as the main script then validatorPkg
is set to some value. If the code is imported as a module then validatorPkg
will (unless there is some very convoluted code) be unset when the exec is
executed.
It would appear that the 'if' statement serves purely to make the code fail
if it is imported as a module.