CPython is the default and most widely used interpreter or implementation of Python. When CPython exits, it attempts to clean up all allocated memory, but not all memory is freed due to several technical limitations. Why Memory Isn't Completely Freed Python is quite serious about cleaning up memory on exit and does try to destroy every single object, but unfortunately objects referenced from the global namespaces of Python modules are not always deallocated when Python exits. The main reasons are: Circular references − Objects that reference each other in a loop C library allocations − Memory ... Read More
Python lambda expressions cannot contain statements due to Python's syntactic framework limitations. Lambda functions are designed for simple expressions only, not complex logic with statements. What is a Lambda Function? A lambda function is an anonymous function that can be defined inline. Here's the syntax ? lambda arguments: expression Lambda functions accept one or more arguments but can only contain a single expression, not statements. Simple Lambda Example # Lambda function to print a string my_string = "Hello, World!" (lambda text: print(text))(my_string) Hello, World! Lambda with ... Read More
All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. However, Python doesn't have traditional "output parameters" like C++ or C#. Instead, we can achieve similar functionality through several approaches. You can achieve call-by-reference behavior in the following ways − Return a Tuple of the Results The most Pythonic approach is to return multiple values as a tuple ? # Function Definition def demo(val1, val2): val1 = 'new value' ... Read More
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance