Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Sarika Singh
Page 4 of 15
How to dynamically load a Python class?
Dynamic loading refers to importing and instantiating Python classes at runtime using their string names. This is useful when you don't know which class to use until the program is running, such as in plugin systems or configuration-driven applications. Basic Class Definition Before exploring dynamic loading, let's understand what a class is. A class is a blueprint for creating objects with specific attributes and methods ? class Calculator: def __init__(self, name): self.name = name def ...
Read MoreHow do we handle circular dependency between Python classes?
In this article we are going to discuss how to handle the circular dependency between Python classes. First of all, let us understand what is circular dependency. When two or more modules depend on one another, this is known as a circular dependency. This is because each module is defined in terms of the other module. What is Circular Dependency? Following is an example of circular dependency ? functionE(): functionF() And functionF(): functionE() The code shown above clearly shows a circular dependency. FunctionE() ...
Read MoreHow do we use equivalence ("equality") operator in Python classes?
Using comparison operators, we may compare various data types in Python. When creating custom classes, we are unable to simply compare them using the comparison operators. This article will go over various approaches to verify equivalence ("equality") in Python classes. Equality of Class Objects The == operator makes it simple to determine whether two built-in objects, such as strings or integers, are equal. This is demonstrated in the example below. Example Following is an example of == operator − char1 = 365 char2 = 83 result = char1 == char2 print("{} and {} ...
Read MoreWhat is the meaning of single underscore prefix with Python variables?
In Python, a single underscore prefix (_variable) serves as a convention to indicate that a variable is intended for internal use. This is known as a "weak private" indicator that signals to other developers that the variable should be treated as an implementation detail. Syntax The syntax for single underscore prefix variables is ? _variable_name Single Underscore in Classes When used in classes, single underscore variables are accessible but indicate internal use ? class Python: def __init__(self): self.public_var = ...
Read MoreWhat does double underscore prefix do in Python variables?
In Python, a double underscore prefix (__) triggers name mangling, which automatically transforms the attribute name to include the class name. This mechanism prevents naming conflicts in inheritance hierarchies. What is Name Mangling? When you prefix a variable with double underscores, Python automatically renames it by adding _ClassName before the original name. This makes the attribute "private" to that specific class. class Python: def __init__(self): self.car = 5 # Normal attribute ...
Read MoreHow do I make a subclass from a super class in Python?
In this article we are going to discuss how to create subclass from a super class in Python. Before proceeding further let us understand what is a class and a super class. A class is a user-defined template or prototype from which objects are made. Classes offer a way to bundle together functionality and data. The ability to create new instances of an object type is made possible by the production of a new class. Each instance of a class may have attributes connected to it to preserve its state. Class instances may also contain methods for changing ...
Read MoreHow to destroy an object in Python?
When an object is deleted or destroyed, a destructor is invoked. Before terminating an object, cleanup tasks like closing database connections or file handles are completed using the destructor. The garbage collector in Python manages memory automatically. For instance, when an object is no longer relevant, it clears the memory. In Python, the destructor is entirely automatic and never called manually. In the following two scenarios, the destructor is called − When an object is no longer relevant or it goes out of scope The object's reference counter reaches zero ...
Read MoreWhat are different data conversion methods in Python?
Data conversion in Python refers to changing the data type of a value to another data type. Python supports two different types of data conversion: Implicit conversion and Explicit conversion. Implicit Conversion is performed automatically by the Python interpreter. Explicit Conversion is manually done by the programmer using built-in functions. Implicit Conversion Python automatically converts one data type to another to avoid data loss during operations. The smaller data type is converted to a higher data type ? Example The following example shows how Python ...
Read MoreHow to create a zip file using Python?
ZIP is a widely used file format that compresses one or more files into a single archive file. This file format helps to reduce storage space and simplifies sharing or save backup of large data files. ZIP archives usually have a .zip file extension and are commonly used across different platforms. Compressed ZIP files reduce the size of the original directory by applying a compression algorithm. This results in faster file sharing over a network as the size of the ZIP file is significantly smaller than the original file. Python Modules for Creating ZIP Files In Python, ...
Read MoreHow to create a tar file using Python?
TAR stands for Tape Archive Files. Tar files are archive files that allow us to store multiple files and directories in a single file. Open-source software is commonly distributed using tar files. Tar files typically end in .tar and can be compressed using tools such as gzip, resulting in files ending with .tar.gz. File Modes for Creating Tar Files Here are the available file modes to create tar files using Python's tarfile module ? "w": Write a tar archive without compression. "w:gz": Write a gzip-compressed archive. "w:bz2": Write a ...
Read More