Hướng dẫn get local variable from function python - lấy biến cục bộ từ hàm python

Không. Một chức năng không được chạy không có người dân địa phương; Nó chỉ là một chức năng. Hỏi làm thế nào để sửa đổi người dân địa phương của một chức năng khi nó không chạy giống như hỏi làm thế nào để sửa đổi đống của chương trình khi nó không chạy.

Bạn có thể sửa đổi hằng số, mặc dù, nếu bạn thực sự muốn.

def func():
    a = 10
    print a

co = func.func_code
modified_consts = list(co.co_consts)
for idx, val in enumerate(modified_consts):
    if modified_consts[idx] == 10: modified_consts[idx] = 15

modified_consts = tuple(modified_consts)

import types
modified_code = types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code, modified_consts, co.co_names, co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno, co.co_lnotab)
modified_func = types.FunctionType(modified_code, func.func_globals)
# 15:
modified_func()

Đó là một hack, bởi vì không có cách nào để biết hằng số nào trong co.co_consts là cái nào; Điều này sử dụng một giá trị sentinel để tìm ra nó. Tùy thuộc vào việc bạn có thể hạn chế các trường hợp sử dụng của bạn đủ, điều đó có thể là đủ.

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():

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
0
I love Geeksforgeeks
5
I love Geeksforgeeks
6

I love Geeksforgeeks
7

Đầ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():

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
0
I love Geeksforgeeks
5
NameError: name 's' is not defined
6
NameError: name 's' is not defined
7
NameError: name 's' is not defined
8

I love Geeksforgeeks
7

I love Geeksforgeeks
5
I love Geeksforgeeks
6

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():

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

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
7

I love Geeksforgeeks
5
NameError: name 's' is not defined
6
Me too.
I love Geeksforgeeks
5
NameError: name 's' is not defined
8

Đầ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():

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
0
I love Geeksforgeeks
5
I love Geeksforgeeks
6

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
7

I love Geeksforgeeks
5
I love Geeksforgeeks
6

Đầ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():

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

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

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

I love Geeksforgeeks
7

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():

I love Geeksforgeeks
0
I love Geeksforgeeks
1
I love Geeksforgeeks
2
I love Geeksforgeeks
3

Đầu ra

I love Geeksforgeeks
0
I love Geeksforgeeks
5
I love Geeksforgeeks
6

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.

I love Geeksforgeeks
0
I love Geeksforgeeks
5f():7

Biến toàn cầu

I love Geeksforgeeks
7

I love Geeksforgeeks
5
I love Geeksforgeeks
6

Đầ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

I love Geeksforgeeks
04
I love Geeksforgeeks
2
I love Geeksforgeeks
06

def f():

I love Geeksforgeeks
0
I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
12
I love Geeksforgeeks
13

def

I love Geeksforgeeks
15

I love Geeksforgeeks
0____1044
I love Geeksforgeeks
2
I love Geeksforgeeks
19

I love Geeksforgeeks
0
I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
23
I love Geeksforgeeks
13

def

I love Geeksforgeeks
26

I love Geeksforgeeks
0def1
I love Geeksforgeeks
29

I love Geeksforgeeks
0
I love Geeksforgeeks
04
I love Geeksforgeeks
2
I love Geeksforgeeks
33

I love Geeksforgeeks
0
I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
37
I love Geeksforgeeks
13

I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
41
I love Geeksforgeeks
13

I love Geeksforgeeks
7

I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
41
I love Geeksforgeeks
13

I love Geeksforgeeks
48

I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
41
I love Geeksforgeeks
13

I love Geeksforgeeks
53

I love Geeksforgeeks
5
NameError: name 's' is not defined
6
I love Geeksforgeeks
41
I love Geeksforgeeks
13

Đầu ra

global :  1
Inside f() :  1
global :  1
Inside g() :  2
global :  1
Inside h() :  3
global :  3

Bài viết này được đóng góp bởi Shwetanshu Rohatgi. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Xin vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc nếu bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.Shwetanshu 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 để bạn truy cập một biến từ một hàm trong Python?

Sử dụng cú pháp thuộc tính đối tượng để truy cập một biến bên ngoài hàm. Trong một chức năng có tên Func, sử dụng Func cú pháp. Biến = giá trị để lưu trữ giá trị trong biến như một thuộc tính của func. Để truy cập giá trị bên ngoài func, sử dụng func () để chạy func, sau đó sử dụng chức năng cú pháp_name.. In a function named func , use the syntax func. variable = value to store value in variable as an attribute of func . To access value outside of func , use func() to run func , then use the syntax function_name.

Làm thế nào để bạn tìm thấy các biến cục bộ trong Python?

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.

Một hàm có thể trả về một biến cục bộ?

Làm thế nào để trả về một biến cục bộ từ một hàm?Nhưng có một cách để truy cập các biến cục bộ của một hàm bằng cách sử dụng con trỏ, bằng cách tạo một biến con trỏ khác trỏ đến biến được trả về và trả về chính biến con trỏ.

Làm thế nào để bạn truy cập các biến cục bộ bên ngoài một hàm trong Python?

Sử dụng câu lệnh toàn cầu nếu bạn muốn gán một giá trị cho một tên được xác định bên ngoài hàm, thì bạn phải nói với Python rằng tên này không phải là cục bộ, nhưng nó là toàn cầu.Chúng tôi làm điều này bằng cách sử dụng tuyên bố toàn cầu.Không thể gán một giá trị cho một biến được xác định bên ngoài một hàm mà không có tuyên bố toàn cầu. If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.