473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1062
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.