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?

Recommended Answers

All 16 Replies

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

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!

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?

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?

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.

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 is right, global constants have their place, but like he mentioned, make them stick out. One time when all upper case is appropriate.

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 you really 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 ?

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.

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 :)

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.

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...

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.

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)))

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 :) !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.