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

How to give a global variable to a function which is in a module?

Hi,

I would like to declare a global variable, which is seen to a
particular function. If I do as the following it works:

x = 1
def test():
global x
print x
return 1

However, it does not helps since my function is in a separate file. In
other words I have a main program which has the following structure:

from test import test
x = 1
y = test()

and I have a test.py file which contains the "test" function:
def test():
global x
print x
return 1

In this case the test does not see the global variable x. How can I
make the x to be visible?

Thank you.
Oct 1 '08 #1
2 1099
Kurda Yon a écrit :
Hi,

I would like to declare a global variable, which is seen to a
particular function.
First point : there's no real 'global' scope in Python. 'global' really
means 'module-level'.

Second point : globals are Bad(tm) anyway.
If I do as the following it works:

x = 1
def test():
global x
print x
return 1
You don't even need the global statement here, since you're not
rebinding x within the function.
However, it does not helps since my function is in a separate file. In
other words I have a main program which has the following structure:

from test import test
x = 1
y = test()
That's obviously something you *don't* want - functions depending on
global variables that can be changed from anywhere. Ok, like any golden
rule, this one is meant to be bypassed sometimes, but unless you fully
understand why this is 99 times out of 100 a BadThing(tm) to do, just
don't do it.

for the record, this would work:

import test
test.x = 42
y = test()

But once again : unless you really know what you're doing and why you're
doing it, just don't do it.
and I have a test.py file which contains the "test" function:
def test():
global x
print x
return 1

In this case the test does not see the global variable x. How can I
make the x to be visible?
Pass it to the function.

# test.py
def test(x):
print x
return x + 42

# main.py
from test import test
y = test(2)

Oct 1 '08 #2
Kurda Yon wrote:
Hi,

I would like to declare a global variable, which is seen to a
particular function. If I do as the following it works:

x = 1
def test():
global x
print x
return 1
If you are just reading x, the global statement does nothing and is not
needed.
However, it does not helps since my function is in a separate file. In
other words I have a main program which has the following structure:

from test import test
x = 1
y = test()

and I have a test.py file which contains the "test" function:
def test():
global x
print x
return 1

In this case the test does not see the global variable x. How can I
make the x to be visible?
'Global' means 'global to the current module in which the global
statement appears. Yes this is confusing. 'Modular' would be more
exact, but 'global' goes back to before Python when there were no
separate modules connected together.

Either pass x to the function (probably best) or put x into the imported
module with 'test.x = 1".

Terry Jan Reedy

Oct 1 '08 #3

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
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...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
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: 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
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
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
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...
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
tracyyun
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...

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.