AppDividend
Latest Code Tutorials

Python locals() Example | locals() Function Tutorial

0

Python locals() Example | locals() Function Tutorial is today’s topic. The locals() function returns the dictionary containing the variables defined in a local namespace. Calling locals() in a global namespace is same as calling the globals() and returns the dictionary representing the global namespace of the module.

Python locals()

The locals() method updates and returns the dictionary of the current local symbol table.

The symbol table is the data structure maintained by the compiler which contains all necessary information about the program.

These include variable names, methods, classes, etc.

There are mainly two kinds of the symbol table.

  1. Global symbol table
  2. Local symbol table

Global symbol table stores all the information related to a global scope of the program, and is accessed in the Python using globals() method.

The global scope contains all the functions, variables which are not associated with any class or function.

Likewise, the Local symbol table stores all information related to the local scope of the program, and is accessed in Python using the locals() method.

The local scope could be within the function, within a class, etc.

#Syntax of locals()

See the following syntax.

locals()

The locals() method doesn’t take any parameters.

The locals() method updates and returns the dictionary associated with a current local symbol table.

The globals() and locals() symbol table for the global environment is the same.

See the following example.

# app.py

def collab():
    return locals()


def localscollab():
    cross = True
    return locals()


print('localsNotPresent:', collab())
print('localsPresent:', localscollab())

In the above example, we have defined the two functions.

In collab() function, we did not define any variable. So that function does not contain any local variable.

In localscollab() function, we have defined one local variable called the cross. So, when the function returns the locals(), it will contain the cross variable.

See the following output.

➜  pyt python3 app.py
localsNotPresent: {}
localsPresent: {'cross': True}
➜  pyt

#Updating locals() dictionary values

Okay, now let’s try to update the local value and then read the value.

# app.py

def localscollab():
    eleven = 11
    print(eleven)
    locals()['eleven'] = 15
    print(eleven)


localscollab()

See the following output.

➜  pyt python3 app.py
11
11
➜  pyt

Unlike, the globals() dictionary which reflects the change to the actual global table, whereas the locals() dictionary may not change an information inside the locals table.

#Get the filename of the current program

Okay, let’s write the code that will return the current file name using locals() function. My current file name is app.py. 

# app.py

data = locals()
print(data["__file__"])

See the output.

➜  pyt python3 app.py
app.py
➜  pyt

#Python locals() inside class

Let’s see outcome when locals() is called inside the class body.

# app.py

class Data:
    eleven = 11
    print('\nlocals() value inside class\n', locals())

See the output.

➜  pyt python3 app.py
locals() value inside class
 {'__module__': '__main__', '__qualname__': 'Data', 'eleven': 11}
➜  pyt

When invoked inside a class body, the locals() contains the module name, class name, and class variables.

#Difference between locals() and globals()

There is no difference because we are executing the locals() and globals() in the current module itself.

We can see the difference when we call these functions inside a method or class.

In which context and scope we are using decide the values of these functions.

There is also the third function called vars().

If the locals() is called inside a function it constructs a dictionary of the function namespace as of that moment and returns it, any further name assignments are not reflected in the returned dictionary, and any assignments to a dictionary are not reflected in an actual local namespace.

Finally, Python locals() Example | locals() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.