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 Rajendra Dharmkar
Page 3 of 16
How 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 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 MoreHow data hiding works in Python Classes?
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 MoreWhat does the cmp() function do in Python Object Oriented Programming?
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 MoreHow I can check if class attribute was defined or derived in given class in Python?
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 MoreHow I can check if A is superclass of B in Python?
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 MoreHow does garbage collection work in Python?
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 MoreHow do we access class attributes using dot operator in Python?
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 MoreExplain the variables inside and outside of a class __init__() function in Python.
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