Python 3 Tutorial: Global Variables and The Global Statement

preview_player
Показать описание
Be sure to like, share and comment to show your support for our tutorials.

=======================================
======================================
Global Variables and The Global Statement In Python

In this Python tutorial, we will talk about global variables and the Global Statement in Python. We saw in the previous tutorial that local variables live within the function and global variables where variables that lived outside a function. We also mentioned that local variables could not be called outside a function and vertically did not exist outside of function.

Before we even go any farther I am going to preach to you for a second. I hate global variables and I will tell you why. Global variables to me are the like the problem child. They are all fine and they are good to you until you have a crappy day and they come along and screw with you.

As you know from the previous tutorial global variables are available inside and outside functions. This is mostly True, we can not change a global value inside a function without our little friend "global statement". More on Mr. global statement shortly. When working with global variables they tend to come back and bite you larger programs because you may forget you already used a name to name global when you are trying to use the name somewhere else. This may or may not interfere with you program but down the road it may get a bit confusing.

I try to put everything into a function sure there may be a time when that is not possible but trust me I will try to get that variable into a function.

I think of functions as minnie programs and I do not like the outerworld messing with my minnie functions and this is why I try my hardest to use only local variables where I can.
Рекомендации по теме
Комментарии
Автор

Wonderful tutorials, thank you very much. They helped me immensly

Khmeriscool
Автор

Thank you indeed, it was absolutely great and easy to undrestanding and i am going to call you graet master :-)

hosseinhabibi
Автор

how do you make a local variable a global variable in an IF statement?
For example:
If num == 6:
x = y
i want x to be a global variable for y if this statement is true

henrykid
Автор

i wanna use a global variable for two file . how can i do that ? Where one file has imported another file

shubhamkhantwal
Автор

How many times "alright" was said?)

MyDifferentSongs