I don't understand global variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I don't understand global variable

The title says it all, I don't understand global variable. How can I use them? When is it better to use them?.Why ppl say it's dangerous using global variable?. examples?

18th Jun 2018, 9:17 PM
Jp Reyes
Jp Reyes - avatar
2 Answers
+ 2
A global variable is a variable that isn't locked to the scope of a method. For example lets say that you have a method named Foo and a class named Testing. if you define a variable in the class but outside a method at the top if the document. (I don't remember if python interprets fields in order from top down) then that variable us global to that class, other methods may call and modify thus variable. If perhaps you have that method "Foo" and you define a variable in it then it is restricted to that method unless you declare it global. Variables defined at class level are by default global. Variables defined at method level are by default local unless declared global.
18th Jun 2018, 9:27 PM
John
John - avatar
0
Ty
19th Jun 2018, 7:03 PM
Jp Reyes
Jp Reyes - avatar