473,387 Members | 1,569 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,387 software developers and data experts.

problem with global variable in a module

def aa():
global b
b=b+1
print b

b=1
aa()

The above code runs well in python shell.

However I have a problem as follows.

new a file named test.py
------------------------------------------------------------------------

def aa():
global b
b=b+1
print b
-------------------------------------------------------------------------

Then in python shell,

from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.
Thanks in advance

Nov 25 '06 #1
3 8886
"hollowspook" <ho*********@gmail.comwrote:
from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.
The global statement makes a name global to a module, it doesn't make it
global to the entire program. The function aa is in module test, the
statements you entered in the shell are in a different module called
__main__.

The particular form of import you used may also be confusing you: just
because you imported '*' doesn't change the function: 'aa' was defined in
the module 'test' and that is where it still looks for its global
variables, all the import did was define a new name 'aa' in the __main__
module which refers to the same function object as 'aa' in 'test'.

Nov 25 '06 #2
hollowspook schrieb:
def aa():
global b
b=b+1
print b

b=1
aa()

The above code runs well in python shell.

However I have a problem as follows.

new a file named test.py
------------------------------------------------------------------------

def aa():
global b
b=b+1
print b
-------------------------------------------------------------------------

Then in python shell,

from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.
Because python does not know "real" globals - globals are always only
global on a module level. Now using the "from module import *"-syntax
asks for trouble. Because your global b above is global to the
__main__-module, not to the module test. But the aa-function expects it
to live in tests.

To make your example work, do it as this:

import test

test.b = 1
test.aa()
See
http://effbot.org/pyfaq/tutor-whats-...foo-import.htm

Diez
Nov 25 '06 #3
Thanks for all the replys.
"Diez B. Roggisch дµÀ£º
"
hollowspook schrieb:
def aa():
global b
b=b+1
print b

b=1
aa()

The above code runs well in python shell.

However I have a problem as follows.

new a file named test.py
------------------------------------------------------------------------

def aa():
global b
b=b+1
print b
-------------------------------------------------------------------------

Then in python shell,

from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.

Because python does not know "real" globals - globals are always only
global on a module level. Now using the "from module import *"-syntax
asks for trouble. Because your global b above is global to the
__main__-module, not to the module test. But the aa-function expects it
to live in tests.

To make your example work, do it as this:

import test

test.b = 1
test.aa()
See
http://effbot.org/pyfaq/tutor-whats-...foo-import.htm

Diez
Nov 26 '06 #4

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

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
6
by: Gurpreet Sachdeva | last post by:
I have two classes; a.py --> #!/usr/bin/python global test test ='' class a(b): def __init__(self,test): print test
2
by: Anand Subramanian | last post by:
Hi, Can someone explain the differences(setup, pre-main() setup/initialization) between global variables in a C++ and a C program? The global variables I used are uninitialized. I have a test.o...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
9
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
2
by: panteraboy | last post by:
Hello again Humble helpfulls. Im having difficult with a module i have created. As its my first it was never going to be straightforward lol. But I was looking at one of the other treads and found it...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
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: 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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.