Server Side Programming Articles

Page 631 of 2109

How to get the class name of an instance in Python?

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

In Python object-oriented programming, getting the class name of an instance is a common requirement. Python provides several approaches to retrieve the class name from any object or instance. Using __class__.__name__ The most straightforward method uses the __class__ attribute combined with __name__ to get the class name ? class Animal: def __init__(self, name): self.name = name lion = Animal("Lion") print(lion.__class__.__name__) Animal The __class__ attribute returns the class object, while __name__ gives the actual class name as a string. ...

Read More

Explain Python class method chaining

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

In object-oriented programming, method chaining is a programming technique where multiple method calls are linked together in a single expression. Each method returns an object (typically self), allowing the next method to be called on the result. Method chaining provides two key benefits ? Reduced code length − No need to create intermediate variables for each step Improved readability − Operations flow sequentially in a clear, logical order How Method Chaining Works For method chaining to work, each method must return an object (usually self) so the next method can be called on it. ...

Read More

How do we reference Python class attributes?

Akshitha Mote
Akshitha Mote
Updated on 24-Mar-2026 975 Views

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 More

How to dynamically load a Python class?

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

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 More

How do we handle circular dependency between Python classes?

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

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 More

How many Python classes should I put in one file?

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

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 More

How do we use equivalence ("equality") operator in Python classes?

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

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 More

What is the difference between old style and new style classes in Python?

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

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 More

How does Python class inherit objects?

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

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 More

What is the meaning of single underscore prefix with Python variables?

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

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 More
Showing 6301–6310 of 21,090 articles
« Prev 1 629 630 631 632 633 2109 Next »
Advertisements