Articles on Trending Technologies

Technical articles with clear explanations and examples

How to print to the Screen using Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 987 Views

The print() function is the standard way to display output to the screen in Python. It can handle various data types and offers flexible formatting options. Basic Print Statement In Python 3, the print function requires parentheses − print('Hello, world') Hello, world Printing Multiple Items To print multiple items on the same line separated by spaces, use commas between them − print('Hello, ', 'World') Hello, World Printing Different Data Types The print function can handle arbitrary data types in the same statement ...

Read More

How to generate XML documents with namespaces in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

Generating XML documents with namespaces in Python requires careful handling since the built-in xml.dom.minidom module has limited namespace support. While you can create namespaced elements, you need to manually add namespace declarations as attributes. Creating XML with Namespaces Using minidom The createElementNS() method creates an element with a namespace, but you must add the namespace declaration manually − import xml.dom.minidom doc = xml.dom.minidom.Document() element = doc.createElementNS('http://hello.world/ns', 'ex:el') element.setAttribute("xmlns:ex", "http://hello.world/ns") doc.appendChild(element) print(doc.toprettyxml()) Creating Complex XML with Multiple Namespaces For more complex XML documents with multiple namespaces and nested ...

Read More

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

Pranathi M
Pranathi M
Updated on 24-Mar-2026 1K+ Views

Python allows you to save definitions to a file and use them in scripts or interactive interpreter sessions. Understanding how modules, classes, and namespaces work together is essential for organizing and structuring Python code effectively. What are Modules? A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py added. Modules help organize code by grouping related functions and classes together. Creating a Simple Module Let's create a module called calculator.py ? # Save this as calculator.py def add(x, y): ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 1K+ Views

In Python, global variables are module-specific, meaning they exist within the scope of a single module rather than being shared across all modules like in C. Understanding this concept is crucial for managing data across multiple Python files. Global Variables Are Module-Specific When you define a global variable in a Python module, it's only accessible within that module. Each module maintains its own global namespace ? # module1.py (simulated) counter = 0 def increment(): global counter counter += 1 return counter print("Module1 ...

Read More

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

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 2K+ Views

Namespace packages are a type of package in Python that allows you to split sub-packages and modules within a single package across multiple, separate distribution packages. Unlike normal packages, namespace packages don't require an __init__.py file. Automatically importing all submodules within a namespace package serves several purposes, like auto-registration without manually importing, and loading all available plugins in a system. Using pkgutil.iter_modules() The pkgutil.iter_modules() function finds and lists submodules and subpackages within a given package. It returns an iterator containing ModuleInfo objects, each with information about a found module or package ? import pkgutil import ...

Read More

How to create python namespace packages in Python 3?

Sumana Challa
Sumana Challa
Updated on 24-Mar-2026 3K+ Views

Namespace packages are a special type of package introduced in Python 3.3 that allows you to split package contents across multiple directories. If you don'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: 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
Rajendra Dharmkar
Updated on 24-Mar-2026 307 Views

Yes, recursive functions in Python create a new namespace each time the function calls itself. This is true for any function call, not just recursive ones. However, when objects are passed as parameters, they are passed by reference. The new namespace gets its own copy of this reference, but it still refers to the same object as in the calling function. If you modify the content of that object, the change will be visible in the calling function. How Python Handles Function Calls When the Python interpreter encounters a function call, it creates a frame object that ...

Read More

What does the if __name__ ==

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 83K+ Views

This article explains what the Python expression if __name__ == '__main__' means and how it's used to control code execution. A Python program uses the condition if __name__ == '__main__' to run specific code only when the program is executed directly by the Python interpreter. The code inside the if statement is not executed when the file is imported as a module. Understanding the __name__ Variable The variable __name__ is a special built−in variable in Python. Python has many special variables that begin and end with double underscores, called "dunder" variables (from Double Underscores). In this case, ...

Read More

How to develop programs with Python Namespaced Packages?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 400 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 while maintaining a unified import structure. Directory Structure Example With the directories Package-1 and Package-2 in PYTHONPATH, you can organize your code as follows − Package-1/ namespace/ __init__.py module1/ __init__.py Package-2/ namespace/ ...

Read More

How to install and import Python modules at runtime?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

Python allows you to install and import modules at runtime using subprocess to call pip and importlib to dynamically import modules. This is useful for optional dependencies or when you want to handle missing packages gracefully. Modern Approach Using subprocess The recommended way to install packages programmatically is using subprocess instead of calling pip directly ? import subprocess import importlib import sys def install_and_import(package): try: return importlib.import_module(package) except ImportError: print(f"Installing {package}...") ...

Read More
Showing 7781–7790 of 61,299 articles
« Prev 1 777 778 779 780 781 6130 Next »
Advertisements