Programming Articles - Page 343 of 3363

__subclasscheck__ and __subclasshook__ in Python

Tushar Sharma
Updated on 09-May-2023 15:48:36

1K+ Views

Python, a generally adaptable and effective programming language, has picked up significant popularity over a long time. The item-oriented nature of Python lets within the execution of a few high-quality capacities, such as inheritance and polymorphism. In this post, we will delve into two lesser-recognized, yet charming techniques that allow for custom-designed inheritance checks in Python: subclasscheck and subclasshook. What are Subclasscheck and Subclasshook? In Python, it is not unusual to determine if a class is a subclass of some other class by utilizing the built-in issubclass() function. By default, this function checks the inheritance tree to decide the connection ... Read More

__new__ in Python

Tushar Sharma
Updated on 09-May-2023 15:47:19

259 Views

Python, a versatile and broadly used programming language, is thought for its simplicity and widespread feature set. One of its lesser-regarded but powerful capabilities is the brand-new technique, which offers builders a unique way of customizing item introduction. This article presents a complete and engaging review of the new approach in Python, discussing its purpose, use instances and quality practices. Section 1: The Essence of New 1.1: Understanding New In Python, new is a special method that belongs to elegance rather than the example of a category. It is chargeable for creating and returning new times of the class. The ... Read More

__init_subclass__ in Python

SaiKrishna Tavva
Updated on 13-Nov-2024 13:49:50

3K+ Views

In Python __init_subclass__ method is a special method that allows us to initialize or customization of class creation particularly for subclasses. Key Characteristics of __init_subclass__ Some of the Key aspects of '__init_subclass__' method are as follows Automatic Invocation: The __init_subclass__ method is automatically called during the creation of a subclass and no need to manually invoke this method. Customizable via Parameters: To pass additional information or configuration options ... Read More

__init__ in Python

Tushar Sharma
Updated on 09-May-2023 15:45:34

608 Views

As we journey through the arena of Python, we regularly come across factors of the language that permits us to express our thoughts and intentions extra intuitively. One such detail is the init approach, a powerful constructor method that brings our custom training to existence. In this post, we'll explore the inner workings of Python's init method, illuminating its key position in initializing our custom instructions and respiratory lifestyles into our code. The Birth of Objects: The Init Method When we create a new instance of a custom class, Python invokes a unique method known as init to initialize the ... Read More

Swift Program to Sort a Dictionary By Values

Ankita Saini
Updated on 09-May-2023 17:18:18

948 Views

Swift supports a sorted() method to sort all the elements present in the given dictionary. This method sorts the key-value pairs of the dictionary according to the values. Syntax func sorted(by:) Here, the value of by parameters are − Greater than(>) − To sort the elements into descending order. Less than(

Swift Program to Search an Element in a Dictionary

Ankita Saini
Updated on 09-May-2023 11:51:09

3K+ Views

In a Swift dictionary, we can search for an element that is either a key or value with the help of contains() function. The contains() function will return a boolean value which represents whether the specified element(key or value) is found or not. Syntax dict.contains{$0.key == “KeyName”} Here, dict is the dictionary. The contains() function returns a boolean value which represents the result. Here we pass a closure in the function which will check if the current key is equal to the specified key or not and then return the result accordingly. Algorithm Step 1 − Create a ... Read More

Swift Program to Replace Elements in a Dictionary

Ankita Saini
Updated on 09-May-2023 11:49:30

384 Views

In Swift, a dictionary is an unordered collection of key-value pairs. So, to replace the value of a key Swift provides bracket notation or subscript notations. Using this notation we can also add new key-value pair in the dictionary if the specified pair does not exist. Syntax dict[keyName] = “Value” Here, dict is the dictionary. The keyName represents the key whose value you want to replace, and the Value represent the new value. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Display the original dictionary. Step 3 − Now replace the value of ... Read More

Swift Program to Remove Null from a Dictionary

Ankita Saini
Updated on 09-May-2023 11:43:32

870 Views

Sometimes a dictionary contains null values so to remove those null values Swift provides an in-built function named the filter() method. In the filter() method, we pass a closure which returns a boolean value indicating whether the key-value pair should be included in the resultant dictionary or not. Or we can say that if the second element(value) of a key-value pair is not nil, then it will be included in the resultant dictionary. Otherwise not. Syntax dict.filter{$0.1 != nil} Here, dict is the dictionary. The filter() function return key-value pairs which satisfy the given closure. The closure returns true ... Read More

Swift Program to Remove duplicate elements from Dictionary

Ankita Saini
Updated on 09-May-2023 11:41:07

907 Views

In Swift dictionary, we cannot have duplicate keys but can have duplicate values hence in this article we will remove duplicate values from the dictionary. So for that, we use contains() function. This function checks if the dictionary contains duplicate values or not. If the dictionary contains duplicate values, then it will remove them from the resultant dictionary. Syntax dict.values.contains(val) Here, dict is the resultant dictionary in which we will store unique key-value pairs and the contains() function compares two values with each other. Algorithm Step 1 − Create a dictionary named as myCityRank with key-value pairs. Step ... Read More

__getitem__ and __setitem__ in Python

Sarika Singh
Updated on 02-Sep-2025 13:16:24

4K+ Views

In Python, you can customize many operations like accessing or modifying elements of a list or dictionary using special methods. Two important methods that make your objects behave like lists or dictionaries are: __getitem__: called when you access an item using obj[key]. __setitem__: called when you assign a value using obj[key] = value. These are examples of dunder methods (short for "double underscore"). They are special methods in Python with names that begin and end with double underscores. When you use square bracket syntax on your object: obj[key] automatically triggers __getitem__ obj[key] = value automatically triggers __setitem__ ... Read More

Advertisements