Hướng dẫn how to pass local variables in python - cách truyền biến cục bộ 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à 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à 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.


Làm thế nào để Python chuyển các biến cho các chức năng?

Python chuyển các đối số bằng cách chuyển nhượng.Đó là, khi bạn gọi hàm Python, mỗi đối số hàm trở thành một biến mà giá trị truyền được gán ...
Nếu một đối tượng đại diện cho giá trị 2 đã tồn tại, thì nó được truy xuất.....
Bộ đếm tham chiếu của đối tượng này được tăng lên ..

Các quy tắc cho các biến cục bộ trong Python là gì?

Các quy tắc cho các biến địa phương và toàn cầu trong Python là gì?Trong Python, các biến chỉ được tham chiếu bên trong một hàm là toàn cầu.Nếu một biến được gán một giá trị ở bất cứ đâu trong cơ thể của hàm, thì nó được coi là cục bộ trừ khi được tuyên bố rõ ràng là toàn cầu.If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global.

Python có biến cục bộ không?

Python cũng có các biến địa phương.Các biến cục bộ được xác định bên trong một hàm và chúng chỉ tồn tại bên trong hàm đó.Các đối số được đưa ra cho một hàm là một ví dụ về các biến cục bộ.Bạn có thể đọc từ các biến toàn cầu, như chúng tôi đã làm ở trên trong hàm See_Message của chúng tôi.. Local variables are defined inside of a function, and they only exist inside that function. The arguments given to a function are an example of local variables. You can read from global variables, as we did above in our see_message function.