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
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?
Can you give us an example?
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
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
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
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
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691