
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How will you compare modules, classes and namespaces in Python?
Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. When a function, module or package is evaluated (that is, starts execution), a namespace is created. Think of it as an "evaluation context". When a function, etc., finishes execution, the namespace is dropped. The variables are dropped. Plus there's a global namespace that's used if the name isn't in the local namespace.
Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module. So, a python module is nothing but a package to encapsulate reusable code. Modules reside in a folder with a __init__.py file on it. Modules can contain functions but also classes. Modules are imported using the import keyword.
Classes, in the other hand, can be defined in your main application code or inside modules imported by your application. Classes are the core of Object Oriented Programming and can contain properties and methods. You can create multiple instances of a class, but you cannot create instances of a module. You could compare modules to static classes or singletons.
- Related Articles
- How will you compare namespaces in Python and C++?
- How will you explain Python namespaces in easy way?
- How to organize Python classes in modules and/or packages
- Namespaces and Scoping in Python
- Namespaces and Scope in Python
- How can you compare and Install different Python GUI frameworks?
- How to generate XML documents with namespaces in Python?
- How do you compare Python objects with .NET objects?
- How do Python modules work?
- How to install Python modules in Cygwin?
- XMLRPC server and client modules in Python
- What is difference between builtin and globals namespaces in Python?
- Reloading modules in Python?
- Locating Modules in Python
- How will you explain Python Operator Overloading?
