# Python Tutorial - Namescape and Scope

Jinku Hu Mar 04, 2021 Feb 04, 2018

In this section, you will learn namespace and scope of a variable in Python programming.

## Name in Python

A name in Python is an identifier given to the object. Python is an object-oriented programming language which means everything is an object in Python and names are used to access objects.

For instance, when you assign a value to a variable `x = 3`. Here `3` is an object and is stored at a memory location (RAM). This memory location is a named memory location that could be accessed by the name `x`. The address of this object (3) can be obtained by using the built-in function `id()`.

``````x = 3
print('id(3) =', id(3))
print('id(x) =', id(x))
``````
``````id(3) = 1864160336
id(x) = 1864160336
``````

Now here object `3` is stored in a location whose name is `x` and hence the address of both of them is the same.

When you assign the value of `x` to some other variable let’s say `y` and change value of `x`, then `x` will have a new location and `y` will have the location of previous `x`. This is demonstrated below:

``````x = 3
print('id(x) =', id(x))
x = 4
print('id(4) =', id(4))
print('id(x) =', id(x))
y = 3
print('id(y) =', id(y))
``````
``````id(x) = 1864160336
id(4) = 1864160352
id(x) = 1864160352
id(y) = 1864160336
``````

You can see here the address of `y` equals that of old `x`.

## Namespace in Python

A namespace is a collection of names. A namespace is a mapping of name to an object and is implemented as a dictionary in Python. Namespaces ensure that every name used in a program is unique.

Namespaces are created when the interpreter is started, deleted when program execution is ended. This namespace contains all the built-in names, so whenever you need a built-in function for example `id()` you can use it directly in your program.

When you call a function, a local namespace that contains all the defined names is created.

## Variable Scope in Python

When you create a variable in your program, you may not have access to that variable from every part of the program. This is because of the scope of variables. You try to access variables from the namespace where they are not defined.

A scope can be defined as a place from where you can access your namespace without any prefix.

The Scope can be categorized as:

1. Scope of a function where you have local names.
2. Scope of a module where you have global variables.
3. Outermost scope where you have built-in names.

When you create a reference inside a function, it is searched in the local namespace first and then it is searched in global namespace; if it is not found anywhere it will be searched in the built-in namespace.

When creating a nested function, you will have a new scope inside of the local scope.

## Use Scope and Namespace in Python

``````def outer():
x = 12
def inner():
y = 13
z = 14
``````

Here, a nested function is defined (function inside another function). Variable `y` is in the nested local namespace of the inner function, variable `x` is in the local namespace of the outer function and variable `z` is in the global namespace.

When it is inside the inner function, variable `y` is a local variable, `x` is non-local and `z` is global. The inner function has no access to non-local `x` and global `z` variables.

Author: Jinku Hu

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8372695,"math_prob":0.86139387,"size":3142,"snap":"2022-40-2023-06","text_gpt3_token_len":774,"char_repetition_ratio":0.15551306,"word_repetition_ratio":0.0,"special_character_ratio":0.25970718,"punctuation_ratio":0.082170546,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95910716,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-09T02:02:48Z\",\"WARC-Record-ID\":\"<urn:uuid:7f0ae12a-6aca-488f-a8d0-7e3d13a5a5c0>\",\"Content-Length\":\"47442\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5c0515c-5e84-47f7-b2f3-f336c57b7637>\",\"WARC-Concurrent-To\":\"<urn:uuid:de02024d-00ca-45bb-8ba1-766a6b56c0d6>\",\"WARC-IP-Address\":\"18.232.245.187\",\"WARC-Target-URI\":\"https://www.delftstack.com/tutorial/python-3-basic-tutorial/namescape-and-scope/\",\"WARC-Payload-Digest\":\"sha1:JEEU6BESJRFI5DLQ5NTIAODYGXAAVQFT\",\"WARC-Block-Digest\":\"sha1:TGEMMYIL65BDDEGZS32GWSKLIX3IEKIV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764501066.53_warc_CC-MAIN-20230209014102-20230209044102-00421.warc.gz\"}"}