Python Articles

Page 200 of 855

My Python class defines __del__ but it is not called when I delete the object

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

The __del__ is a magic method in Python that serves as a destructor. Magic methods (also called Dunder methods) are identified by double underscores (__) as prefix and suffix. In Python, we create an object using __new__() and initialize using __init__(). However, to destruct an object, we have __del__(). Sometimes __del__() may not be called when you expect it to be. Basic Example Let us create and delete an object ? class Demo: def __init__(self): print("We are creating an object.") ...

Read More

How can a Python subclass control what data is stored in an immutable instance?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 341 Views

When subclassing an immutable type like int, str, or tuple, you need to override the __new__() method instead of __init__() to control what data gets stored in the instance. The __new__ method gets called when an object is created, whereas __init__() initializes the object after creation. For immutable types, the data must be set during object creation, not initialization. Understanding Magic Methods Magic methods (also called dunder methods) are identified by double underscores as prefix and suffix. They allow us to customize object behavior in Python ? print(dir(int)) ['__abs__', '__add__', '__and__', '__bool__', ...

Read More

Why does the result of id() appear to be not unique in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 279 Views

The id() function in Python returns the identity of an object — a unique identifier that represents the object's memory address. While this ID is guaranteed to be unique during an object's lifetime, you might notice that id() values can appear to repeat across different program runs or even within the same program under certain conditions. The key insight is that two objects with non-overlapping lifetimes may have the same id() value. This happens because Python's memory manager can reuse memory addresses after objects are garbage collected. Syntax id(object) The parameter can be any ...

Read More

When can I rely on identity tests with the is operator in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 150 Views

The is operator in Python tests for object identity, not equality. You can rely on identity tests in specific circumstances where object identity is guaranteed. Understanding the is Operator The is operator compares object identities using their memory addresses. The test a is b is equivalent to id(a) == id(b). x = ["Paul", "Mark"] y = ["Paul", "Mark"] z = x # Identity test print("x is z:", x is z) print("x is y:", x is y) # Verify with id() print("id(x):", id(x)) print("id(z):", id(z)) print("id(y):", id(y)) x is z: True x ...

Read More

How do I create static class data and static class methods in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

Python provides support for static class data (class attributes) and static class methods through class variables and the @staticmethod decorator. Static Class Data Class attributes are shared among all instances of the class. Define them at the class level and access them using the class name ? class Demo: count = 0 # Static class data def __init__(self): Demo.count = Demo.count + 1 def getcount(self): ...

Read More

How can I organize my Python code to make it easier to change the base class?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 428 Views

When working with inheritance in Python, you might need to change which base class your derived class inherits from. Python provides several organizational patterns to make this easier to manage. Understanding Base and Derived Classes In Python inheritance, a base class (parent/super class) provides functionality that a derived class (child/sub class) inherits. Let's see the basic syntax − class Base: # Body of the class pass class Derived(Base): # Body of the class pass Multiple Inheritance Syntax ...

Read More

How do I get int literal attribute instead of SyntaxError in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 387 Views

When trying to access attributes of integer literals directly in Python, you might encounter a SyntaxError: invalid decimal literal. This happens because Python's parser interprets the dot as part of a float literal. To fix this, use a space before the dot or wrap the integer in parentheses. Understanding Numeric Literals Python supports several numeric literal types: int (signed integers) − Positive or negative whole numbers with no decimal point float (floating point real values) − Real numbers with a decimal point, may use scientific notation complex (complex numbers) − Numbers of the form a + ...

Read More

Why does -22 // 10 return -3 in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 480 Views

In Python, -22 // 10 returns -3 because of how floor division works with negative numbers. The // operator always rounds down toward negative infinity, not toward zero. Understanding Floor Division Floor division (//) returns the largest integer less than or equal to the division result. For positive numbers, this behaves as expected, but with negative numbers, it rounds away from zero toward negative infinity. Syntax a // b Where a and b are the dividend and divisor respectively. Floor Division with Positive Numbers a = 22 b = 10 ...

Read More

How it is possible to write obfuscated oneliners in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 432 Views

Python allows writing obfuscated one-liners using lambda functions, functional programming constructs, and advanced techniques. These create compact but hard-to-read code that can perform complex operations in a single line. Understanding Lambda Functions Lambda expressions define anonymous functions without a name. The syntax is ? lambda arguments: expression A lambda can have multiple arguments but only one expression. Simple Lambda Example message = "Hello World!" (lambda text: print(text))(message) Hello World! Using functools.reduce for Complex Operations The reduce() function from functools applies a function cumulatively to items ...

Read More

Is there an equivalent of C's "?:" ternary operator in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 540 Views

Yes, Python has an equivalent to C's ternary operator ?:. Python's conditional expression provides a concise way to choose between two values based on a condition. C Language Ternary Operator First, let's see how C's ternary operator works − #include int main() { int x = 10; int y; y = (x == 1) ? 20 : 30; printf("Value of y = %d", y); y = (x == 10) ? 20 : 30; printf("Value ...

Read More
Showing 1991–2000 of 8,549 articles
« Prev 1 198 199 200 201 202 855 Next »
Advertisements