# 4.7.2.     네임스페이스(Namespace)와 범위(Scoping)

변수란 객체에 매핑 되는 이름(식별자: identifiers)를 말합니다. 그러나 전체 프로그래밍 공간에서 고유한 이름으로 변수를 구별할 수 없습니다. 프로그래밍언어에서는 네임스페이스(name space: 이름공간)라는 개념을 도입하여, 특정한 하나의 이름이 통용될 수 있는 범위를 제한합니다. 파이썬에서도 변수의 유효 범위를 네임스페이스(namespace) 기반으로 결정합니다. 따라서 소속된 네임스페이스가 다르다면 같은 이름이 다른 개체를 가리키도록 하는 것이 가능해집니다.

파이썬 문은 로컬 네임스페이스와 전역 네임스페이스의 변수에 액세스 할 수 있습니다. 함수의 값이 할당된 변수는 모두 로컬 변수라고 가정합니다. 따라서 함수 내의 전역 변수에 값을 할당하려면 먼저 global 문을 사용해야합니다.

globals() 및 locals() 함수는 호출된 위치에 따라 전역 및 로컬 네임스페이스의 이름을 반환하는 데 사용할 수 있습니다. locals()가 함수 내에서 호출되면 해당 함수에서 로컬로 액세스 할 수 있는 모든 이름을 반환합니다. globals()가 함수 내에서 호출되면 함수에서 전역적으로 액세스 할 수 있는 모든 이름을 반환합니다. 이 두 함수의 반환 유형은 dictionary입니다. 따라서 keys() 함수를 사용하여 이름을 추출할 수 있습니다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sdc-james.gitbook.io/onebook/3./3.7.-modules/3.7.2.-namespace-scoping.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
