By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Inheritance

P: n/a

I am trying to understand the 'if' statement and the exec statement in
the code below. I would like to add several common routines to this
class and then inherit it into a class in another file. This other
class would need to access these common functions as well as inherit
the PyHttpTestCase class. In particular what is the purpose of the
surrounding plus signs? May I assume the if statement overrides an
imported assignment statement.
Thanks,

jh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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'
# definition of test class
class baseClass(PyHttpTestCase):
def logon()
print 'logon()'

def runTest(self):
print 'runTest()'

def myFn(self):
print 'myFn()'

# Code to load and run the test
if __name__ == 'main':
test = baseClass("bClass")
test.runTest()

May 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
HMS Surprise said unto the world upon 05/22/2007 02:40 PM:
I am trying to understand the 'if' statement and the exec statement in
the code below. I would like to add several common routines to this
class and then inherit it into a class in another file. This other
class would need to access these common functions as well as inherit
the PyHttpTestCase class. In particular what is the purpose of the
surrounding plus signs? May I assume the if statement overrides an
imported assignment statement.
Thanks,

jh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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'


The if test is, AFAICT, ensuring that validatorPkg is defined.
Config.getValidatorPkgName() likely returns a string. The + signs are
just concatenating a string to be exec'ed:
>>>validatorPkg = 'some string returned by getValidatorPkgName()'
'from '+validatorPkg+' import Validator'
'from some string returned by getValidatorPkgName() import Validator'
>>>>

HTH,

Brian vdB
May 22 '07 #2

P: n/a
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.
May 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.