469,331 Members | 6,696 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Inheritance


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
2 989
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
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.

Similar topics

2 posts views Thread by Graham Banks | last post: by
4 posts views Thread by JKop | last post: by
5 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by davidrubin | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
6 posts views Thread by Bart Simpson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.