Các biến toàn cầu là các biến không được xác định bên trong bất kỳ hàm nào và có phạm vi toàn cầu trong khi các biến cục bộ là các biến được xác định bên trong một hàm và phạm vi của nó chỉ giới hạn ở hàm đó. Nói cách khác, chúng ta có thể nói rằng các biến cục bộ chỉ có thể truy cập bên trong hàm trong đó nó được khởi tạo trong khi các biến toàn cầu có thể truy cập trong toàn bộ chương trình và bên trong mọi hàm. Các biến cục bộ là các biến được khởi tạo bên trong một hàm và chỉ thuộc về hàm cụ thể đó. Nó không thể được truy cập ở bất cứ đâu bên ngoài chức năng. Hãy cùng xem cách tạo một biến cục bộ. are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function. Local variables are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.
Ví dụ: Tạo các biến cục bộCreating local variables
Python3
def
f[]:
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined4
NameError: name 's' is not defined5
NameError: name 's' is not defined6
Đầu ra
I love Geeksforgeeks
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Example:
Python3
def
f[]:
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks6
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks7
NameError: name 's' is not defined6
NameError: name 's' is not defined4
NameError: name 's' is not defined5
Output:
NameError: name 's' is not defined
Đầu ra
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Biến toàn cầuDefining and accessing global variables
Python3
def
f[]:
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
Me too. I love Geeksforgeeks6
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks7
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined6
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
UnboundLocalError: local variable 's' referenced before assignment4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks7
Đầu ra
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Biến toàn cầu As there are no locals, the value from the globals will be used but make sure both the local and the global variables should have same name.
Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.
Python3
def
f[]:
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined4
NameError: name 's' is not defined5
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined6
NameError: name 's' is not defined4
NameError: name 's' is not defined5
Đầu ra
Me too. I love Geeksforgeeks
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Biến toàn cầu
Example:
Python3
def
f[]:
Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
Me too. I love Geeksforgeeks6
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks7
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined6
Output:
UnboundLocalError: local variable 's' referenced before assignment
Đầu ra
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Biến toàn cầuglobal keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f[], so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example:
Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.Using global keyword
Python3
def
f[]:
Ví dụ: Xác định và truy cập các biến toàn cầu
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined4
NameError: name 's' is not defined5
Biến S được định nghĩa là biến toàn cầu và được sử dụng cả bên trong hàm cũng như bên ngoài hàm.
NameError: name 's' is not defined4
6LƯU Ý: Vì không có người dân địa phương, giá trị từ toàn cầu sẽ được sử dụng nhưng đảm bảo cả hai biến cục bộ và toàn cầu nên có cùng tên.
NameError: name 's' is not defined6
NameError: name 's' is not defined4
NameError: name 's' is not defined5
Đầu ra
Python is great! GFG Look for Geeksforgeeks Python Section Look for Geeksforgeeks Python Section
Nếu chúng ta sẽ cố gắng sử dụng biến cục bộ bên ngoài chức năng thì hãy để xem điều gì sẽ xảy ra.
Biến toàn cầuUsing global and local variables
Python3
Đây là những cái được xác định bên ngoài bất kỳ chức năng nào và có thể truy cập trong suốt chương trình, tức là, bên trong và bên ngoài mọi chức năng. Hãy cùng xem cách tạo ra một biến toàn cầu.
def
f[]:
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined11
NameError: name 's' is not defined12
Ví dụ: Xác định và truy cập các biến toàn cầu
NameError: name 's' is not defined0____11
NameError: name 's' is not defined2
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined22
NameError: name 's' is not defined12
Biến S được định nghĩa là biến toàn cầu và được sử dụng cả bên trong hàm cũng như bên ngoài hàm.
LƯU Ý: Vì không có người dân địa phương, giá trị từ toàn cầu sẽ được sử dụng nhưng đảm bảo cả hai biến cục bộ và toàn cầu nên có cùng tên.
Bây giờ, điều gì sẽ xảy ra nếu có một biến có cùng tên được khởi tạo bên trong một chức năng cũng như toàn cầu. Bây giờ câu hỏi đặt ra, biến cục bộ sẽ có một số ảnh hưởng đến biến toàn cầu hoặc ngược lại, và điều gì sẽ xảy ra nếu chúng ta thay đổi giá trị của một biến bên trong hàm f []? Nó sẽ ảnh hưởng đến toàn cầu? Chúng tôi kiểm tra nó trong phần mã sau: & nbsp;
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined36
NameError: name 's' is not defined12
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined40
NameError: name 's' is not defined12
NameError: name 's' is not defined6
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined40
NameError: name 's' is not defined12
NameError: name 's' is not defined47
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined40
NameError: name 's' is not defined12
NameError: name 's' is not defined52
NameError: name 's' is not defined4
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks5
NameError: name 's' is not defined40
NameError: name 's' is not defined12
Đầu ra
global : 1 Inside f[] : 1 global : 1 Inside g[] : 2 global : 1 Inside h[] : 3 global : 3
____101011
Python is great! GFG Look for Geeksforgeeks Python Section Look for Geeksforgeeks Python Section1Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.