Found 10476 Articles for Python

How will you compare modules, classes and namespaces in Python?

Pranathi M
Updated on 11-May-2023 14:31:57

814 Views

Python allows you to save definitions to a file and then use them in a script or interactive instance of the interpreter. A module is a file that contains definitions that can be imported into other modules or the main module. So, a Python module is nothing more than a package that contains reusable code. Modules are stored in a folder that contains a __init .py file. Modules can contain both functions and classes. The import keyword is used to import modules. A file containing Python commands and definitions is referred to as a module. These files named .py ... Read More

How will you compare namespaces in Python and C++?

SaiKrishna Tavva
Updated on 27-Jan-2025 17:14:26

834 Views

Namespaces help in organizing code, managing the scope of variables and preventing naming conflicts. Python and C++ use namespaces, but they do so in different ways. Below is an overview of namespaces in both. Namespaces in C++ In C++, namespaces are created using the keyword 'namespace'. They are mainly intended to organize code into logical groups and avoid name conflicts, particularly when working with multiple libraries. Example In the following example we are going to how to use a namespace by utilizing '::' to access functions within that namespace. #include using namespace std; // first namespace namespace first_space ... Read More

Explain the visibility of global variables in imported modules in Python?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:24:41

1K+ Views

Globals in Python are global to a module, not across all modules. (Unlike C, where a global is the same across all implementation files unless you explicitly make it static.). If you need truly global variables from imported modules, you can set those at an attribute of the module where you're importing it.import module1 module1.a=3On the other hand, if a is shared by a whole lot of modules, put it somewhere else, and have everyone import it:global_module.py module1.py: import global_module def fun():     print global_module.var Other files: import global_module import module1 global_module.var = 3 module1.fun()Read More

How to import everything from a python namespace / package?

Sumana Challa
Updated on 18-Apr-2025 15:47:32

790 Views

What are Namespace Packages? Namespace packages are a special type of package that were introduced in Python 3.3. These allow you to split package contents across multiple directories. If you didn't include at least an empty _init_.py file in your package, then your package becomes a namespace package. Python supports three main types of namespace packages - Implicit namespace packages pkg_resources-style pkgutil-style Python Packages and Imports Python package is a directory containing a _init_.py file and one or more Python modules. When you import from a package, ... Read More

How do I import all the submodules of a Python namespace package?

Sumana Challa
Updated on 21-Apr-2025 16:06:42

912 Views

What are Namespace Packages? Namespace packages are a type of package in Python that allows you to split the sub-packages and modules within a single package across multiple, separate distribution packages. Unlike normal packages, the namespace packages don't require _init_.py file. Automatically importing all submodules within a namespace package serves several purposes, like auto-registration without manually importing, and to load all available plugins in a system. This article discusses the two methods that you can use to import all the sub-modules of a Python namespace package - Using pkgutil.iter_modules() Using ... Read More

How to create python namespace packages in Python 3?

Sumana Challa
Updated on 21-Apr-2025 16:20:21

2K+ Views

Namespace packages is a special package introduced in Python 3.3 that allows you to split package contents across multiple directories. If you didn't include at least an empty _init_.py file in your package, then your package becomes a namespace package. In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. Currently, there are three methods for developing namespace packages. These methods are mentioned below. Native namespace packages (PEP 420) pkgutil-style Namespace packages ... Read More

Do recursive functions in Python create a new namespace each time the function calls itself?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

181 Views

Yes, a function call (any function call, not just recursive ones) creates a new namespace. BUT, when given as parameters, OBJECTS are passed by reference.So, the new namespace get its own copy of this reference but it still refers to the same object as in the calling function, and if you change the content of that object, you will notice the change in the calling function.To be more specific, Whenever the Interpreter encounters a call to a function, its creates a frame object, which is pushed to a frame stack. Each time a frame is created, that frame is given ... Read More

What are Python namespaces all about?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

239 Views

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.Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global ... Read More

What does the if __name__ ==

Sarika Singh
Updated on 23-Aug-2023 12:53:52

83K+ Views

This article explains what the Python code expression if __name__ == '__main__' means. A Python programme uses the condition if __name__ == '__main__' to only run the code inside the if statement when the program is run directly by the Python interpreter. The code inside the if statement is not executed when the file's code is imported as a module. What is __main__? The word "__name__" denotes a unique variable in Python. Python has a large number of special variables that begin and end with double underscores. They are referred to as dunder to keep it brief (from Double Underscores). ... Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar
Updated on 01-Oct-2019 11:17:05

264 Views

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories Package-1 and Package-2 in PYTHONPATH, Package-1/namespace/__init__.py Package-1/namespace/module1/__init__.py Package-2/namespace/__init__.py Package-2/namespace/module2/__init__.py the end-user can import namespace.module1 and import namespace.module2.On Python 3.3, you don't have to do anything, just don't put any __init__.py in your namespace package directories and it will just work. This is because Python 3.3 introduces implicit namespace packages.On older versions, there's a standard module, called pkgutil, with which you can 'append' modules to a given namespace. You ... Read More

Advertisements