473,385 Members | 2,028 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,385 software developers and data experts.

Scope - import and globals


In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.

Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?
Thanks,

jh

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from PyHttpTestCase import PyHttpTestCase
from baseClass import baseClass

# definition of test class
class temp(baseClass):

def runTest(self):
global hostName
hostName = getHostNameFromUser()
self.logon()

May 29 '07 #1
2 1499

"HMS Surprise" <jo**@datavoiceint.comskrev i en meddelelse
news:11**********************@p47g2000hsd.googlegr oups.com...
>
In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.

Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?
If a file needs an import, make the import!
Dont rely on other file's imports , in general, imho.

Not sure excactly what you are doing, but are you sure you dont want an
instance variable instead of a global ?
self.hostName = "blah"
And then a getHostName() method in the class ?

regards
Troels

May 29 '07 #2
HMS Surprise wrote:
>
In the file snippet below the value for the global hostName is
determined at runtime. Functions imported from the parent baseClass
file such as logon also need access to this variable but cannot see it
the with the implementation I have attempted here.
Use a class variable:

class baseClass:
hostName = None # undefined yet

def someFunc(self):
assert self.hostName is not None, "hostname not set yet"
... # use hostName here

class temp(baseClass):
def runTest(self):
baseClass.hostName = getHostName()
...

or a global variable:

baseClass.py:

hostName = None
class baseClass:
def someFunc(self):
assert hostName is not None
....

testme.py:

import baseClass
class temp(baseClass.baseClass):
....
baseClass.hostName = getHostName()

although neither solution strikes me as very elegant. I would normally pass
the hostname to the constructor of baseClass or use a separate 'settings'
module.

Global variables are per-module. Use the "global" keyword when assigning a
global variable in the 'current' module. Global variables of other modules
are properties of the module, use <module>.<name>.
>
Also, functions in this file and in the imported parent class need
PyHttpTestCase. Does there need to be an import statement in both
files?
Yes. Don't worry, the work is done only once.

Regards,
Tijs
May 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lawrence | last post by:
Can anyone suggest to me a strategy for including() a variable on a page from within a function but somehow getting that function into global space? Let us suppose that I have this document: ...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
4
by: Hugh Macdonald | last post by:
We're starting to version a number of our python modules here, and I've written a small function that assists with loading the versioned modules... A module would be called something like:...
10
by: m.epper | last post by:
Hi to everybody. First of all sorry for my english, I'm italian. How can I execute a portion of code, in a function, into the global scope? Example: <?php
5
by: Sandra-24 | last post by:
Is there a way in python to add the items of a dictionary to the local function scope? i.e. var_foo = dict. I don't know how many items are in this dictionary, or what they are until runtime. ...
9
by: bmaron2 | last post by:
Hello everybody, I have a (hopefully) simple question about scoping in python. I have a program written as a package, with two files of interest. The two files are /p.py and /lib/q.py My file...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
9
by: Pat | last post by:
I have a Globals class. In it, I have a variable defined something like this: remote_device_enabled = bool In one module, I assign True/False to Globals.remote_device_enabled. Once set,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.