Computer Science
When is global statement used ? Why is its use not recommended ?
Python Functions
6 Likes
Answer
If we want to assign some value to the global variable without creating any local variable then global statement is used. It is not recommended because once a variable is declared global in a function, we cannot undo the statement. That is, after a global statement, the function will always refer to the global variable and local variable cannot be created of the same name. Also, by using global statement, programmers tend to lose control over variables and their scopes.
Answered By
3 Likes
Related Questions
What is scope ? What is the scope resolving rule of Python ?
What is the difference between local and global variable ?
Write the term suitable for following descriptions :
(a) A name inside the parentheses of a function header that can receive a value.
(b) An argument passed to a specific parameter using the parameter name.
(c) A value passed to a function parameter.
(d) A value assigned to a parameter name in the function header.
(e) A value assigned to a parameter name in the function call.
(f) A name defined outside all function definitions.
(g) A variable created inside a function body.
What do you understand by local and global scope of variables ? How can you access a global variable inside the function, if function has a variable with same name.