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

Help on object scope?

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 p.py looks like this:

---

from lib import q

def main():
global r
r = q.object1()
s = q.object2()

if __name__ == "__main__":
main()

---

My file q.py in the subdirectory lib looks like this:

class object1:
t = 3

class object2:
print r.t

---

Python gives me an error, saying it can't recognize global name r.
However I define r as global in the top-level main definition! Can
anyone suggest how I can get around this, if I want to define and bind
global names inside of main() which are valid in all sub-modules?

Thanks very much for your help!

Feb 25 '07 #1
9 1175
hg
bm*****@hotmail.com wrote:
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 p.py looks like this:

---

from lib import q

def main():
global r
r = q.object1()
s = q.object2()

if __name__ == "__main__":
main()

---

My file q.py in the subdirectory lib looks like this:

class object1:
t = 3

class object2:
print r.t

---

Python gives me an error, saying it can't recognize global name r.
However I define r as global in the top-level main definition! Can
anyone suggest how I can get around this, if I want to define and bind
global names inside of main() which are valid in all sub-modules?

Thanks very much for your help!
Might be wrong, but globals can only be global to the module they're
declared in.

I suggest you find another way such as passing your object as a parameter

hg
Feb 25 '07 #2
On Feb 25, 9:37 am, hg <h...@nospam.orgwrote:
bmar...@hotmail.com wrote:
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 p.py looks like this:
---
from lib import q
def main():
global r
r = q.object1()
s = q.object2()
if __name__ == "__main__":
main()
---
My file q.py in the subdirectory lib looks like this:
class object1:
t = 3
class object2:
print r.t
---
Python gives me an error, saying it can't recognize global name r.
However I define r as global in the top-level main definition! Can
anyone suggest how I can get around this, if I want to define and bind
global names inside of main() which are valid in all sub-modules?
Thanks very much for your help!

Might be wrong, but globals can only be global to the module they're
declared in.

I suggest you find another way such as passing your object as a parameter

hg
Dear hg,

Thank you for the advice, but that seems a bit unwieldy. My code will
have about 10 of these global objects, all of which interact with
eachother. It seems silly to have to pass 10 parameters around to each
instance I work with. I hope there is a smarter way to do it, or
perhaps someone can suggest a smarter way to code it.

Am I stuck with just having to put all the code in one file? That is
what I wanted to avoid, because the file will get incredibly long. It
seems like the only reason my code above failed is because there were
two separate modules. Perhaps I just need to import /lib/q.py in a
different way?

Many thanks.

Feb 25 '07 #3
>
Thank you for the advice, but that seems a bit unwieldy. My code will
have about 10 of these global objects, all of which interact with
eachother. It seems silly to have to pass 10 parameters around to each
instance I work with. I hope there is a smarter way to do it, or
perhaps someone can suggest a smarter way to code it.

Am I stuck with just having to put all the code in one file? That is
what I wanted to avoid, because the file will get incredibly long. It
seems like the only reason my code above failed is because there were
two separate modules. Perhaps I just need to import /lib/q.py in a
different way?
You can interact just fine, just qualify the objects with the module
names. So in q, you need to use p.r instead of just r.

It's another question if this design is really good - relying on so many
globals. It would certainly be better to have e.g. a class that looks
like this:
import p
import q
class GlueThingy(object):
def __init__(self):
self.p1 = p.SomeObject(self)
self.q1 = q.SomeOtherObject(self)
Then in SomeObject you can use the passed GlueThingy reference to access
other instances:

class SomeObject(object):

def __init__(self, glue):
self.glue = glue

def do_something(self):
self.glue.q1.foobar()
Diez
Feb 25 '07 #4
bm*****@hotmail.com writes:
My code will have about 10 of these global objects, all of which
interact with eachother. It seems silly to have to pass 10
parameters around to each instance I work with.
Yes. A better solution would be to put them inside a module, and
import that module. Then the objects are available at any level as
attributes of the imported module.

===== foo.py =====
def spam():
return max(eggs, ham)

eggs = 17
ham = 12
=====

===== bar.py =====
import foo

def main():
spork = do_something_with(foo.eggs)
foo.spam(spork)
=====

--
\ "Know what I hate most? Rhetorical questions." -- Henry N. Camp |
`\ |
_o__) |
Ben Finney

Feb 25 '07 #5
"hg" <hg@nospam.orgwrote:

bm*****@hotmail.com wrote:
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
Make a third file for all the system wide globals, say param.py:

global r
r = 42

My file p.py looks like this:

---

from lib import q

def main():
from param import *
global r
r = q.object1()
s = q.object2()

if __name__ == "__main__":
main()

---

My file q.py in the subdirectory lib looks like this:
from param import *

class object1:
t = 3

class object2:
print r.t

---

Python gives me an error, saying it can't recognize global name r.
However I define r as global in the top-level main definition! Can
anyone suggest how I can get around this, if I want to define and bind
global names inside of main() which are valid in all sub-modules?

Thanks very much for your help!

Might be wrong, but globals can only be global to the module they're
declared in.
Correct.
>
I suggest you find another way such as passing your object as a parameter
or make the third file and import it everywhere you need them...

- Hendrik

Feb 26 '07 #6
"Diez B. Roggisch" <de***@nospam.web.dewrote:
You can interact just fine, just qualify the objects with the module
names. So in q, you need to use p.r instead of just r.
No. When p.py is being run as a script, the module you need to access is
__main__.

Much better to put the globals into a separate module created specifically
to hold the global variables rather than having them in something which
might or might not be the main script.
Feb 26 '07 #7
On Feb 26, 1:16 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Mon, 26 Feb 2007 07:54:12 +0200, "Hendrik van Rooyen"
<m...@microcorp.co.zadeclaimed the following in comp.lang.python:
from param import *

"from <import *" (or any "from <import ..." variant) is NOT the
best thing to use.

Any rebinding to an imported name breaks the linkage to the import
module.

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
Thank you all for the advice.

The suggestion Dennis made about using a 3rd, "common" module to hold
global names seemed to be the best idea. The only problem is now I
have to type common.r.t instead of just r.t. If I put common in the /
lib directory, it is even worse and I have to type lib.common.r.t. I
like that it is explicit and perhaps this is the Python way, but it is
annoying and produces ugly code to see all those fully-qualified names
when all I'd really like to use is r.t throughout the program.

Is there a way to import lib.common but then re-bind its attributes to
the local space without breaking the linkage?
Feb 27 '07 #8
bm*****@hotmail.com kirjoitti:
<snip>
global names seemed to be the best idea. The only problem is now I
have to type common.r.t instead of just r.t. If I put common in the /
lib directory, it is even worse and I have to type lib.common.r.t. I
like that it is explicit and perhaps this is the Python way, but it is
annoying and produces ugly code to see all those fully-qualified names
when all I'd really like to use is r.t throughout the program.

Is there a way to import lib.common but then re-bind its attributes to
the local space without breaking the linkage?

See section "6.12 The import statement" in the "Python Reference
Manual". (Hint the part "as name" is what you want)

HTH,
Jussi
Feb 27 '07 #9
On Feb 25, 10:25 pm, bmar...@hotmail.com wrote:
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 p.py looks like this:

---

from lib import q

def main():
global r
r = q.object1()
s = q.object2()

if __name__ == "__main__":
main()

---

My file q.py in the subdirectory lib looks like this:

class object1:
t = 3

class object2:
print r.t
"Globals" are only global within the module they are defined in.
Change "global r" to "import __main__; __main__.r = r", and in q.py
add "from __main__ import r".

Feb 27 '07 #10

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

Similar topics

7
by: Ben Thomas | last post by:
Hi all, I'm having some trouble understanding the behavior of std::ostringstream. (I'm using Visual Studio .Net & STL port 4.5.3). I'll appreciate if someone can give me a little explanation of...
4
by: Jason80 | last post by:
First I wrote some _VBScript to get info from OS, and now I wrote some code in VB.Net, and I have a problem now. Look at this script in vbs List1.vbs: strComputer = "."
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
8
by: Agam Mehta | last post by:
Hi, Everything works fine with ixmlhttprequest. It gives me "access violation" only when i am trying to release it from the memory (i.e pXMLHttpReq->Release()). Below is my code....
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
7
by: teddarr | last post by:
I have an assignment I've been working on for a few days now. I need some help with the last detail. The program is supposed to create 5 objects with varying floating-point parameter lists. The...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.