How to declare a global variable in PythonA variable which can be accessed by the other block is called a global variable. It can be defined in outside a block. In other words, a global variable is defined outside the function, and we can access it inside the function. On the other hand, a variable defined in a block and available for in that block is called a local variable. Such a variable can be accessed only in the defined block. Let’s understand the following example of a local and global variable. Example – Local VariablesOutput: The sum is: 30 Explanation:The variable is defined inside the function and it can only use in a defined function so that nature of the variable is called a local variable. We cannot access them in other functions. To overcome this problem, we use global variables. Let’s understand the example of a global variable. Example –Output: The sum is: 30 The sub is: 10 Explanation:In the above code, we have defined two global variables a and b outside the functions. We used them inside the sum() and sub() function. Both functions returned the result when we called. If we define the same name local variable, it will print the value that is inside the function and then global variable value. Example – 3:Output: Hello, how are you? I am fine Explanation:We have defined the local variable the same name as a global variable; first, it printed the local variable and then global variable value. The Global KeywordPython provides the global Keyword that is used to modify the value of the global variable inside the function. It is beneficial when we want to change the value of the global variable or assign some other value. Below are a few rules to define the global variables. Rules of global Keywords
Example – Without global keywordOutput: line 8, in mul c = c * 10 UnboundLocalError: local variable 'c' referenced before assignment Explanation:The above code has thrown an error because we have tried to assign the value to the global variable. We can modify the value of global value inside a function using the global keyword. Example – With global KeywordOutput: The value inside function: 100 The value outside the function: 100 Explanation:In the above example, we have defined the variable c in mul() function using the global keyword. The value of c is multiplied by the 10; therefore, it returned the 200. We can see in the output that the variation in value inside the function is reflected in the value outside the global variable. Global Variables in Python ModulesThe benefit of the global keyword is to create global variables and share them among the different modules. For example – we create a name.py which consists of global variables. If we change these variables, then this change will reflect everywhere. Let’s understand the following example. Code – 1: Make a name.py file to store the global variables. Code – 2: Make a change.py file to modify global variables. Here, we have modified the value of a, b, and msg. These global variables were defined in the name.py file and we imported name, and accessed these variables. Code – 3: Make a result.py file to print the modified global variables. Output: 15 26 Welcome to JavaTpoint Global in Nested FunctionsWe can use the global keyword inside a nested function. We must declare the variable using the global keyword inside a nested function. Let’s understand the following example. Example – Output: Before modifying : 15 Making change After modifying: 15 value of x 20 Explanation: In the above code, the value inside add() takes the value of local variable x = 15. In modify() function, we have assigned the x = 20 using the global keyword. That change reflected in add() function variable. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263611.html