What is the relationship between scope and namespaces in Python?

In many resources I found “scope” and “namespaces” are used interchangeably, which seems a bit confusing since they mean different things.

  • Scope defines the region of the code where a name is available.
  • The LEGB rule defines the way names are looked up.
  • Namespace is a place where you look up names.

Then I read:

  • “names are bind to a namespace according to where they are assigned…” (which I believe is the deal with scopes in lexical scoping).
  • “functions add an extra namespace layer to your programs” [ref.] (don’t they add a extra local scope?)
  • “all the names assigned inside a function definition are put in the local scope (the
    namespace associated with the function call).”
  • “global scope—that is, a namespace in which variables created (assigned) at the top level of the module file live.”

*all of the quotes are from learning python 5th edition ch17

Are namespaces in Python the way scopes are implemented? Are they the same thing? Can anyone enlighten me?

2

A namespace is a dictionary, mapping names (as strings) to values. When you do an assignment, like a = 1, you’re mutating a namespace. When you make a reference, like print(a), Python looks through a list of namespaces to try and find one with the name as a key.

A scope defines which namespaces will be looked in and in what order. The scope of any reference always starts in the local namespace, and moves outwards until it reaches the module’s global namespace, before moving on to the builtins (the namespace that references Python’s predefined functions and constants, like range and getattr), which is the end of the line.

Imagine you have a function named inner, nested within a global function named outer, and inner contains a reference to a name. Python first looks in the inner namespace. If the name’s not there, Python then looks in the outer namespace. If that fails, Python tries the module’s global namespace, then the builtin namespace, eventually throwing a NameError if the name isn’t found.

When we say x is in a function’s namespace, we mean it is defined there, locally within the function. When we say x is in the function’s scope, we mean x is either in the function’s namespace or in any of the outer namespaces that the function’s namespace is currently nested within.

Whenever you define a function, you create a new namespace and a new scope. The namespace is the new, local hash of names. The scope is the implied chain of namespaces that starts at the new namespace, then works its way through any outer namespaces (outer scopes), up to the global namespace (the global scope), and on to the builtins.

The terms can be used almost interchangeably, but that’s not because they mean the same thing; it’s because they overlap a lot in what they imply.

9

There is an excellent article on Python namespaces here. To quote the relevant part to answer your question about the reference between scopes and namespaces:

A scope refers to a region of a program from where a namespace can be accessed without a prefix.

For example, imagine a simple die-rolling program:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import random # 'random' is in module namespace
def roll(sides=6): # 'roll' is in module namespace, 'sides' is in roll's
return random.randint(1, sides) # both 'random' and 'sides' are in scope here
# but sides can't be accessed out here
</code>
<code>import random # 'random' is in module namespace def roll(sides=6): # 'roll' is in module namespace, 'sides' is in roll's return random.randint(1, sides) # both 'random' and 'sides' are in scope here # but sides can't be accessed out here </code>
import random  # 'random' is in module namespace

def roll(sides=6):  # 'roll' is in module namespace, 'sides' is in roll's
    return random.randint(1, sides)  # both 'random' and 'sides' are in scope here

# but sides can't be accessed out here 

roll has its own namespace, but the names in the module’s namespace are also in scope.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật