AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:46:46

3K+ Views

The __del__ is magic method in Python. The magic methods that allow us to do some pretty neat tricks in object-oriented programming. They are also called Dunder methods. These methods are identified by two underscores (__) used as prefix and suffix. In Python, we create an oject using the __new__() ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:45:56

290 Views

For this, at first understand what is __new__() method in Python. When subclassing an immutable type, override the __new__() method instead of the __init__() method. The __new__ method gets called when an object is created whereas the __init__ method will be called to initialize the object. These are magic methods. ... Read More

How do I cache method calls in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:40:15

3K+ Views

The two tools for caching methods are functools.cached_property() and functools.lru_cache(). Both the module is part of the functools module. The functools module is for higher-order functions: functions that act on or return other functions. Let us first install and import the functools module − Install functools To install the functools ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:39:36

218 Views

The id() method in Python return the identity of an object i.e. a unique id for the specified object. Now, you would be wondering, what is this id(). The id here is the object's memory address, an integer guaranteed to be unique and constant for this object during its lifetime. ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:38:33

109 Views

Example The is operator is an identity operator in Python. It tests for object identity. Let us see an example − x = ["Paul", "Mark"] y = ["Paul", "Mark"] z = x # Python IS operator print(x is z) Output True Let’s say we consider another example, ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:35:57

2K+ Views

Python includes the concept of static class data and static class methods. The static class data Here, define a class attribute for the static class data. Explicitly use the class name in the assignment, if you want to assign a new value to the attribute − class Demo: ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:35:18

373 Views

Before learning how to change the base class, let us first understand the concept of base and derived class in Python. We will learn about Base and Derived class using the concept of Inheritance. In multiple inheritance, the features of all the base classes are inherited into the derived class. ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:32:12

298 Views

To get int literal attribute instead of SyntaxError, use a space or parenthesis. The int literal is a part if Numeric Literals in Python. Numeric Literals also includes the following four different numerical types − int (signed integers) − They are often called just integers or ints, are positive ... Read More

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

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:30:57

383 Views

In Python, -22//10 returns -3 because of the concept of Floor Division i.e. Double Slash Operator. The // is the Double Slash i.e. Arithmetic Operator. Let us first understand about it. Floor Division in Python The division of operands where the result is the quotient in which the digits after ... Read More

How it is possible to write obfuscated oneliners in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 13:28:41

373 Views

Yes, it is possible to write obfuscated one-liners in Python using Lambda. Before moving further, let us first understand what are Lambdas in Python. Python Lambda The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the ... Read More

Advertisements