Welcome guys! In this article we will going to discuss about name and namespace in Python programming language.
What is Name in Python?
When we create any object in Python programming language we give name to that object. In python, we simply use variable name as name in our python programs. And as we earlier discussed in about python variable, we know that we can declare variable names and can assign them to various objects.
What is Python Namespace?
A namespace is simple defined as a list or collection of unique names and the details of each and every object referenced by those names in python. In simple terms, Namespace is a method to make sure that every name in the program are unique and can be used without causing any confusion or problem. In python, namespace are considered as dictionary.
Types of Python Namespace
There are four types of Python Namespace which are explained as follows;
Global Namespace
Example:
xvar1 = 5xvar2 = 5
def add(var1, var2):
temp = var1 + var2
return temp
In the above example we have used and from global namespace.
Local Namespace
In Local Namespace, all those names are included which are defined for a function, class or block of code and are local to it. In python, local namespace is created with the execution of function and destroys with the termination of function.
Example:
xvar1 = 5xvar2 = 5
def add(var1, var2):
temp = var1 + var2
return temp
In the above example, we have used the variable name , and which are defined in the local name space.
Enclosing Namespace
As we all are aware of the fact that we can define one function within another function and the function which is defined inside the other function can access namespace from the function which is outside. So, the namespace of the outer function is defined as the enclosing namespace.
Example:
xvar1 = 5xvar2 = 5
def add(var1, var2):
temp = var1 + var2
def print_sum():
print(temp)
return temp
Here, function is defined inside the function so, the function is the enclosing namespace.
Built-in Namespace
In Built-in Namespace, all names of the built-in function and objects are included. These names are created when we start python interpreter and accessible through the program and destroyed when the the interpreter is closed. With the help of following command, we can access the list of all names in built-in namespace.
builtin_types = dir(__builtins__)for types in builtin_types:
print(types)
ArithmeticErrorAssertionErrorAttributeErrorBaseExceptionBlockingIOErrorBrokenPipeErrorBufferErrorBytesWarningChildProcessErrorConnectionAbortedErrorConnectionErrorConnectionRefusedErrorConnectionResetErrorDeprecationWarningEOFErrorEllipsisEnvironmentErrorExceptionFalseFileExistsErrorFileNotFoundErrorFloatingPointErrorFutureWarningGeneratorExitIOErrorImportErrorImportWarningIndentationErrorIndexErrorInterruptedErrorIsADirectoryErrorKeyErrorKeyboardInterruptLookupErrorMemoryErrorModuleNotFoundErrorNameErrorNoneNotADirectoryErrorNotImplementedNotImplementedErrorOSErrorOverflowErrorPendingDeprecationWarningPermissionErrorProcessLookupErrorRecursionErrorReferenceErrorResourceWarningRuntimeErrorRuntimeWarningStopAsyncIterationStopIterationSyntaxErrorSyntaxWarningSystemErrorSystemExitTabErrorTimeoutErrorTrueTypeErrorUnboundLocalErrorUnicodeDecodeErrorUnicodeEncodeErrorUnicodeErrorUnicodeTranslateErrorUnicodeWarningUserWarningValueErrorWarningZeroDivisionError__build_class____debug____doc____import____loader____name____package____spec__absallanyasciibinboolbreakpointbytearraybytescallablechrclassmethodcompilecomplexcopyrightcreditsdelattrdictdirdivmodenumerateevalexecexitfilterfloatformatfrozensetgetattrglobalshasattrhashhelphexidinputintisinstanceissubclassiterlenlicenselistlocalsmapmaxmemoryviewminnextobjectoctopenordpowpropertyquitrangereprreversedroundsetsetattrslicesortedstaticmethodstrsumsupertupletypevarszip
Conclusion
Above we have discussed about name and namespace in python programming language. A namespace is simple defined as a list or collection of unique names and the details of each and every object referenced by those names in python. And there are four types of namespaces in python i.e., Global, Local, Enclosing and Built-in Namespace.