Inheritance

H

HMS Surprise

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()
 
B

Brian van den Broek

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:


HTH,

Brian vdB
 
D

Duncan Booth

HMS Surprise said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

EOL character 0
Scope - import and globals 3
Class name as argument 1
Importing 2
Nosetests 8
Building App doesnt work 1
I am a newbie for python and try to understand class Inheritance. 5
docstring inheritance 1

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top