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
Programming Articles
Page 643 of 2547
How 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 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 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 More