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 on Trending Technologies
Technical articles with clear explanations and examples
How do we reference Python class attributes?
In Python, the variables that are declared inside a class are known as attributes. These attributes can be accessed and modified by both the class and its objects. There are two types of attributes ? Class Attributes: Variables defined at the class level that are shared among all instances of the class. They can be accessed using the class name or instance objects. Instance Attributes: Variables defined inside the __init__() method or other instance methods that are specific to each object. They are accessed using the object name. Accessing Class ...
Read MoreHow 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 to use enums in Python classes?
Python's enum module provides a way to create enumerated constants within classes. Enums are useful for representing a fixed set of constants like car brands, colors, or status codes. Creating an Enum Class To create an enum, inherit from enum.Enum and define class attributes as constants − import enum class Car(enum.Enum): SUZUKI = 1 HYUNDAI = 2 DEZIRE = 3 print("All the enum values are:") for car in Car: print(car) All the enum values are: ...
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 many Python classes should I put in one file?
Python code is organized in files called "modules" and groups of related modules called "packages". The question of how many classes to put in one file is a common organizational concern that affects code maintainability and readability. Understanding Python Modules A module is a distinct unit that may have one or more closely-related classes. Modules need to be imported before they are read, used, maintained and extended if needed. So a module is a unit of reuse. The rule is this: a module is the unit of reuse. Everything in Python libraries and other Python applications is ...
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 difference between old style and new style classes in Python?
In Python, there are two styles of classes that differ based on their inheritance from built-in types. Understanding this distinction is important when working with Python 2.x code or legacy systems. Old Style Classes Old style classes (also called "classic" classes) have no built-in type as a base class. They were the default in Python 2.x when no explicit inheritance was specified − class OldSpam: # no base class pass print(OldSpam.__bases__) () New Style Classes New style classes have a built-in type ...
Read MoreHow does Python class inherit objects?
In Python, all classes inherit from the base object class, but the syntax and behavior differ between Python 2.x and Python 3.x. Understanding this inheritance is crucial for working with modern Python code. Python 2.x Class Styles Python 2.x had two distinct class styles depending on inheritance ? Old Style Classes These classes have no built-in type as a base class ? # This example shows Python 2 syntax (for reference only) class OldFoo: # no base class pass print(OldFoo.__bases__) () ...
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 More