Python Name, Namespace, & Scope

    In this tutorial, we will discuss some technical terms of python that we often use in python and other programming languages. so let's discuss Python Name, Namespace, and Scope.

    Python Name, Namespace, and Scope

    Name in Python

    Name in python also known as Identifiers. As we have discussed earlier that everything in python is an object. Identifiers are the names that are used to assign to data or object. If we specifically discuss what is Name in python, we can say that Name is a name that can be assigned to an object, and the Name helps us to access the object. Let’s understand it with an Example Suppose you used the assignment operator (=) to assign a value 200 which is an integer or you can also call it an object and you assign the value 200 to a variable number. So, here 200 is the object and number is Name. Let’s code the above statement using python

    number = 200
    print(number)

    Output

    200

    Let’s understand the concept of Name with the inbuilt id() function. The id() function is used to return the memory location of the object in the integer form.

    number_1 = 200
    number_2 = 200
    print("the ID of number is: ", id(number_1))
    print("the ID of 200 is: ",id(200))
    print("the ID of number_2 is: ", id(number_2))

    Output

    #the ID values might be different when you run the above program on your system.
    the ID of number is:  1770734960
    the ID of 200 is:  1770734960
    the ID of number_2 is:  1770734960

    from the above example, you can see that the id values for each variable and even for the object (200) is the same what does it signify? It signifies that every object occupies a unique place in the memory, and the Names or identifies associated with that object are just used to access it. Using the Name concept of Python, you can make duplicates of a predefined function; for example, you can duplicate the function print().

    Let’s code to duplicate the function print().

    dup_print = print
    dup_print("this statement is printing using Duplicate print")

    Output:

    this statement is printing using Duplicate print You cannot duplicate the name; a name cannot access two different objects simultaneously. A Name can access to a single object.

    var = "Hello"
    var =  23
    var = [1,2,3,4,5]
    print(var)

    Output

    [1, 2, 3, 4, 5]

    Namespace in Python

    As in the above example, we have discussed that a Name cannot be duplicated or a name can only access an object. In simple words, two objects cannot share the same Name. For example, a variable cannot have two objects as its value. But what if we want a variable name common for two different objects can we do that? Yes, with the help of Namespace concept, we can name two different objects by the same name and use the different objects for different purposes. So, if we define the Namespace; Namespace is a concept in python that allows different objects to share a common name without having any conflict between them. For using the namespace concept, there is a condition that both name having different objects or structure should be isolated from each other.

    Let’s understand it with an example:

    var = 30                #var outside the function
    
    def name_space():
      var= [1,2,3,4,5]                  # var inside a function
      print(var) #[1,2,3,4,5]
    
    name_space()
    print(var)   #30

    Output

    [1, 2, 3, 4, 5]
    30

    In the above example, we see that var has two objects one is integer 30, and the other is a list [1,2,3,4,5], but both the name var are separated from each other because we have isolated the list var inside the function name_space(). You can see that python has no problem with the var which is outside the function, and the value of var which is outside the function did not change either.

    The namespace is divided into three types:

    1. Built-in Namespace
    2. Global Namespace
    3. Local Namespace

    Python Variable Scope

    The scope is a concept in python that deals with the visibility of the identifiers or names; it is used to tell the program which part of the program can access which variable. The scope comes very useful when we use the concept of Namespace.

    Python has two scopes.

    1. Local Scope variable: All those variables which are assigned inside a function known as the local scope Variable
    2. Global Scope variable: All those variables which are outside the function are termed as a global variable

    Let’s understand it with an example;

    global_var = 30         # global scope
    def scope():
        local_var = 40      #local scope
        print(global_var)   #30
        print(local_var)    #40
    
    scope()
    
    print(global_var)       #30

    Output

    30
    40
    30

    In the above example, you saw that we can access a global variable inside a function but can we access the local variable outside the function no. if you do that it will throw an error.

    def scope():
        local_var = 40
        print(local_var)  #40
    
    scope()
    print(local_var)      #error

    #Output

    40 
    File "main.py", line 9, in <module>
    print(local_var)
    NameError: name 'local_var' is not defined

    In the above example we saw that we can access the global variable inside a function can we change alter its value inside the function?

    Yes, for that you need a keyword global which will import the global variable inside the function and you can alter its value which will remain permanent.

    Let’s understand it with an example.

    global_var = 30
    
    def scope():
        global global_var
        global_var+=70     #update the global variable
    
    scope()
    
    print(global_var)      #100

    Output

    100

    What if you do not use the global keyword to import the global variable inside the function then it will throw an error.

    Points to remember

    • The name is the identifier
    • Two objects can share the same name if both are isolated.
    • You can not access the local variable outside the function
    • To import the global variable instead of making a namespace inside a function use the global keyword