Hướng dẫn local variable and global variable in python - biến cục bộ và biến toàn cục trong python

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 defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

NameError: name 's' is not defined
6

Đầ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 defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

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 defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Me too.
I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
UnboundLocalError: local variable 's' referenced before assignment
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

Đầ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 defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Đầ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 defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
Me too.
I love Geeksforgeeks
6
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
7

    

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

NameError: name 's' is not defined
6

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 defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

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 defined
4    6

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à các biến toàn cầu nên có cùng tên.

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
NameError: name 's' is not defined
5

Đầ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 defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
11
NameError: name 's' is not defined
12

Ví dụ: Xác định và truy cập các biến toàn cầu

NameError: name 's' is not defined
0____11
NameError: name 's' is not defined
2

    

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
22
NameError: name 's' is not defined
12

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à các biến 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 defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
36
NameError: name 's' is not defined
12

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
6

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
47

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

NameError: name 's' is not defined
52

NameError: name 's' is not defined
4
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
5
NameError: name 's' is not defined
40
NameError: name 's' is not defined
12

Đầ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 Section
1Shwetanshu 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.


Sự khác biệt giữa biến cục bộ và biến toàn cầu là gì?

Sự khác biệt chính giữa các biến toàn cầu và cục bộ là các biến toàn cầu có thể được truy cập trên toàn cầu trong toàn bộ chương trình, trong khi các biến cục bộ chỉ có thể được truy cập trong hàm hoặc khối mà chúng được xác định.global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.

Một biến cục bộ trong Python là gì?

Trong Python hoặc bất kỳ ngôn ngữ lập trình nào khác, định nghĩa về các biến cục bộ vẫn giữ nguyên, đó là một biến được khai báo bên trong hàm được gọi là hàm cục bộ.Chúng ta có thể truy cập một biến cục bộ bên trong nhưng không nằm ngoài chức năng.A variable declared inside the function is called local function”. We can access a local variable inside but not outside the function.

Biến toàn cầu Python là gì?

Một biến toàn cầu trong Python thường được tuyên bố là đỉnh của chương trình.Nói cách khác, các biến được khai báo bên ngoài hàm được gọi là biến toàn cầu.Bạn có thể truy cập các biến toàn cầu trong Python cả bên trong và bên ngoài hàm.variables that are declared outside of a function are known as global variables. You can access global variables in Python both inside and outside the function.

Biến cục bộ và biến toàn cầu với ví dụ là gì?

Biến địa phương vs toàn cầu:.