954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

confusion with global statement.

Okay, I am confused on the global statement. To my knowledge it lets you easily access variables from a function to be used in the session. Now someone said that it's useless and you shouldn't use it in your code. Is this true?

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

The global statement allows you to break a variable out of the function scope it'd normally be contained in if you just defined it in a function. The "Global variables are evil" paradigm says that anything to do with a global is inherently evil, and that class scope should usually be used instead. See http://stackoverflow.com/questions/146557/do-you-use-the-global-statement-in-python

JugglerDrummer
Junior Poster
139 posts since Apr 2009
Reputation Points: 14
Solved Threads: 22
 

Why is it evil, it only makes like easier, or so it appears.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

As your code gets more complex and longer, a function could change the value of the global variable and make your code buggy. This kind of bug might be hard to figure out! Now your life has just been made a lot harder!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Well, I always set the global variables in the function to a new variable, then delete the original global variable. So then would the new variable (outside the function) inherit the global trait?

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 
Well, I always set the global variables in the function to a new variable, then delete the original global variable. So then would the new variable (outside the function) inherit the global trait?

Can you give us an example?

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Sure. Here's a little code example.

def testfunc():
    global c
    c = "test"

testfunc()
c1 = c
del c
print (c1)
input ()


So there are only 2 questions.

1. Does c1 inherit the global trait, or remain local
2. Does the global trait remained assigned to a variable named c, or would deleting the variable also delete the global statement.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 
1. Does c1 inherit the global trait, or remain local 2. Does the global trait remained assigned to a variable named c, or would deleting the variable also delete the global statement.


There is nothing special with a global variable. The statement global c in testfunc only means that the name 'c' used in the function's body is the same as the name 'c' defined at module level, and not a new name local to this function. When testfunc returns, nothing remains of this 'global trait' that could be inherited by c1.
As JugglerDrummer said above, not using global variables is a programming paradigm. It's a matter of code design. Module level variables are OK when they have constant value, and usually written in uppercase letters, like

HOST = '127.0.0.1'
PORT = 9000

It shouldn't be a religion, use global variables if you need them, but with some experience with python, you might end up avoiding global variables too.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Gribouillis is right, global constants have their place, but like he mentioned, make them stick out. One time when all upper case is appropriate.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 
def testfunc():
    global c
    c = "test"

testfunc()
c1 = c
del c
print (c1)
input ()


My question is : why don't you do

def testfunc():
    return "test"

c1 = testfunc()
print (c1)

I totally agree with what is said : globals should never be used except if youreally know what you do. It's very dangerous.
I don't see that it makes things easier (maybe when you write your code for first time). In your example, where is the need of a global ?

jice
Posting Whiz in Training
225 posts since Oct 2007
Reputation Points: 64
Solved Threads: 57
 

My question is : why don't you do

def testfunc():
    return "test"
 
c1 = testfunc()
print (c1)

Well, originally I was going to use function parameters to set the variables (i.e. testfunc(x, y, z) ) with the function and use the return, but those variables were hard to handle and weren't very flexible. Thanks for the idea though.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

I was once a fond of global variables as "child" programmer.
As I went on coding, things turned harsh on me. Thanks God I then learned OOP. In OOP, you do alot of stuffs without global variable.
I have forgotten even last time i used global keyword. all I remember is alot of self.xyz and __init__

learn OOP and enjoy classes :)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

Yes dont use global find other soultion like OOP.

Think of function like a black box you can only put in variabels and return variabels out.
Then you can use your function anywhere in your code.
It dont leak out in global space.

Think always about design of your code.
Say u have a global variabel that you want to doubble opp.

global_var = 10

def doubble(x):
    return 2 * x  

new_global = doubble(global_var)
print new_global  #20


Function doubble(x) er invulnerable and you can use it anyware in you code.

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 
Well, originally I was going to use function parameters to set the variables (i.e. testfunc(x, y, z) ) with the function and use the return, but those variables were hard to handle and weren't very flexible. Thanks for the idea though.

Sorry, but I can't understand what is hard to handle and not flexible...
I think exactly the opposite. Especially in python where you have *args and **args to have non fixed number of arguments. And you can return lists or dictionnaries...

jice
Posting Whiz in Training
225 posts since Oct 2007
Reputation Points: 64
Solved Threads: 57
 

Okay, screw the global statement, here's what I've got so far.

class TestClass:
    def TestFunc():
        test = "This is working!"

obj = TestClass

print (obj.test)

input()


Now, that's all I could figure out intuitively. But it's not a surprise that it isn't working. But is there something similar that I could do to get it working.

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

You could use this

class TestClass:
    def __init__(self):
        self.test = None
    def test_func(self):
        self.test = "This is working"

obj = TestClass() # <-- CALL the class to create an instance
print("Before test_func(), obj.test == {0}".format(repr(obj.test)))
obj.test_func()
print("After test_func(), obj.test == {0}".format(repr(obj.test)))
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Uh, that's too long to substitute global, so I think I'll just use the suggested OOP way like snipsat suggested. But thanks for all the help anway :) !

AutoPython
Junior Poster
138 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You