472,374 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

global variables ?

how do I assign a value to a variable inside a function then use it in the
main body ?

i am trying to unpickle a dictionary in a function to use it in the program

ruari
Jul 18 '05 #1
5 15941
ruari mactaggart wrote:
how do I assign a value to a variable inside a function then use it in the
main body ?
If the variable name is for example 'isbadforyou', start the function with
the statement:

global isbadforyou

i am trying to unpickle a dictionary in a function to use it in the
program


By far the best approach for this is to have the function work with
local variables (faster as well as cleaner), preparing the dictionary
say in local variable 'result', and end the function with:

return result

then, the function's caller gets to decide the name (a sounder
organization!) just by calling the function in some way such as:

thenameilike = thefunction(its, arguments, ifany)
Alex

Jul 18 '05 #2

"ruari mactaggart" <ru***@charliefortune.com> wrote in message
news:bo**********@news8.svr.pol.co.uk...
how do I assign a value to a variable inside a function then use it in the
main body ?

i am trying to unpickle a dictionary in a function to use it in the program

Since Alex has already mentioned the 'global' statement,
I won't dwell on it. I'd just mention that it's a whole lot
simpler to use a global dictionary or an object instance
than to use a simple variable that has to be rebound.

For example:

foobar = "huh?"

def snafu():
global foobar
foobar = "what?"
Using a dictionary, it looks like this:

foodict = {foobar: "huh?"}

def snafu():
foodict["foobar"] = "what?"

That lets you consolidate all of those messy global variables
in one place as well as giving you a name you can use for
better internal documentation.

John Roth
ruari

Jul 18 '05 #3
thank you ! It works now. This is very satisfying.

John Roth <ne********@jhrothjr.com> wrote in message
news:vq************@news.supernews.com...

"ruari mactaggart" <ru***@charliefortune.com> wrote in message
news:bo**********@news8.svr.pol.co.uk...
how do I assign a value to a variable inside a function then use it in the main body ?

i am trying to unpickle a dictionary in a function to use it in the

program

Since Alex has already mentioned the 'global' statement,
I won't dwell on it. I'd just mention that it's a whole lot
simpler to use a global dictionary or an object instance
than to use a simple variable that has to be rebound.

For example:

foobar = "huh?"

def snafu():
global foobar
foobar = "what?"
Using a dictionary, it looks like this:

foodict = {foobar: "huh?"}

def snafu():
foodict["foobar"] = "what?"

That lets you consolidate all of those messy global variables
in one place as well as giving you a name you can use for
better internal documentation.

John Roth

ruari


Jul 18 '05 #4
Ulrich Petri:

Well - no. It's not.
Globals are bad, ugly, ...


Well, not in and of themselves. They're used in about 200 places in the
standard distribution, and there's talk (although no plans) of extending the
syntax further to allow designation of an encompassing (or possible other)
namespace, as in:

global var in namespace
Emile van Sebille
em***@fenx.com
Jul 18 '05 #5

"ruari mactaggart" <ru***@charliefortune.com> schrieb im Newsbeitrag
news:bo**********@news6.svr.pol.co.uk...
John Roth <ne********@jhrothjr.com> wrote in message
news:vq************@news.supernews.com...
Using a dictionary, it looks like this:

foodict = {foobar: "huh?"}

def snafu():
foodict["foobar"] = "what?"

That lets you consolidate all of those messy global variables
in one place as well as giving you a name you can use for
better internal documentation.

John Roth


thank you ! It works now. This is very satisfying.


Well - no. It's not.
Globals are bad, ugly, ...
Jul 18 '05 #6

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
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...
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...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.