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 "<interacti ve 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 3 8915
"hollowspoo k" <ho*********@gm ail.comwrote:
from test import *
b=1
aa()
The error message is :
Traceback (most recent call last):
File "<interacti ve 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'.
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 "<interacti ve 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
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 "<interacti ve 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>
<instructions>
def bar:
<instructions>
|
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
|
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 which declares a global int " int xxx;". Now I link
test.o to a FreeBSD kernel module which then tries to access xxx. If
test.o was compiled from C source the kernel can access the global
variable (which should be of course its own copy in kernel...
|
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 second name for better
readable. After optimization, global variables might be misaligned
because each global variables must be converted to 32 bits, but I do
see that C/C++ Compiler do padding between variables. Struct does the
same to do padding....
|
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 been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"
I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.
| |
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 opaque data types to
transfer information in between them.
At some stage I was stuck and needed to make a variable global, and I
also needed to make the struct declaration public to some other parts.
Looking through the code I found out that lots...
|
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 */
#include <time.h>
|
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
|
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 quite helpful about global variables but i eventually ran into difficulty..
the following code is for the click event that calls the Clicker Module
Private Sub Image30_Click()
Dim counterx As Module
Set counterx = Clicker...
|
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 = ""
Then I create a class, some methods etc. In one of the methods I assign
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |