Built-In and Global Scopes
As mentioned earlier, the global scope is your code's main scope (environment). Every script (.py file) has its global scope.
1234567first = "This" second = "is" third = "the" fourth = "main" fifth = "scope" print(first, second, third, fourth, fifth)
Note
There is only the built-in scope out of the global scope.
We can use functionality from outer scopes in inner scopes. This means that we can use built-in functionality in the global scope as well as in local scopes, which are nested within the global scope.
Look at the example:
12345678print(len("some string")) # built-in objects usage number = 15 # global scope def print_number(): print(number) # global object usage print_number()
In the example above, the global variable number is used inside the print_number() function local scope.
You can access variables and functions from outer scopes but cannot change them:
1234567number = 221 def modify_global(): number += 5 modify_global() print(number)
You can access the change of a global variable/function using the global keyword:
123456789101112number = 20 print(number) def modify_global(): global number number += 10 modify_global() print(number) modify_global() print(number)
1. What scope is the main scope of your code?
2. What scope is the Python Tools scope?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 6.67
Built-In and Global Scopes
Scorri per mostrare il menu
As mentioned earlier, the global scope is your code's main scope (environment). Every script (.py file) has its global scope.
1234567first = "This" second = "is" third = "the" fourth = "main" fifth = "scope" print(first, second, third, fourth, fifth)
Note
There is only the built-in scope out of the global scope.
We can use functionality from outer scopes in inner scopes. This means that we can use built-in functionality in the global scope as well as in local scopes, which are nested within the global scope.
Look at the example:
12345678print(len("some string")) # built-in objects usage number = 15 # global scope def print_number(): print(number) # global object usage print_number()
In the example above, the global variable number is used inside the print_number() function local scope.
You can access variables and functions from outer scopes but cannot change them:
1234567number = 221 def modify_global(): number += 5 modify_global() print(number)
You can access the change of a global variable/function using the global keyword:
123456789101112number = 20 print(number) def modify_global(): global number number += 10 modify_global() print(number) modify_global() print(number)
1. What scope is the main scope of your code?
2. What scope is the Python Tools scope?
Grazie per i tuoi commenti!