Python không cục bộ

con trăn. Phạm vi Python và Quy tắc LEGB. Resolved name in your code
Key learning qua video
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript

Mục lục bài viết

  • phạm vi hiểu biết
    • Name and range in Python
    • Phạm vi Python so với Không gian tên
  • Sử dụng Quy tắc LEGB cho phạm vi Python
    • Chức năng. Pham vi local
    • Các hàm lồng nhau. Phạm vi bảo trùm
    • mô-đun. Phạm vi toàn cầu
    • nội trang. Phạm vi tích hợp
  • Sửa đổi hành động của phạm vi Python
    • Toàn cầu tuyên bố
    • Tuyên bố phi địa phương
  • Sử dụng phạm vi bảo vệ làm đóng cửa
  • name up range when input
  • Khám phá các phạm vi Python bất thường
    • Phạm vi hiểu biết
    • Lỗi vi phạm ngoại lệ
    • Phạm vi thuộc tính lớp và phiên bản
  • Sử dụng các chức năng tổng hợp liên quan đến phạm vi
    • cấu hình []
    • người dân địa phương []
    • lọ []
    • thư mục []
  • Phần kết luận

Khái niệm phạm vi quy định cách các biến và tên được tra cứu trong mã của bạn. Nó xác định khả năng hiển thị của một biến trong mã. Phạm vi của tên hoặc biến phụ thuộc vào vị trí trong mã của bạn, nơi bạn tạo biến đó. Tầm niệm phạm vi Python thường được trình bày bằng cách sử dụng một quy tắc được gọi là quy tắc LEGB

Các chữ cái trong từ viết tắt LEGB là viết tắt của các phạm vi Cục bộ, Bao bọc, Toàn cầu và Tích hợp. Điều này tóm tắt không chỉ các mức phạm vi của Python mà còn cả trình tự các bước mà Python tuân theo khi phân giải tên trong một chương trình

Trong hướng dẫn này, bạn sẽ học

  • Phạm vi là gì và cách chúng hoạt động trong Python
  • Tại sao điều quan trọng là phải biết về phạm vi Python
  • Những điều mà quy tắc LEGB là gì và như thế nào Python sử dụng nó để quyết định tên
  • Cách sửa đổi hành vi tiêu chuẩn của phạm vi Python bằng cách sử dụng 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    6và
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    7
  • Có gì các công cụ phạm vi liên quan đến Python cung cấp và làm thế nào bạn có thể sử dụng chúng

Với ý tưởng này trong tay, bạn có thể tận dụng phạm vi Python để viết các dự án đáng tin cậy và dễ bảo trì hơn. Sử dụng phạm vi Python sẽ giúp bạn tránh hoặc giảm thiểu các lỗi liên quan đến xung đột tên cũng như việc sử dụng sai tên toàn cục trong các chương trình của bạn

Bạn sẽ tận dụng tối đa hướng dẫn này nếu bạn đã quen thuộc với các khái niệm Python trung gian như lớp , hàm , hàm bên trong , biến , ngoại lệ , hiểu , hàm tích dữ liệu và cấu trúc dữ liệu chuẩn

phạm vi hiểu biết

Trong lập trình, phạm vi của tên xác định khu vực của chương trình mà bạn có thể truy cập rõ ràng vào tên đó, chẳng hạn như biến, hàm, đối tượng, v. v. Tên sẽ chỉ hiển thị và có thể truy cập bằng mã trong phạm vi của nó. Một số ngôn ngữ lập trình tận dụng lợi thế của phạm vi để tránh xung đột tên và các hành vi không thể đoán trước. Thông thường nhất, bạn sẽ phân biệt hai phạm vi chung

  1. Phạm vi toàn cầu. Các tên mà bạn xác định trong phạm vi này có sẵn cho tất cả mã của bạn

  2. Pham vi local. Các tên mà bạn xác định trong phạm vi này chỉ có sẵn hoặc hiển thị đối với mã trong phạm vi

Phạm vi ra đời vì các ngôn ngữ lập trình ban đầu [như BASIC] chỉ có tên chung. Với loại tên này, bất kỳ phần nào của chương trình cũng có thể sửa đổi bất kỳ biến bất kỳ vào bất kỳ lúc nào, vì vậy việc duy trì và giải quyết lỗi chương trình lớn có thể trở thành một cơn ác mộng thực sự. Để làm việc với các tên chung, bạn cần ghi nhớ tất cả các mã cùng một lúc để biết giá trị của một tên cụ thể bất kỳ lúc nào. Đây là một tác dụng phụ quan trọng của công việc không có phạm vi

Một số ngôn ngữ như Python sử dụng phạm vi để tránh loại vấn đề này. Khi bạn sử dụng một ngôn ngữ phát triển khai phạm vi, không có cách nào để bạn truy cập vào tất cả các biến trong một chương trình tại tất cả các vị trí trong chương trình đó. Trong trường hợp này, khả năng truy cập vào tên cụ thể của bạn sẽ phụ thuộc vào nơi bạn đã xác định tên đó

Lưu ý. Bạn sẽ sử dụng tên thuật ngữ để tham chiếu đến các mã định danh của biến, hằng số, hàm, lớp hoặc bất kỳ đối tượng nào khác có thể được gán tên

Các tên trong chương trình của bạn sẽ có phạm vi của mã khối mà bạn xác định chúng. Khi bạn có thể truy cập giá trị của một tên cụ thể từ một nơi nào đó trong mã của mình, bạn sẽ nói rằng tên đó nằm trong phạm vi. Nếu bạn không thể truy cập vào tên, thì bạn sẽ nói rằng tên đó nằm ngoài phạm vi

Name and range in Python

Vì Python là một ngôn ngữ được định kiểu động, các biến trong Python xuất hiện khi bạn gán giá trị cho chúng lần đầu tiên. Mặt khác, hàm và lớp có sẵn sau khi bạn định nghĩa chúng bằng cách sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
8hoặc 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
9, tương ứng. Cuối cùng, các mô-đun mô-đun tồn tại sau khi bạn nhập chúng. Tóm lại, bạn có thể tạo tên Python thông qua một trong các thao tác sau

Hoạt độngTuyên bốBài tập

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
0Nhập hoạt động
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
1 hoặc là 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
2Định nghĩa hàm
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
3Định nghĩa đối số trong ngữ cảnh của hàm
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
4Định nghĩa lớp
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
5

Tất cả các hoạt động này được tạo hoặc, trong trường hợp được gán, cập nhật tên Python mới vì tất cả chúng đều gán tên cho một biến, hằng, hàm, lớp, cá thể, mô-đun hoặc đối tượng Python khác

Lưu ý. Có một sự khác biệt quan trọng giữa  hoạt động được chỉ định và  hoạt động tham khảo hoặc truy cập. Khi bạn tham khảo một tên, bạn chỉ đang truy xuất nội dung hoặc giá trị của nó. Khi bạn chỉ định một tên, bạn đang tạo ra tên đó hoặc sửa đổi nó

Python sử dụng vị trí của công việc được gán tên hoặc định nghĩa để liên kết nó với một phạm vi cụ thể. Nói cách khác, nơi bạn chỉ định hoặc xác định tên trong mã của mình sẽ xác định phạm vi vi phạm hoặc khả năng hiển thị của tên đó

Ví dụ. nếu bạn gán giá trị cho một tên bên trong một hàm, thì tên đó sẽ có phạm vi Python cục bộ. Ngược lại, nếu bạn gán giá trị cho một tên bên ngoài tất cả các chức năng — ví dụ, ở mức cao nhất của mô-đun — thì tên đó sẽ có phạm vi Python toàn cầu

Phạm vi Python so với Không gian tên

Trong Python, khái niệm phạm vi có liên quan chặt chẽ với khái niệm không gian tên. Như bạn đã học cho đến nay, phạm vi xác định vị trí của Python có thể nhìn thấy tên trong chương trình của bạn. Phạm vi Python được khai thác dưới dạng từ điển ánh xạ tên cho các đối tượng. Những từ điển này thường được gọi là không gian tên. Đây là những cơ chế mà Python sử dụng để lưu trữ tên. Chúng được lưu trữ trong một thuộc tính đặc biệt có tên là 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6

Tên ở mức cao nhất của mô-đun được lưu trữ trong không gian tên của mô-đun. Nói cách khác, chúng được lưu trữ trong ____46 thuộc tính của mô-đun. Please see after code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
2

Sau khi nhập ________ 48, bạn có thể sử dụng ________ 49 để kiểm tra các khóa của 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
50. Điều này trả về một danh sách với tất cả các tên được xác định ở mức cao nhất của mô-đun. Trong trường hợp này, bạn có thể nói rằng nó 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6chứa không gian tên 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8và là một đại diện cụ thể của phạm vi mô-đun

Lưu ý. Đầu ra của một số ví dụ trong hướng dẫn này đã được viết tắt [

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
53] để tiết kiệm dung lượng. Kết quả đầu ra có thể khác nhau dựa trên nền tảng, phiên bản Python của bạn hoặc thậm chí dựa trên thời gian bạn đang sử dụng phiên bản tương tác Python hiện tại của mình

Ví dụ thêm, giả sử dụng rằng bạn cần sử dụng tên 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54, được định nghĩa trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8. Nếu bạn biết cách 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6và không gian tên hoạt động trong Python, thì bạn có thể tham khảo 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54theo ít nhất hai cách khác nhau

  1. Use ký hiệu dấu chấm trên tên của mô-đun trong biểu mẫu
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    58
  2. Sử dụng  hoạt động đăng ký trên 
    >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    6 điển hình
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    50

Please see after code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6

Sau khi nhập xong, 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8bạn có thể truy cập 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54 bằng cách sử dụng ký hiệu dấu chấm trên 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8. Bạn cũng có thể truy cập 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54 bằng cách sử dụng tra cứu từ điển với từ khóa 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
55. Cả hai hành động đều trả về cùng một kết quả , 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
56

Lưu ý.

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54 là một chuỗi chỉ định lời nhắc chính của trình dịch Python.
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
54chỉ được xác định nếu thông tin giao dịch ở chế độ tương tác và giá trị ban đầu của nó là
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
56

Bất kể bạn sử dụng tên nào, giả sử như một biến hoặc một hàm tên, Python sẽ tìm kiếm qua các cấp độ khác nhau [hoặc không gian tên] để xác định xem tên đó có tồn tại hay không. Nếu tên tồn tại, thì bạn sẽ luôn nhận được lần xuất hiện đầu tiên của nó. Nếu không, bạn sẽ gặp lỗi. Bạn sẽ đề cập đến cơ chế tìm kiếm này trong phần tiếp theo

Sử dụng Quy tắc LEGB cho phạm vi Python

Python giải quyết các tên bằng cách sử dụng cái gọi là quy tắc LEGB, được đặt tên theo phạm vi Python cho các tên. Các chữ cái trong LEGB là viết tắt của Local, Enclosing, Global và built-in. Dưới đây là tổng quan nhanh về ý nghĩa của các thuật ngữ này

  • Phạm vi cục bộ [hoặc hàm] là khối mã hoặc nội dung của bất kỳ hàm nào hoặc ________ 360 biểu thức Python nào. Phạm vi Python này chứa các tên mà bạn xác định bên trong hàm. Những cái tên này sẽ chỉ hiển thị từ mã của hàm. Nó được tạo ra khi gọi hàm, không phải ở định nghĩa hàm, vì vậy bạn sẽ có nhiều phạm vi cục bộ khác nhau như lời gọi hàm. Điều này đúng ngay cả khi bạn gọi cùng một hàm nhiều lần hoặc gọi một cách đệ quy. Mỗi cuộc gọi sẽ dẫn đến một phạm vi bộ cục bộ mới được tạo

  • Phạm vi bao bọc [hoặc không địa phương] là một phạm vi đặc biệt chỉ tồn tại cho các hàm lồng nhau. Nếu phạm vi cục bộ là một hàm bên trong hoặc hàm lồng nhau , thì phạm vi bao quanh là phạm vi của hàm bên ngoài hoặc hàm bao quanh. Phạm vi này bao gồm các tên mà bạn xác định trong hàm bao quanh. Tên trong phạm vi bao quanh có thể nhìn thấy từ mã của các hàm bên trong và bao quanh

  • Phạm vi toàn cầu [hoặc mô-đun] là phạm vi cao nhất trong chương trình, tệp lệnh hoặc mô-đun Python. Phạm vi Python này bao gồm tất cả các tên mà bạn xác định ở mức cao nhất của chương trình hoặc mô-đun. Các tên trong phạm vi Python này có thể nhìn thấy từ mọi nơi trong mã của bạn

  • Phạm vi phân tích là một phạm vi Python đặc biệt được tạo hoặc tải xuống bất kỳ khi nào bạn chạy một tập lệnh hoặc mở một phiên bản tương tác. Phạm vi này bao gồm các tên như từ khóa , hàm, ngoại lệ và các thuộc tính khác được tích hợp trong Python. Các tên trong phạm vi Python này cũng có sẵn ở mọi nơi trong mã của bạn. Nó được Python tự động tải xuống khi bạn chạy chương trình hoặc tập lệnh

Quy tắc LEGB là một loại thủ tục lưu tên liên tục, xác định thứ tự mà Python tra cứu tên. Ví dụ. nếu bạn tham chiếu đến một tên định danh nhất, thì Python sẽ tìm tên tuần tự đó trong phạm vi bộ cục bộ, bao gồm, toàn cục và phân tích. Nếu tên tồn tại, thì bạn sẽ nhận được lần xuất hiện đầu tiên của nó. Nếu không, bạn sẽ gặp lỗi

Lưu ý. Lưu ý rằng phạm vi nội bộ và phạm vi bao quanh Python chỉ được tìm kiếm nếu bạn sử dụng tên bên trong một hàm [phạm vi cục bộ] hoặc một hàm lồng nhau hoặc hàm bên trong [phạm vi cục bộ và bao xung quanh]

Tóm lại, khi bạn sử dụng các hàm lồng nhau, các tên được giải quyết bằng cách kiểm tra phạm vi bộ cục bộ trước hoặc phạm vi bộ cục bộ của hàm trong cùng. Sau đó, Python xem xét tất cả các phạm vi bao gồm các hàm bên ngoài từ phạm vi trong cùng đến phạm vi ngoài cùng. Nếu không tìm thấy kết quả phù hợp nào, thì Python sẽ xem xét phạm vi toàn cầu và phạm vi phân tích. Nếu nó không thể tìm thấy tên, thì bạn sẽ gặp lỗi

Ở bất kỳ thời điểm xác định nào trong quá trình thực thi, bạn sẽ có tối đa phạm vi vi Python đang hoạt động — bộ cục bộ, bao gồm, toàn cầu và phân tích — tùy thuộc vào vị trí của bạn trong mã. Mặt khác, bạn sẽ luôn có ít nhất hai phạm vi hoạt động, đó là phạm vi toàn cầu và phạm vi phân tích. Hai phạm vi này sẽ luôn có sẵn cho bạn

Chức năng. Pham vi local

Phạm vi địa chỉ hoặc chức năng phạm vi là một phạm vi Python được tạo tại chức năng cuộc gọi. Mỗi khi bạn gọi một hàm, bạn cũng đang tạo một phạm vi bộ cục bộ mới. Mặt khác, bạn có thể coi mỗi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
8câu lệnh và 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
60 biểu thức như một bản thiết kế cho các phạm vi cục bộ mới. Bộ phạm vi cục bộ này sẽ tồn tại bất kể khi nào bạn gọi hàm trong tầm tay

Theo mặc định, các tham số và tên mà bạn gán bên trong một hàm chỉ tồn tại trong phạm vi hàm hoặc phạm vi cục bộ được liên kết với lệnh gọi hàm. Khi chức năng được trả về, bộ phạm vi cục bộ bị hủy và tên bị quên. Đây là cách nó hoạt động

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
9

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63là một hàm tính bình phương của một số định rõ nhất 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64,. Khi bạn gọi hàm, Python sẽ tạo một phạm vi bộ cục bộ bao gồm các tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64[một đối số] và 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
66[một bộ biến cục bộ]. Sau lần gọi đầu tiên đến 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63, 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64 giữ một giá trị 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
69và 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
66 giữ một giá trị 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
601. Lần thứ hai, các bộ tên cục bộ sẽ không nhớ các giá trị đã được lưu trữ trong lần đầu tiên hàm được gọi. Lưu ý rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64bây giờ giữ giá trị 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
603và 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
66 giữ 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
605

Lưu ý. Nếu bạn cố gắng truy cập 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
66hoặc 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64sau cuộc gọi hàm, thì bạn nhận được một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608, bởi vì chúng chỉ tồn tại ở phạm vi trong bộ vi cục bộ được tạo bởi lệnh gọi tới 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63. Bất kể khi nào bạn cố gắng truy cập vào một tên không được xác định trong bất kỳ phạm vi Python nào, bạn sẽ nhận được một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608. Thông báo lỗi sẽ bao gồm tên không thể tìm thấy

Vì bạn không thể truy cập tên bộ cục bộ từ các câu lệnh nằm ngoài hàm, các hàm khác nhau có thể xác định các đối tượng có cùng tên. Check ví dụ này

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8

Lưu ý rằng bạn xác định 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
611 bằng cách sử dụng cùng một biến và tham số mà bạn đã sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63. Tuy nhiên, vì 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
611không thể nhìn thấy các tên bên trong phạm vi vi bộ của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63và đảo ngược, cả hai hàm đều hoạt động như mong đợi mà không có bất kỳ xung đột tên nào

Bạn có thể tránh xung đột tên trong chương trình của mình bằng cách sử dụng đúng phạm vi Python local. Điều này cũng làm cho các chức năng trở nên khép kín hơn và tạo ra các đơn vị chương trình có thể bảo trì. Ngoài ra, vì bạn không thể thay đổi tên địa phương từ những nơi xa trong mã của mình, nên các chương trình của bạn sẽ dễ dàng khắc phục lỗi, đọc và sửa đổi hơn

Bạn có thể kiểm tra tên và tham số của một hàm bằng cách sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
615, đây là một thuộc tính chứa thông tin về mã bên trong của hàm. Vui lòng xem đoạn mã dưới đây

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
4

Trong ví dụ mã này, bạn kiểm tra 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
615 trên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63. Đây là một thuộc tính đặc biệt chứa thông tin về mã của một hàm Python. Trong trường hợp này, bạn thấy 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
618chứa một bộ chứa các tên mà bạn xác định bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63

Các hàm lồng nhau. Phạm vi bảo trùm

Phạm vi bao quanh hoặc không có địa chỉ được quan sát khi bạn kéo dài các hàm vào bên trong các hàm khác. Phạm vi bao quanh đã được nhập vào trong Python 2. 2. Nó có dạng phạm vi bộ cục bộ của bất kỳ phạm vi bộ cục bộ nào của hàm bao quanh. Các tên mà bạn xác định trong phạm vi Python bao quanh thường được gọi là tên phi địa phương. Vui lòng xem xét đoạn mã sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined

Khi bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620, bạn cũng đang tạo một bộ phạm vi cục bộ. Phạm vi cục bộ của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620, đồng thời, là phạm vi bao quanh của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622. Từ bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622, phạm vi này không phải là phạm vi toàn bộ cũng không phải là phạm vi bộ cục bộ. Đó là một phạm vi đặc biệt nằm giữa hai phạm vi đó và được gọi là phạm vi bao quanh

Lưu ý. Theo một nghĩa nào đó, 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622là một hàm tạm thời chỉ hoạt động trong quá trình thực thi chức năng bao quanh của nó 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620,. Lưu ý rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622chỉ hiển thị với mã trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620

Tất cả các tên mà bạn tạo trong phạm vi bao quanh đều có thể nhìn thấy từ bên trong 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622, ngoại trừ những tên được tạo sau khi bạn gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622. Đây là phiên bản mới của _
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
630nó cho thấy điểm này

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope

Khi bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620, mã sẽ chạy xuống điểm bạn gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622. Tuyên bố cuối cùng của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
622cố truy cập 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
634. Tại thời điểm này, 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
634 vẫn chưa được xác định, vì vậy Python đã tăng một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608vì nó không thể tìm thấy tên mà bạn đang cố gắng sử dụng

Cuối cùng nhưng không mất phần quan trọng, bạn không thể sửa đổi tên trong phạm vi bao quanh từ bên trong một hàm lồng nhau trừ khi bạn khai báo chúng như 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7trong hàm lồng nhau. Bạn sẽ trình bày cách sử dụng trong phần 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7 sau trong hướng dẫn này

mô-đun. Phạm vi toàn cầu

Kể từ thời điểm bạn bắt đầu một chương trình Python, bạn đã ở trong phạm vi Python toàn cầu. Bên trong, Python biến tập lệnh chính của chương trình của bạn thành một mô-đun được gọi là 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639 để giữ quá trình thực thi của chương trình chính. This module name is not to phạm vi toàn cầu chính của chương trình của bạn

Lưu ý. Trong Python, các khái niệm về phạm vi toàn cục và tên toàn cục được liên kết chặt chẽ với các mô-đun tệp. Ví dụ. nếu bạn xác định tên ở cấp cao nhất của bất kỳ mô-đun Python nào, thì tên đó được coi là toàn cục đối với mô-đun. Đó là lý do tại sao loại phạm vi này còn được gọi là mô-đun phạm vi

Nếu bạn đang làm việc trong một phiên bản tương tác với Python, thì bạn sẽ nhận thấy rằng

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
640 cũng là tên của mô-đun chính của nó. Để kiểm tra điều đó, hãy mở một phiên bản tương tác và nhập thông tin sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
5

Bất kể khi nào bạn chạy một chương trình Python hoặc một phiên bản tương tác như trong đoạn mã trên, thông tin dịch vụ sẽ thực thi mã trong mô-đun hoặc lệnh đóng vai trò trò chơi là điểm vào chương trình của bạn. Mô-đun hoặc tập lệnh này được tải xuống với tên đặc biệt 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639,. Từ thời điểm này, bạn có thể nói rằng phạm vi toàn cầu chính của bạn là phạm vi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639

Để kiểm tra các tên trong phạm vi toàn cầu chính của bạn, bạn có thể sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643. Nếu bạn gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mà không có đối số, thì bạn sẽ nhận được danh sách các tên nằm trong phạm vi toàn cầu hiện tại của bạn. Please see this code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
5

Khi bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mà không có đối số, bạn sẽ nhận được danh sách các tên có sẵn trong phạm vi Python toàn cầu chính của bạn. Lưu ý rằng nếu bạn chỉ định một tên mới [chẳng hạn như 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646ở đây] ở mức cao nhất của mô-đun [ 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639 ở đây], thì tên đó sẽ được thêm vào danh sách được trả về 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643

Lưu ý. You will trình bày 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643chi tiết hơn ở phần sau trong hướng dẫn này

Chỉ có một phạm vi Python toàn cầu cho mỗi lần thực hiện chương trình. Phạm vi này vẫn tồn tại cho đến khi chương trình kết thúc và tất cả các tên của nó bị lãng quên. Nếu không, lần sau khi bạn chạy chương trình, các tên sẽ ghi nhớ các giá trị của chúng từ lần chạy trước

Bạn có thể truy cập hoặc tham khảo giá trị của bất kỳ tên toàn cầu nào từ bất kỳ vị trí nào trong mã của bạn. Điều này bao gồm các hàm và các lớp. Dưới đây là một ví dụ làm rõ những điểm này

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6

Bên trong 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
650, bạn có thể tự truy cập hoặc tham khảo giá trị của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646. Điều này không ảnh hưởng đến tên toàn cầu của bạn 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646, nhưng nó cho bạn thấy rằng bạn 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646có thể được truy cập tự do từ bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
650. Mặt khác, bạn không thể gán các tên toàn cục bên trong các hàm trừ khi bạn tuyên bố rõ ràng chúng là tên chung bằng một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh mà bạn sẽ thấy ở phần sau

Bất kể khi nào bạn gán giá trị cho một tên trong Python, một trong hai điều kiện có thể xảy ra

  1. You tạo tên mới
  2. You cập nhật một tên hiện có

Hành vi cụ thể sẽ phụ thuộc vào phạm vi Python mà bạn đang gán tên. Nếu bạn cố gắng gán giá trị cho tên chung bên trong một hàm, thì bạn sẽ tạo tên đó trong phạm vi phạm vi cục bộ của hàm, phủ bóng hoặc ghi đè tên chung. Điều này có nghĩa là bạn sẽ không thể thay đổi hầu hết các biến đã được xác định bên ngoài hàm từ bên trong hàm

Nếu bạn tuân theo logic này, thì bạn sẽ nhận ra rằng đoạn mã sau sẽ không hoạt động như bạn có thể mong đợi

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
60

Bên trong 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
656, bạn cố gắng tăng biến toàn cục 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646,. Vì
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646không được khai báo 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
656, Python tạo một biến cục bộ mới có cùng tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646, bên trong hàm. Trong quá trình này, Python nhận ra rằng bạn đang cố gắng sử dụng bộ cục bộ 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646 trước nhiệm vụ đầu tiên của nó [ 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
663], vì vậy nó tạo ra một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
664

Đây là một ví dụ khác

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
61

Bạn có thể mong đợi có thể có toàn bộ yêu cầu 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646 và có thể cập nhật 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646sau, nhưng một lần nữa bạn lại nhận được 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
664. Điều xảy ra ở đây là khi bạn chạy phần thân của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
650, Python quyết định rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646là một biến cục bộ vì nó được gán trong phạm vi hàm. Đây không phải là một lỗi, mà là một sự lựa chọn thiết kế. Python giả định rằng các tên được gán trong phần thân của một hàm là bộ tổng thể của hàm đó

Lưu ý. Tên chung có thể được cập nhật hoặc sửa đổi từ bất kỳ nơi nào trong phạm vi Python toàn cầu của bạn. Ngoài ra, 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh có thể được sử dụng để sửa đổi tên chung từ hầu hết bất kỳ vị trí nào trong mã của bạn, như bạn sẽ thấy trong The 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6Statement

Việc sửa đổi tên toàn cục thường được coi là phương pháp lập trình không tốt vì nó có thể dẫn đến mã

  • Error khó giải quyết. Hầu hết mọi câu lệnh trong chương trình đều có thể thay đổi giá trị của tên toàn cục
  • Khó hiểu. Bạn cần biết tất cả các lệnh truy cập và sửa đổi tên chung
  • Unable to use back. Mã phụ thuộc vào tên chung dành riêng cho một chương trình cụ thể

Thực hiện chương trình tốt khuyến khích bạn nên sử dụng tên bộ cục bộ thay vì tên cục bộ. Dưới đây là một số mẹo

  • Viết các hàm độc lập dựa vào tên cục bộ thay vì tên chung
  • Cố gắng sử dụng các tên đối tượng duy nhất, bất kể bạn đang ở trong phạm vi nào
  • Cannot sửa đổi tên toàn cầu trong suốt chương trình của bạn
  • Range sửa đổi tên mô-đun chéo
  • Sử dụng tên chung làm hằng số không thay đổi trong quá trình thực thi chương trình của bạn

Cho đến thời điểm này, bạn đã bao gồm ba phạm vi Python. Xem ví dụ sau để biết bản tóm tắt về vị trí của chúng trong mã của bạn và cách Python tra cứu tên thông qua chúng

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
62

Khi bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
620, bạn sẽ được 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
601in trên màn hình. Nhưng làm thế nào để Python tra cứu tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
674trong trường hợp này?

  1. Bên trong 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    622. Đây là phạm vi bộ cục bộ, nhưng 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    674không tồn tại ở đó
  2. Bên trong 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    620. Đây là phạm vi bao quanh, nhưng 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    674 cũng không được xác định ở đó
  3. Trong phạm vi mô-đun. Đây là phạm vi chung và bạn tìm thấy 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    674ở đó, vì vậy bạn có thể vào 
    >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    674ra màn hình

Nếu 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
674không được xác định bên trong phạm vi toàn cục, thì Python sẽ tiếp tục tìm kiếm bằng cách xem phạm vi được tích hợp sẵn. Đây là phần cuối cùng của quy tắc LEGB, như bạn sẽ thấy trong phần tiếp theo

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683. Phạm vi tích hợp

Các tích hợp trong phạm vi là một phạm vi Python đặc biệt được thực hiện như một mô-đun thư viện chuẩn được đặt tên 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683 bằng Python 3. x. Tất cả các đối tượng tích hợp sẵn của Python đều nằm trong mô-đun này. Chúng được tự động tải vào phạm vi hợp lý khi bạn chạy trình thông dịch Python. Tìm kiếm 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683trong Python cuối cùng trong quá trình tìm kiếm LEGB của nó, vì vậy bạn đã nhận được tất cả các tên mà nó được định nghĩa là miễn phí. Điều này có nghĩa là bạn có thể sử dụng chúng mà không cần nhập bất kỳ mô-đun nào

Lưu ý rằng các tên trong 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683 luôn được tải vào phạm vi Python toàn cầu của bạn với tên đặc biệt 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
687, vì bạn có thể thấy trong đoạn mã sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
63

Trong phần đầu của lệnh gọi đầu tiên 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643, bạn có thể thấy điều đó 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
687luôn xuất hiện trong phạm vi Python toàn cầu. Nếu bạn ________ 2687 kiểm tra theo cách sử dụng ________ 2643, thì bạn sẽ nhận được toàn bộ danh sách các tên được tích hợp sẵn trong Python

Phạm vi tích hợp mang đến hơn 150 tên cho phạm vi Python toàn cầu hiện tại của bạn. Ví dụ. trong Python 3. 8, bạn có thể biết số lượng tên chính xác như sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
64

Với cuộc gọi đến 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
692, bạn sẽ nhận được số lượng các mục được 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
693 trả về 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643. Điều này trả về 152 tên bao gồm các ngoại lệ, hàm, kiểu, thuộc tính đặc biệt và các đối tượng tích hợp sẵn khác của Python

Mặc dù bạn có thể truy cập miễn phí tất cả các đối tượng tích hợp sẵn trong Python này [mà không cần nhập bất kỳ thứ gì], bạn cũng có thể nhập 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683và truy cập tên một cách rõ ràng bằng ký hiệu dấu chấm. Đây là cách nó hoạt động

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
65

Bạn có thể nhập 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683như bạn làm với bất kỳ mô-đun Python nào khác. Từ thời điểm này, bạn có thể truy cập tất cả các tên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683 bằng cách sử dụng tra cứu thuộc tính có dấu chấm hoặc các tên đủ điều kiện. Điều này có thể khá hữu ích nếu bạn muốn chắc chắn rằng bạn sẽ không xung đột tên nếu bất kỳ tên chung nào của bạn ghi đè lên bất kỳ tên xây dựng nào

Bạn có thể ghi lại hoặc xác định lại bất kỳ tên hợp tác nào trong phạm vi toàn cầu của mình. Nếu bạn làm như vậy, hãy nhớ rằng điều này sẽ ảnh hưởng đến tất cả mã của bạn. Please view ví dụ sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
66

Nếu bạn ghi đè hoặc gán lại 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
698, thì bản gốc tích hợp sẵn 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
699 sẽ ảnh hưởng đến toàn bộ mã của bạn. Bây giờ, giả sử rằng bạn cần gọi tên gốc 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
699và bạn quên rằng bạn chỉ định lại tên. Trong trường hợp này, khi bạn gọi lại 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
699, bạn sẽ nhận được 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
902vì 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
698bây giờ giữ một tham chiếu đến một số nguyên, không thể gọi được

Lưu ý. Việc ghi lại hoặc xác định lại các tên có sẵn trong phạm vi toàn cầu của bạn một cách vô tình hoặc vô ý có thể là nguồn phát sinh các lỗi nguy hiểm và khó tìm. Tốt hơn hết là bạn nên thử và tránh kiểu tập luyện này

Nếu bạn đang thử nghiệm với một số mã và bạn vô tình gán lại tên phân tích theo lời nhắc tương tác, thì bạn có thể khởi động lại phiên bản của mình hoặc chạy 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
904 để xóa định nghĩa lại khỏi phạm vi Python toàn bộ yêu cầu của bạn. Bằng cách này, bạn đang khôi phục tên gốc trong phạm vi được tích sẵn. Nếu bạn xem lại ví dụ về 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
699, thì bạn có thể làm như sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
67

Khi bạn xóa 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
698tên tùy chỉnh , bạn đang xóa tên khỏi phạm vi toàn cầu của mình. Điều này cho phép bạn truy cập lại bản gốc 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
699trong phạm vi tích hợp

Để giải quyết vấn đề này, bạn có thể xác định rõ ràng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683 và sau đó sử dụng các tên đủ điều kiện, chẳng hạn như trong đoạn mã sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
68

Khi bạn xác định rõ ràng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683, bạn có sẵn tên mô-đun trong phạm vi Python toàn cầu của mình. Từ thời điểm này, bạn có thể sử dụng các tên đủ điều kiện để có được những tên bạn cần rõ ràng một cách rõ ràng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
683, giống như bạn đã làm với 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
911trong ví dụ trên

Như một bản tóm tắt nhanh, một số hàm ý của phạm vi Python được hiển thị trong bảng sau

Hoạt độngMã toàn cầuMã địa phươngMã chức năng lồng ghép nhauTruy cập hoặc tham chiếu các tên tồn tại trong phạm vi toàn cầu Đúng chính xácSửa đổi hoặc cập nhật các tên tồn tại trong phạm vi toàn cầuKhông đúng [ngoại trừ khi được khai báo

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6]Không [ngoại trừ khi được khai báo

Ngoài ra, mã trong các phạm vi khác nhau có thể sử dụng cùng một tên cho các đối tượng khác nhau. Bằng cách này, bạn có thể sử dụng một biến cục bộ có tên 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
915và cả một biến cục bộ có cùng tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
915,. Tuy nhiên, đây được coi là một hành động thiết lập sự suy nhược

Sửa đổi hành động của phạm vi Python

Cho đến nay, bạn đã biết cách phạm vi hoạt động của Python và cách chúng giới hạn khả năng hiển thị của các biến, hàm, lớp và các đối tượng Python khác đối với một số phần định dạng mã của bạn. Bây giờ bạn biết rằng bạn có thể truy cập hoặc tham khảo các tên chung từ bất kỳ vị trí nào trong mã của mình, nhưng chúng có thể được sửa đổi hoặc cập nhật từ phạm vi Python toàn cầu

Bạn biết rằng bạn chỉ có thể truy cập tên bộ cục bộ từ bên trong phạm vi Python cục bộ mà chúng được tạo trong hoặc từ bên trong một hàm lồng nhau, nhưng bạn không thể truy cập chúng từ phạm vi Python toàn cục hoặc . Ngoài ra, bạn đã biết rằng các tên phi địa phương có thể được truy cập từ bên trong các hàm lồng nhau, nhưng chúng không thể được sửa đổi hoặc cập nhật từ đó

Mặc dù phạm vi Python tuân theo các quy tắc chung này theo mặc định, vẫn có nhiều cách để sửa đổi hành vi tiêu chuẩn này. Python cung cấp hai từ khóa cho phép bạn sửa đổi nội dung của tên toàn cục và tên phi địa phương. Hai từ khóa này là

  1. >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    6
  2. >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    7

Trong hai phần tiếp theo, bạn sẽ trình bày cách sử dụng các từ khóa Python này để sửa đổi hành vi tiêu chuẩn của phạm vi Python

các 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6Tuyên Bố

Bạn đã biết rằng khi bạn cố gắng gán một giá trị cho một tên toàn cục bên trong một hàm, bạn sẽ tạo một tên cục bộ mới trong phạm vi hàm. Để sửa đổi hành vi này, bạn có thể sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh. Với câu lệnh này, bạn có thể xác định danh sách các tên sẽ được coi là tên chung

Bao gồm câu lệnh bao gồm 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6từ khóa được theo sau bởi một hoặc nhiều tên được phân tách bằng dấu phẩy. Bạn cũng có thể sử dụng nhiều 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh với một tên [hoặc một danh sách tên]. Tất cả các tên mà bạn liệt kê trong một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh sẽ được ánh xạ tới phạm vi chung hoặc phạm vi mô-đun mà bạn xác định chúng

Đây là một ví dụ mà bạn cố gắng cập nhật một biến toàn cục từ bên trong một hàm

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
69

Khi bạn cố gắng gán 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925, Python sẽ giả định rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924là bộ cục bộ 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925và tăng một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
664bởi vì bạn đang cố gắng truy cập vào một tên chưa được xác định

Nếu bạn muốn mã hóa hoạt động này theo cách bạn mong đợi ở đây, thì bạn có thể sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh như sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
90

Trong phiên bản mới này 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925, bạn thêm câu lệnh 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
931vào phần nội dung của hàm ngay trước khi bạn cố gắng thay đổi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924. Với sự thay đổi nhỏ này, bạn đang ánh xạ tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924trong phạm vi chức năng với cùng một tên trong phạm vi toàn cục hoặc mô-đun. Từ thời điểm này, bạn có thể tự động sửa đổi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925. Tất cả các thay đổi sẽ phản ánh trong biến toàn cục

Với câu lệnh 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
931, bạn đang yêu cầu Python tìm tên trong phạm vi toàn cầu 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924. Bằng cách này, biểu thức 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
938không tạo tên mới trong phạm vi hàm, nhưng cập nhật tên đó trong phạm vi toàn cục

Lưu ý. Nhìn chung, việc sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6được coi là hành vi xấu. Nếu bạn thấy mình đang sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6 để giải quyết các sự cố như ở trên, thì hãy dừng lại và suy nghĩ xem có cách nào tốt hơn để viết mã của bạn không

Ví dụ. bạn có thể cố gắng viết một hàm độc lập dựa trên tên cục bộ thay vì tên cục bộ toàn cục như sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
91

Việc phát triển khai báo này của 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925định nghĩa 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924là một tham số và trả về giá trị của nó được tăng thêm theo 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
943 đơn vị mỗi khi hàm được gọi. Bằng cách này, kết quả là 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
925 phụ thuộc vào cách 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
924bạn sử dụng đầu vào chứ không phụ thuộc vào những thay đổi mà các hàm khác [hoặc các đoạn mã] có thể thực hiện trên các biến toàn cục 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
946,

Bạn có thể sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh để tạo tên toàn cục dẻo bằng cách khai báo chúng bên trong một hàm. Please see after code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
92

Khi bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
948, bạn cũng đang tạo một biến toàn cục được gọi là 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
949. Lưu ý rằng sau khi gọi hàm, tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
949có sẵn trong phạm vi Python toàn cầu. Nếu bạn kiểm tra không gian tên chung bằng cách sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643, thì bạn sẽ thấy vùng đó 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
949xuất hiện cuối cùng trong danh sách

Lưu ý. Mặc dù bạn có thể sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh để tạo tên toàn cục, nhưng đây có thể là một phương pháp nguy hiểm có thể dẫn đến mã lỗi. Vì vậy, tốt nhất nên tránh những thứ như thế này trong mã của bạn

Ví dụ. giả sử bạn đang cố gắng truy cập vào một trong những tên lười biếng đó và vì lý do nào đó, mã của bạn chưa gọi hàm tạo tên đó. Trong trường hợp này, bạn sẽ nhận được một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608 và chương trình của bạn sẽ gặp sự cố

Cuối cùng, cần lưu ý rằng bạn có thể sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6từ bên trong bất kỳ hàm hoặc hàm lồng nhau nào và các tên được liệt kê sẽ luôn được ánh xạ với các tên trong phạm vi Python toàn cầu

Cũng lưu ý rằng, mặc dù việc sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh ở mức cao nhất của mô-đun là hợp pháp, nó không có ý nghĩa nhiều vì bất kỳ tên nào được gán trong phạm vi toàn cục đã là tên toàn cục theo định nghĩa . Please see after code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
93

Việc sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6câu lệnh như 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
958không thay đổi bất kỳ điều gì trong phạm vi toàn cầu hiện tại của bạn, vì bạn có thể thấy trong đầu ra của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643. Biến 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
960là một biến toàn cục cho dù bạn có sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6hay không

các 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7Tuyên Bố

Tương tự như tên toàn cục, tên phi địa phương có thể truy cập được từ các hàm bên trong, nhưng không được gán hoặc cập nhật. Nếu bạn muốn sửa đổi chúng, thì bạn cần sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7câu lệnh. Với một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7câu lệnh, bạn có thể xác định danh sách các tên sẽ được coi là phi địa phương

Câu 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7lệnh bao gồm 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7từ khóa được theo sau bởi một hoặc nhiều tên được phân tách bằng dấu phẩy. Những tên này sẽ đề cập đến những tên giống nhau trong phạm vi Python bao quanh. Ví dụ sau đây cho thấy cách bạn có thể sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7 để sửa đổi một biến đã được xác định trong phạm vi bao quanh hoặc không thuộc phạm vi địa phương

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
94

Với câu lệnh 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
968, bạn nói với Python rằng bạn sẽ sửa đổi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
970. Sau đó, bạn tăng dần 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646 bằng cách sử dụng một hoạt động chỉ định tăng cường sức mạnh. Thay đổi này bị phản ánh trong tên phi địa phương 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646, hiện có giá trị là 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
973

Không giống như 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6, bạn không thể sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7bên ngoài một hàm lồng nhau hoặc hàm kèm theo. Nói chính xác hơn, bạn không thể sử dụng một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7câu lệnh trong phạm vi toàn cục hoặc phạm vi bộ. This is a ví dụ

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
95

Ở đây, lần đầu tiên bạn cố gắng sử dụng một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7câu lệnh trong phạm vi Python toàn cầu. Vì 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7chỉ hoạt động bên trong một hàm bên trong hoặc trong một hàm lồng nhau, bạn sẽ nhận được thông báo 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
979rằng bạn không thể sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7trong phạm vi mô-đun. Lưu ý rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7nó cũng không hoạt động trong phạm vi cục bộ

Lưu ý. Để biết thêm thông tin chi tiết về 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7tuyên bố, hãy xem PEP 3104 - Quyền truy cập vào tên trong phạm vi bên ngoài

Ngược lại 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
6, bạn không thể sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7 để tạo các tên không địa phương lười biếng. Các tên phải tồn tại trong phạm vi Python đi kèm nếu bạn muốn sử dụng chúng làm tên phi địa phương. Điều này có nghĩa là bạn không thể tạo tên phi địa phương bằng cách khai báo chúng trong một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7câu lệnh trong một chương trình lồng nhau. Vui lòng xem ví dụ sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
96

Trong ví dụ này, khi bạn cố gắng xác định một tên phi địa phương bằng cách sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
986, Python ngay lập tức đưa ra một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
979 bởi vì 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
988không tồn tại trong phạm vi bao gồm 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
970

Sử dụng phạm vi bảo vệ làm đóng cửa

Closures là một trường hợp sử dụng đặc biệt của phạm vi Python bao quanh. Khi bạn xử lý một hàm lồng nhau dưới dạng dữ liệu, các câu lệnh được tạo nên hàm đó được đóng gói cùng với môi trường mà chúng thực thi. Kết quả đối tượng được gọi là một bao đóng. Nói cách khác, bao đóng là một hàm bên trong hoặc hàm lồng nhau mang thông tin về phạm vi bao quanh của nó, ngay cả khi phạm vi này đã hoàn thành việc thực thi

Lưu ý. Bạn có thể gọi loại hàm này là một nhà máy , một hàm nhà máy hoặc — chính xác hơn — một nhà máy cũng đóng để chỉ định rằng hàm xây dựng và trả về các bao đóng [một hàm bên trong], thay vì các hàm

Đóng cửa cung cấp một cách để giữ lại trạng thái thông tin giữa các lần gọi hàm. Điều này có thể hữu ích khi bạn muốn viết mã dựa trên khái niệm đánh giá chậm hoặc chậm rãi. Vui lòng xem đoạn mã sau để biết ví dụ về cách hoạt động của các bao đóng và cách bạn có thể tận dụng chúng trong Python

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
97

Hàm đóng cửa nhà máy của bạn 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
990nhận một đối số được gọi là 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
991. Bạn có thể sử dụng chức năng này để xây dựng các bao đóng chạy các chức năng năng lượng khác nhau. Điều này hoạt động vì mỗi cuộc gọi
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
990 đã nhận được tập tin thông tin trạng thái của riêng nó. Nói cách khác, nó có giá trị 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
991

Lưu ý. Các biến như ________ 3991 được gọi là biến tự do. Chúng là các biến được sử dụng trong một khối mã nhưng không được định nghĩa ở đó. Các biến tự do là cơ chế mà các bao đóng sử dụng để giữ lại trạng thái thông tin giữa các cuộc gọi

Trong ví dụ trên, hàm bên trong 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
995 lần đầu tiên được gán cho 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
996. Trong trường hợp này, hàm ghi nhớ rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
991 bằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
998. Trong ví dụ thứ hai, bạn gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
990 bằng cách sử dụng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
800làm đối số. Bằng cách này, 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
801nắm giữ một hàm đối tượng có thể nhớ rằng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
991là 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
800. Lưu ý rằng bạn có thể tự động sử dụng lại 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
996và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
801vì họ không quên thông tin trạng thái tương ứng của họ

Đối với ví dụ cuối cùng về cách sử dụng các bao đóng, giả sử rằng bạn cần tính giá trị trung bình của một số dữ liệu mẫu. Bạn thu thập dữ liệu thông qua một chuỗi các phép đo liên tiếp của thông số bạn đang phân tích. Trong trường hợp này, bạn có thể sử dụng một cửa máy đóng cửa để tạo ra một kết thúc ghi nhớ các phép đo trước đó trong mẫu. Please see after code

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
98

Bao đóng mà bạn tạo trong đoạn mã ghi trên trạng thái ghi nhớ của thông tin trạng thái 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
806 giữ lại các lần gọi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
807. Bằng cách này, bạn có thể giải quyết vấn đề theo cách thanh lịch và theo cách Pythonic

Lưu ý rằng nếu luồng dữ liệu của bạn quá lớn, thì chức năng này có thể trở thành một vấn đề về việc sử dụng bộ nhớ. Đó là bởi vì mỗi lệnh gọi tới 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
807, 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
806 sẽ bao gồm một danh sách giá trị ngày càng lớn hơn. Vui lòng xem đoạn mã sau để biết cách phát triển khai thay thế bằng cách sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
7

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
99

Mặc dù giải pháp này dài dòng hơn, nhưng bạn không có danh sách phát triển liên tục nữa. Bây giờ bạn có một giá trị duy nhất cho 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
811và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
812. Việc triển khai này mang lại hiệu quả hơn rất nhiều về mức tiêu thụ bộ nhớ so với giải pháp trước đó

Cuối cùng, bạn có thể tìm thấy một số ví dụ về việc sử dụng bao đóng trong thư viện Python chuẩn. Ví dụ.

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
813cung cấp một hàm có tên 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
814sử dụng kỹ thuật đóng để tạo các đối tượng hàm mới có thể được gọi bằng cách sử dụng các đối số đã được xác định trước đó. This is a ví dụ

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
80

Bạn sử dụng 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
815 để xây dựng một hàm đối tượng ghi nhớ thông tin trạng thái, ở đâu 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
816. Sau đó, bạn gọi đối tượng này để thực hiện các nguồn hoạt động và nhận được kết quả cuối cùng

điền tên vào phạm vi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
817

Khi bạn viết một chương trình Python, bạn thường tổ chức mã hóa thành một số mô-đun. Để chương trình của bạn hoạt động, bạn cần đặt tên trong các mô-đun, riêng biệt tên đó vào mô-đun của mình 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639. Để làm điều đó, bạn cần phải 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
817mô-đun hoặc đặt tên rõ ràng. Đây là cách duy nhất bạn có thể sử dụng những tên đó trong phạm vi Python toàn cầu chính của mình

Vui lòng xem đoạn mã sau để biết ví dụ về điều gì sẽ xảy ra khi bạn nhập một số mô-đun và tên tiêu chuẩn

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
81

Trước tiên, bạn hãy nhập 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
821từ thư viện chuẩn Python. Bằng cách gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mà không có đối số, bạn có thể thấy rằng các mô-đun này hiện có sẵn cho bạn dưới dạng tên trong phạm vi toàn cầu hiện tại của bạn. Bằng cách này, bạn có thể sử dụng ký hiệu dấu chấm để truy cập vào các tên đã được xác định trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
821

Lưu ý. Nếu bạn muốn tìm hiểu sâu hơn về cách nhập hoạt động trong Python, hãy xem Nhập tuyệt đối với Nhập đối tương trong Python

Trong 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
817 hoạt động mới nhất , bạn sử dụng biểu mẫu 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
826. Bằng cách này, bạn có thể sử dụng tên đã nhập trực tiếp trong mã của mình. Nói cách khác, bạn không cần phải sử dụng ký hiệu dấu chấm một cách rõ ràng

Khám phá các phạm vi Python bất thường

Bạn sẽ tìm thấy một số cấu trúc Python trong đó độ phân giải tên như không phù hợp với quy tắc LEGB cho phạm vi Python. Bao gồm cấu trúc này

  • Sự hiểu biết
  • Khối ngoại lệ
  • Các lớp và phiên bản

Trong một vài phần tiếp theo, bạn sẽ trình bày cách thức hoạt động của phạm vi Python trên ba cấu trúc này. Với ý kiến ​​này, bạn sẽ có thể tránh được các lỗi nhỏ liên quan đến việc sử dụng tên trong các loại cấu trúc Python này

Phạm vi hiểu biết

Đầu tiên, cấu trúc cấu trúc bạn sẽ đề cập đến là phần hiểu. Đọc hiểu là một cách thu gọn để xử lý tất cả hoặc một phần của các phần tử trong một tập hợp hoặc trình tự. Bạn có thể sử dụng phần hiểu để tạo danh sách , từ điển và bộ

Phần hiểu bao gồm một cặp dấu trích lược [ 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
827] hoặc dấu ngoặc nhọn [ 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
828] có chứa một biểu thức, theo sau là một hoặc nhiều 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
829mệnh đề và sau đó là số không hoặc một 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
830mệnh đề cho mỗi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
829mệnh đề

Các điều khoản 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
829 trong một sự hiểu biết làm công việc giống như một hệ thống truyền 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
829 vòng lặp. Xoay vòng trong phần hiểu biết là bộ cục bộ của cấu trúc. Kiểm tra mã sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
82

Khi bạn chạy tính năng hiểu danh sách , biến 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
834 sẽ bị quên và bạn không thể truy cập giá trị của nó nữa. Không chắc rằng bạn cần sử dụng biến này bên ngoài phần hiểu, nhưng bất kể, Python chắc chắn rằng giá trị của nó sẽ không còn nữa khi quá trình hiểu kết thúc

Lưu ý rằng điều này chỉ áp dụng cho phần hiểu. Khi nói đến 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
829 Vòng lặp thông thường , vòng lặp giữ giá trị cuối cùng là vòng lặp xử lý

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
83

Bạn có thể tự truy cập vòng lặp _

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
834khi vòng lặp kết thúc. Tại đây, vòng lặp giữ giá trị cuối cùng được xử lý bởi vòng lặp, 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
837trong ví dụ này

Lỗi vi phạm ngoại lệ

Một trường hợp không điển hình khác của phạm vi Python mà bạn sẽ gặp là trường hợp của biến ngoại lệ. Biến ngoại lệ là một biến chứa tham chiếu đến ngoại lệ được đưa ra bởi một ______5838câu lệnh. Trong Python 3. x, các biến như vậy là bộ cục bộ của 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839khối và bị quên khi kết thúc khối. Kiểm tra mã sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
84

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
840 giữ một tham chiếu ngoại lệ được nêu ra bởi
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
838mệnh đề. Bạn chỉ có thể sử dụng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
840bên trong mã khối của 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839mệnh đề. Bằng cách này, bạn có thể nói rằng phạm vi Python cho biến ngoại lệ là bộ cục bộ của 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839khối mã. Cũng lưu ý rằng nếu bạn cố gắng truy cập 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
840từ bên ngoài 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839khối, thì bạn sẽ nhận được 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608. Đó là bởi vì khi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839khối kết thúc, tên không tồn tại nữa

Để giải quyết vấn đề này, bạn có thể xác định một biến phụ từ câu lệnh

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
838 và sau đó gán ngoại lệ cho biến bên trong
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
839khối. Check ví dụ sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
85

Bạn sử dụng 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
851như một biến phụ để duy trì tham chiếu đến ngoại lệ được 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
838mệnh đề nêu ra. Điều này có thể hữu ích khi bạn cần làm điều gì đó với các đối tượng ngoại lệ sau khi mã hóa khối hoàn tất. Lưu ý rằng nếu không có ngoại lệ nào được nêu ra, thì 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
851 còn 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
854

Phạm vi thuộc tính lớp và phiên bản

Khi bạn xác định một lớp, bạn đang tạo một phạm vi bộ cục bộ mới của Python. Các tên chỉ được xác định ở cấp cao nhất của lớp nằm trong phạm vi của bộ cục bộ này. Các tên mà bạn chỉ định bên trong một 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
9câu lệnh không xung đột với các tên ở nơi khác. Bạn có thể nói rằng những cái tên này tuân theo quy tắc LEGB, trong đó khối lớp đại diện cho mức L

No same as functions, range of local set of the layer are not created at the call point, which is at time point thuc thi. Mỗi lớp đối tượng có 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6 thuộc tính riêng chứa phạm vi lớp hoặc không gian tên nơi tất cả các thuộc tính của lớp thuộc tính tại. Kiểm tra mã này

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
86

Khi bạn kiểm tra các phím của 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6bạn, bạn sẽ thấy nó 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
858có trong danh sách giống với những cái tên đặc biệt khác. Từ điển này đại diện cho phạm vi bộ cục bộ của lớp. Các tên trong phạm vi này được hiển thị cho tất cả các phiên bản của lớp và với lớp chính đó

Để có quyền truy cập vào một thuộc tính lớp từ bên ngoài lớp, bạn cần sử dụng ký hiệu dấu chấm như sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
87

Bên trong phạm vi bộ cục bộ của 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859, bạn có thể truy cập trực tiếp vào các thuộc tính của lớp, giống như bạn đã làm trong câu lệnh 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
860. Để truy cập bất kỳ thuộc tính lớp nào sau khi khối mã của lớp được thực thi, bạn cần sử dụng ký hiệu dấu chấm hoặc tham chiếu thuộc tính như bạn đã làm với 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
861. Nếu không, bạn sẽ nhận được một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
608, do thuộc tính 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
858là bộ cục bộ của khối lớp

Mặt khác, nếu bạn cố gắng truy cập một thuộc tính không được xác định bên trong một lớp, thì bạn sẽ nhận được 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
864. Check ví dụ sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
88

Trong ví dụ này, bạn cố gắng truy cập thuộc tính

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
865. Vì thuộc tính này không tồn tại trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859 nên bạn sẽ nhận được thông báo 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
864rằng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859không có thuộc tính được đặt tên 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
865

Bạn cũng có thể truy cập bất kỳ thuộc tính lớp nào bằng cách sử dụng một thể hiện của lớp như sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
89

Khi bạn có thể, bạn có thể truy cập các thuộc tính của lớp bằng cách sử dụng ký hiệu dấu chấm, như bạn đã làm ở đây với 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
870. Các thuộc tính của lớp dành riêng cho các đối tượng của lớp, nhưng bạn có thể truy cập chúng từ bất kỳ trường hợp nào của lớp. Cần lưu ý rằng các thuộc tính của lớp là chung cho tất cả các trường hợp của một lớp. Nếu bạn sửa đổi một thuộc tính của lớp, thì những thay đổi sẽ hiển thị trong tất cả các trường hợp của lớp

Lưu ý. Hãy nghĩ về ký hiệu dấu chấm như thể bạn đang nói với Python, “Vui lòng tìm thuộc tính được gọi là 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
858trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
872. Nếu tìm được thì trả lại cho tôi”

Bất kể khi nào bạn gọi một lớp, bạn đang tạo ra một thể hiện mới của lớp đó. Các cá thể có 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6 thuộc tính riêng của chúng bao gồm các tên trong phạm vi phạm vi cục bộ hoặc không gian tên của các cá thể. Những tên này thường được gọi là thuộc tính cá thể và là địa phương và cụ thể cho từng cá thể. Điều này có nghĩa là nếu bạn sửa đổi một phiên bản thuộc tính, thì những thay đổi sẽ chỉ hiển thị cho phiên bản cụ thể đó

Để tạo, cập nhật hoặc truy cập bất kỳ thuộc tính cá nhân nào từ bên trong Lớp, bạn cần sử dụng 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
874cùng với ký hiệu dấu chấm. This, 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
874là một thuộc tính đặc biệt của đại diện cho phiên bản hiện tại. Mặt khác, để cập nhật hoặc truy cập bất kỳ thuộc tính cá thể nào từ bên ngoài lớp, bạn cần tạo một cá thể và sau đó sử dụng ký hiệu dấu chấm. Đây là cách nó hoạt động

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
40

Lớp 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859 nhận một đối số được gọi là 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646, đối số này được tự động nhân đôi bên trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
878 bằng cách sử dụng thao tác gán 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
879. Lưu ý rằng khi bạn kiểm tra 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6trên 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
872, bạn nhận được một cuốn từ điển bao gồm tất cả các thuộc tính ví dụ. Trong trường hợp này, từ điển chỉ chứa tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646, giá trị của nó hiện tại 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
973

Lưu ý. Để biết thêm về cách các lớp hoạt động trong Python, hãy xem Giới thiệu về Lập trình hướng đối tượng trong Python

Mặc dù bạn có thể tạo các thuộc tính cá nhân trong bất kỳ phương thức nào trong một lớp, nhưng bạn nên tạo và khởi động chúng bên trong 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
878. Vui lòng xem phiên bản mới này của 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
41

Tại đây, bạn đã sửa đổi ________ 5859 để bổ sung một phương thức mới được gọi là 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
887. Sau đó, bạn tạo một thể hiện của 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
859 bằng cách chuyển vào 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
601trình khởi tạo lớp. Sau đó, bây giờ bạn có thể gọi ________ 5887 thành ________ 5872 để nhân đôi giá trị được lưu trữ trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
892. Cuối cùng, nếu bạn cố gắng truy cập 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646 bằng cách sử dụng các đối tượng lớp thay vì một cá thể, thì bạn sẽ nhận được thông báo 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
864vì không thể truy cập các thuộc tính cá thể bằng cách sử dụng các đối tượng lớp

Nói chung, khi bạn đang viết mã hướng đối tượng bằng Python và bạn cố gắng truy cập một thuộc tính, chương trình của bạn sẽ thực hiện các bước sau

  1. Check tra dụ phạm vi địa phương hay không gian đầu tiên
  2. Nếu thuộc tính không được tìm thấy ở đó, hãy kiểm tra phạm vi bộ cục bộ của lớp hoặc không gian tên
  3. Nếu tên không tồn tại trong không gian tên lớp, thì bạn sẽ nhận được 
    >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    864

Đây là chế độ cơ bản mà Python giải quyết các tên trong các lớp và phiên bản

Mặc dù các lớp xác định phạm vi bộ cục bộ của lớp hoặc không gian tên, nhưng chúng không tạo phạm vi bao quanh cho các phương thức. Do đó, khi bạn đang triển khai một lớp, các tham chiếu đến thuộc tính và phương thức phải được thực hiện bằng cách sử dụng ký hiệu dấu chấm

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
42

Vì các lớp không tạo phạm vi bao quanh cho các phương thức nên bạn không thể truy cập 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646trực tiếp từ bên trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
897như cách bạn cố gắng làm ở đây. Để có quyền truy cập vào các thuộc tính lớp từ bên trong bất kỳ phương thức nào, bạn cần sử dụng ký hiệu dấu chấm. Để giải quyết sự cố trong ví dụ này, hãy thay đổi câu lệnh 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
898bên trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
897thành 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
400và xem điều gì sẽ xảy ra

Bạn có thể ghi đè một lớp thuộc tính bằng một thuộc tính cá thể, điều này sẽ sửa đổi hành vi chung của lớp bạn. Tuy nhiên, bạn có thể truy cập rõ ràng cả hai thuộc tính bằng cách sử dụng ký hiệu dấu chấm như trong ví dụ sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
43

Lớp trên có một thuộc tính có thể hiện và một lớp thuộc tính có cùng tên 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646. Bạn có thể sử dụng mã sau để truy cập từng mã

  1. version. Sử dụng 
    >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    892 để truy cập thuộc tính này
  2. Class. Sử dụng 
    >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    403 để truy cập thuộc tính này

Vì cả hai trường hợp nhất đều sử dụng ký hiệu dấu chấm nên không có vấn đề gì về va chạm tên

Lưu ý. Nói chung, các phương pháp OOP tốt khuyên bạn không nên phủ bóng các thuộc tính lớp với các thuộc tính cá nhân có thể có các nhiệm vụ khác nhau hoặc thực hiện các hành động khác nhau. Làm như vậy có thể dẫn đến các lỗi tinh vi và khó tìm

Cuối cùng, hãy lưu ý rằng class 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6và instance 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6là các từ điển hoàn toàn khác nhau và độc lập. Đó là lý do tại sao các thuộc tính của lớp có sẵn ngay lập tức sau khi bạn chạy hoặc nhập mô-đun mà lớp đó đã được định nghĩa. Ngược lại, các thuộc tính cá nhân chỉ có thể hoạt động sau khi một đối tượng hoặc cá thể được tạo

Sử dụng các chức năng tổng hợp liên quan đến phạm vi

Có rất nhiều hàm dựng sẵn có liên quan chặt chẽ đến khái niệm phạm vi và không gian tên trong Python. Trong các phần trước, bạn đã từng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643 lấy thông tin về các tên tồn tại trong một phạm vi nhất định. Bên cạnh đó 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643, có một số hàm phân tích khác có thể giúp bạn khi bạn đang cố lấy thông tin về phạm vi Python hoặc không gian tên. Trong phần này, bạn sẽ trình bày cách làm việc với

  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    408
  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    409
  • >>> def outer_func[]:
    ..     # This block is the Local scope of outer_func[]
    ..     var = 100  # A nonlocal var
    ..     # It's also the enclosing scope of inner_func[]
    ..     def inner_func[]:
    ..         # This block is the Local scope of inner_func[]
    ..         print[f"Printing var from inner_func[]: {var}"]
    ...
    ..     inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Printing var from outer_func[]: 100
    >>> inner_func[]
    Traceback [most recent call last]:
      File "", line 1, in 
    NameError: name 'inner_func' is not defined
    
    643
  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    411

Vì tất cả các chức năng này đều là các chức năng tích hợp sẵn nên chúng có sẵn miễn phí trong phạm vi tích hợp sẵn. Điều này có nghĩa là bạn có thể sử dụng chúng bất kỳ lúc nào mà không cần nhập bất kỳ thứ gì. Hầu hết các chức năng này Mục đích đích được sử dụng trong một phiên tương tác để lấy thông tin về các đối tượng Python khác nhau. Tuy nhiên, bạn cũng có thể tìm thấy một số trường hợp sử dụng thú vị cho họ trong mã của mình

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408

Trong Python, 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408là một hàm tổng hợp trả về một tham chiếu đến phạm vi toàn cầu hoặc từ điển không gian tên hiện tại. Từ điển này luôn lưu trữ tên của mô-đun hiện tại. Điều này có nghĩa là nếu bạn gọi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408trong một mô-đun định trước nhất, thì bạn sẽ nhận được một từ điển bao gồm tất cả các tên mà bạn đã xác định trong mô-đun đó, ngay trước lệnh gọi tới 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408. This is a ví dụ

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
44

Lệnh gọi đầu tiên 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408trả về một từ điển chứa các tên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639mô-đun hoặc chương trình của bạn. Lưu ý rằng khi bạn chỉ định một tên mới ở mức cao nhất của mô-đun, chẳng hạn như trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
418, thì tên đó sẽ được thêm vào từ điển và được trả về bởi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408

Một ví dụ thú vị về cách bạn có thể sử dụng 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408trong mã của mình là điều phối các chức năng nằm trong phạm vi toàn cầu. Giả sử bạn muốn điều khiển các chức năng phụ thuộc vào nền tảng phân phối. Để làm điều này, bạn có thể sử dụng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408như sau

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
45

Nếu bạn chạy tập lệnh này trong dòng lệnh của mình, thì bạn sẽ nhận được đầu ra phụ thuộc vào nền tảng hiện tại của bạn

Một ví dụ khác về cách sử dụng 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408 sẽ là kiểm tra danh sách các tên đặc biệt trong phạm vi toàn cầu. Vui lòng xem qua danh sách sau

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
46

Tính năng hiểu danh sách này sẽ trả về một danh sách với tất cả các tên đặc biệt đã được xác định trong phạm vi Python toàn cầu hiện tại của bạn. Lưu ý rằng bạn có thể sử dụng ________ 6408 từ điển giống như sử dụng bất kỳ từ điển thông thường nào. Ví dụ. you could lặp qua nó thông qua nó bằng các phương pháp truyền thống sau

  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    9
  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    425
  • >>> def outer_func[]:
    ..     var = 100
    ..     def inner_func[]:
    ..         print[f"Printing var from inner_func[]: {var}"]
    ..         print[f"Printing another_var from inner_func[]: {another_var}"]
    ...
    ..     inner_func[]
    ..     another_var = 200  # This is defined after calling inner_func[]
    ..     print[f"Printing var from outer_func[]: {var}"]
    ...
    >>> outer_func[]
    Printing var from inner_func[]: 100
    Traceback [most recent call last]:
      File "", line 1, in 
        outer_func[]
      File "", line 7, in outer_func
        inner_func[]
      File "", line 5, in inner_func
        print[f"Printing another_var from inner_func[]: {another_var}"]
    NameError: free variable 'another_var' referenced before assignment in enclosing
     scope
    
    426

Bạn cũng có thể thực hiện các hoạt động đăng ký thông thường 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408 bằng cách sử dụng dấu phân tách như trong 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
428. Ví dụ. bạn có thể sửa đổi nội dung của _______ 6408 mặc dù điều này không được khuyến nghị. Hãy xem ví dụ này

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
47

Tại đây, bạn thay đổi từ khóa 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
430 để bao gồm một chuỗi tài liệu đến 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
639từ bây giờ, chuỗi tài liệu của mô-đun chính sẽ có giá trị 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
432

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409

Một chức năng khác liên quan đến phạm vi Python và không gian tên là 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409. Hàm này cập nhật và trả về một từ điển chứa bản sao của trạng thái hiện tại của phạm vi hoặc không gian tên Python bộ cục bộ. Khi bạn gọi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409trong một khối chức năng, bạn nhận được tất cả các tên được chỉ định trong phạm vi bộ cục bộ hoặc chức năng cho đến thời điểm bạn gọi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409. This is a ví dụ

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
48

Bất kể khi nào bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
650, từ điển kết quả chứa tên 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646được ánh xạ tới giá trị 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
601và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
441được ánh xạ tới 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
442. Because
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409chỉ lấy các tên được chỉ định trước khi bạn gọi nó,
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
444không có trong từ điển

Nếu bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409trong phạm vi Python toàn cầu, thì bạn sẽ nhận được cùng một từ điển mà bạn sẽ nhận được nếu bạn gọi 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408

>>>

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
49

Khi bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409trong phạm vi Python toàn cầu, bạn sẽ nhận được một từ điển giống kiểu mẫu với từ điển được trả về bằng lệnh gọi tới 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
408

Lưu ý rằng bạn không nên sửa đổi nội dung của 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409vì những thay đổi đó có thể không ảnh hưởng đến giá trị của tên địa phương và tên tự do. Check ví dụ sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
0

Khi bạn cố gắng sửa đổi nội dung 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646 sử dụng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409, thay đổi không phản ánh giá trị của 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
646. Vì vậy, bạn có thể nói rằng điều đó 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409chỉ hữu ích cho các hoạt động đọc vì các bản cập nhật or cho 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
454từ điển bị Python bỏ qua

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411là một hàm tích hợp trong Python được trả về
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6 thuộc tính của mô-đun, lớp, cá thể hoặc bất kỳ đối tượng nào khác có từ điển thuộc tính. Hãy nhớ rằng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6là một từ điển đặc biệt mà Python sử dụng để khai thác các tên không gian. Vui lòng xem các ví dụ sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
1

Khi bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411 sử dụng 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8như một tham số, bạn sẽ có được 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6 của 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
8. Bạn cũng có thể gọi ________ 6411 bằng cách sử dụng các loại đối tượng Python khác nhau, miễn phí là chúng có thuộc tính từ điển này

Không có bất kỳ đối số nào, 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411 hoạt động giống như 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409và trả về một từ điển với tất cả các tên trong phạm vi Python cục bộ

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
2

Tại đây, bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411ở mức cao nhất của một phiên hợp tác. Không có đối số, lệnh gọi này trả về một từ chứa tất cả các tên trong phạm vi Python toàn cầu. Lưu ý rằng, ở cấp độ này, 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411và 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
409trả lại cùng một từ điển

Nếu bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411với một đối tượng không có 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6, thì bạn sẽ nhận được một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
902, như trong ví dụ sau

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
3

Nếu bạn gọi 

>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
411với một số nguyên đối tượng, thì bạn sẽ nhận được một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
902vì loại đối tượng Python này không có một 
>>> def outer_func[]:
..     var = 100
..     def inner_func[]:
..         print[f"Printing var from inner_func[]: {var}"]
..         print[f"Printing another_var from inner_func[]: {another_var}"]
...
..     inner_func[]
..     another_var = 200  # This is defined after calling inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Traceback [most recent call last]:
  File "", line 1, in 
    outer_func[]
  File "", line 7, in outer_func
    inner_func[]
  File "", line 5, in inner_func
    print[f"Printing another_var from inner_func[]: {another_var}"]
NameError: free variable 'another_var' referenced before assignment in enclosing
 scope
6

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643

Bạn có thể sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mà không cần tham số để lấy danh sách tên trong phạm vi Python hiện tại. Nếu bạn gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643với một đối số, thì hàm sẽ cố gắng trả về một 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
693trong các thuộc tính hợp lệ cho đối tượng đó

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
4

Nếu bạn gọi 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mà không có đối số, thì bạn sẽ nhận được một danh sách chứa các tên nằm trong phạm vi toàn cục. Bạn cũng có thể sử dụng 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643 để kiểm tra danh sách tên hoặc thuộc tính của các đối tượng khác nhau. Điều này bao gồm các chức năng, mô-đun, biến, v. v

Mặc dù tài liệu chính thức nói rằng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643mục đích sử dụng tương tác, bạn có thể sử dụng chức năng để cung cấp danh sách toàn diện các thuộc tính của một đối tượng nhất định. Lưu ý rằng bạn cũng có thể gọi 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643từ bên trong một hàm. Trong trường hợp này, bạn sẽ nhận được danh sách các tên được xác định trong phạm vi hàm

>>>

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
5

Trong ví dụ này, bạn sử dụng 

>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643bên trong 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
650. Khi bạn gọi hàm, bạn sẽ nhận được một danh sách chứa các tên mà bạn xác định trong phạm vi bộ cục bộ. Cần lưu ý rằng trong trường hợp này, 
>>> def outer_func[]:
..     # This block is the Local scope of outer_func[]
..     var = 100  # A nonlocal var
..     # It's also the enclosing scope of inner_func[]
..     def inner_func[]:
..         # This block is the Local scope of inner_func[]
..         print[f"Printing var from inner_func[]: {var}"]
...
..     inner_func[]
..     print[f"Printing var from outer_func[]: {var}"]
...
>>> outer_func[]
Printing var from inner_func[]: 100
Printing var from outer_func[]: 100
>>> inner_func[]
Traceback [most recent call last]:
  File "", line 1, in 
NameError: name 'inner_func' is not defined
643chỉ hiển thị những tên bạn đã khai báo trước khi gọi hàm

Phần kết luận

Các phạm vi của một biến hoặc tên xác định tầm nhìn của mình trên suốt mã của bạn. Trong Python, phạm vi được khai thác dưới dạng phạm vi Cục, Bao gồm, Toàn cầu hoặc Tích hợp. Khi bạn sử dụng một biến hoặc tên, Python sẽ tìm kiếm các phạm vi này theo một tuần tự để giải quyết nó. Nếu tên không được tìm thấy, thì bạn sẽ gặp lỗi. Đây là cơ chế chung mà Python sử dụng để phân giải tên và được gọi là quy tắc LEGB

Bây giờ bạn có thể

  • Tận dụng phạm vi Python để tránh hoặc giảm thiểu các lỗi liên quan đến xung đột tên
  • Hãy sử dụng tốt tên toàn cầu và địa phương trên toàn bộ chương trình của bạn để cải thiện mã bảo trì
  • Sử dụng một chiến lược quán nhất để truy cập, sửa đổi hoặc cập nhật tên trên tất cả mã Python của bạn

Ngoài ra, bạn đã đề cập đến một số công cụ và kỹ thuật liên quan đến phạm vi mà Python cung cấp và cách bạn có thể sử dụng chúng để thu thập thông tin về các tên nằm trong phạm vi định dạng nhất hoặc để sửa đổi . Tất nhiên, còn nhiều điều về chủ đề này nằm ngoài phạm vi của hướng dẫn này, vì vậy hãy thoát khỏi đó và tiếp tục giải quyết vấn đề có tên bằng Python

Chủ Đề