Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 3 of 16

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

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 261 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

How data hiding works in Python Classes?

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

Data hiding in Python uses double underscores before attribute names to make them private or inaccessible from outside the class. This is achieved through name mangling, where Python internally changes the attribute name. Basic Data Hiding Example The following example shows how a variable with double underscores becomes hidden − class MyClass: __hiddenVar = 0 def add(self, increment): self.__hiddenVar += increment print(self.__hiddenVar) myObject = MyClass() myObject.add(3) ...

Read More

What does the cmp() function do in Python Object Oriented Programming?

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

The cmp() function was a built-in function in Python 2 that compared two objects and returned an integer indicating their relationship. It has been removed in Python 3, but understanding its behavior is useful for legacy code and implementing custom comparison logic. Syntax cmp(x, y) Return Values The cmp() function returns ? Negative number (-1) if x is less than y Zero (0) if x is equal to y Positive number (1) if x is greater than y Basic Examples Here are some examples showing how cmp() works with ...

Read More

How I can check if class attribute was defined or derived in given class in Python?

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

In Python, you can check if a class attribute was defined directly in a class or inherited from a parent class by examining the class's __dict__ attribute and using hasattr(). Understanding Class Attributes When a class inherits from another class, it can access parent class attributes. To distinguish between defined and inherited attributes, check if the attribute exists in the class's own __dict__. Example Let's create two classes to demonstrate defined vs inherited attributes ? class A: foo = 1 class B(A): pass # ...

Read More

How I can check if A is superclass of B in Python?

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

In Python, you can check if class A is a superclass of class B using the built-in issubclass() function or by examining the __bases__ attribute. Let's explore both methods with practical examples. Class Definition First, let's define our example classes where A is the parent class and B inherits from A ? class A(object): pass class B(A): pass Method 1: Using issubclass() The issubclass() function returns True if the first argument is a subclass of the second argument ? class A(object): ...

Read More

How does garbage collection work in Python?

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

Python automatically manages memory through garbage collection, which frees unused objects to prevent memory leaks. The garbage collector runs during program execution and is triggered when an object's reference count reaches zero. How Reference Counting Works Python tracks how many variables reference each object. When this count reaches zero, the object is automatically deleted ? # Reference counting example a = 40 # Create object , ref count = 1 b = a # Increase ref count of to 2 c = [b] ...

Read More

How do we access class attributes using dot operator in Python?

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

A class attribute is an attribute of the class rather than an attribute of an instance of the class. In Python, we can access class attributes using the dot operator (.) both through the class itself and through its instances. In the code below, class_var is a class attribute, and i_var is an instance attribute. All instances of the class have access to class_var, which can also be accessed as a property of the class itself ? Example class MyClass(object): class_var = 2 def __init__(self, i_var): ...

Read More

Explain the variables inside and outside of a class __init__() function in Python.

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

In Python, variables can be defined inside and outside the __init__() function of a class. Variables outside the __init__() function are called class variables, while variables inside the __init__() function are called instance variables. Understanding the difference is crucial for proper object-oriented programming. Class Variables vs Instance Variables Class variables are shared among all instances of a class, while instance variables are unique to each object. Let's examine this with a practical example − Example class MyClass: stat_elem = 456 # Class variable ...

Read More
Showing 21–30 of 160 articles
« Prev 1 2 3 4 5 16 Next »
Advertisements