Python Flask Basics Welcome to Flask Request Variables

vikas pal
vikas pal
8,545 Points

what 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

If 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()
>>> name
Traceback (most recent call last):
    File <pyshell#3>, line 1, in <module>
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