Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialvikas pal
11,563 Pointswhat is global?
please tell me in brief what is global in thus video.i also doesn't understand it from teachers notes.please tell me what it is.
1 Answer
Alexander Davison
65,469 PointsIf you define variables in the global scope (a variable is in the global scope if you don't define it in functions), it is accessable anywhere in your program:
# Not in any kind of function
name = "Alex"
If you define a variable in the function scope (when you define it inside a function), that variable is only accessable to that function:
>>> # in the Python Shell
>>>
>>> def some_func():
... name = "Alex"
... print(name)
>>> some_func()
Alex
>>> name
Traceback (most recent call last):
File <pyshell#3>, line 1, in <module>
name
NameError: name 'name' is not defined
But, it is possible to make it go the global scope, too:
def some_func():
global name
name = "Alex"
However, it's considered bad practices in many ways, so try not to use it too often :)
It can make your program more confusing.
Good luck! I hope you understand! ~Alex
vikas pal
11,563 Pointsvikas pal
11,563 PointsKenneth Love Ken Alger