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 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 does the destructor method __del__() work in Python?
The __del__() method in Python is a special method called a destructor. It is automatically called when an object is about to be destroyed or garbage collected, allowing you to perform cleanup operations before the object is removed from memory. Basic Syntax The destructor method follows this syntax − class MyClass: def __init__(self, name): self.name = name print(f"Object {self.name} created") def __del__(self): ...
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 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 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 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 MoreHow to pop-up the first element from a Python tuple?
Python tuples are immutable, meaning you cannot directly remove elements from them. However, you can achieve the effect of "popping" the first element by converting the tuple to a list, removing the element, and converting back to a tuple. Method 1: Using list() and pop() Convert the tuple to a list, use pop(0) to remove the first element, then convert back to a tuple ? # Original tuple T1 = (1, 2, 3, 4) print("Original tuple:", T1) # Convert to list, pop first element, convert back L1 = list(T1) first_element = L1.pop(0) T1 = tuple(L1) ...
Read More