Backslashes in Python regular expressions (regex) are used to define special sequences and escape characters. As backslashes are also used in Python strings, we need to be careful while using them. How Backslashes Work Now let us see how backslashes work in Python regular expressions - Escaping Characters: Some characters have specific meanings in regex (such as . or *). To treat them as normal characters, use a backslash (\.). ... Read More
This article gives a guide on the difference between re.findall() and re.finditer() methods available in Python. The re module of Python helps in working with regular expressions. Both the re.findall() and re.finditer() methods are used to search for patterns, but they behave differently. Let us see the differences with simple explanations and examples in this article. Using re.findall() Method The re.findall() helps to get a list of all matching patterns. It searches from the start or end of the given string. If we use the findall() method to search for a pattern in a given string, it will return all occurrences ... Read More
Python offers many built-in data structures like lists, tuples, sets, and dictionaries, which are used to store and manage data easily. In this article, we will discuss the difference between a Python tuple and a dictionary. Tuple Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they are immutable. We can't change the elements of a tuple, but we can execute a variety of actions on them, such as count, index, type, etc. Tuples are created in Python by placing a sequence of values separated by a 'comma', with or ... Read More
The functors are the function objects in C++. The functor allows an instance object of some class to be called as if it were an ordinary function. Let us consider a function that takes one argument. We can use this function as function object to do some task on a set of data. The Functors are widely used in STL algorithms like transform(), sort(), etc. Functor vs Regular Function: Need of Functor? Imagine we have a function that takes only one argument, like this: int increment(int x) { return x + 1; } Now, what ... Read More
The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to Initialize one object from another of the same type. Copy an object to pass it as an argument to a function. Copy an object to return it from a function. If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer variables and has some dynamic ... Read More
When something goes wrong in Python, it shows a message called exception text that explains the error. You can save and use this text in your program to help with logging or fixing the problem later. You can get Python exception text using str() function, traceback module, logging, or the args attribute. This allows you to log, debug, or display user-friendly error messages in your programs. Using str() Function The easiest way to get the exception text is by converting the exception object to a string using the str() function. This returns the error message associated with the exception. Example ... Read More
In Python, exceptions are not just error messages; they are actual objects. Understanding that exceptions are objects helps you work with them more effectively, such as accessing their attributes or creating custom exceptions. What do We mean by Exception is an Object? When an exception occurs, Python creates an instance of an exception class. This instance contains information about the error, like its type, message, and traceback. Since exceptions are objects, you can interact with them just like any other Python object. Example: Catching an exception object In the following example, we catch a ZeroDivisionError and assign it to a ... Read More
In Python, you can create your own custom exceptions by defining a new class that inherits from the built-in Exception class (or one of its subclasses). This allows you to raise meaningful errors specific to your application's needs. Basic Custom Exception Custom exceptions make your code easy to understand and handle errors better by clearly showing different types of errors. They help you to find and fix issues more quickly, especially in bigger and more complex programs. Example: Basic custom exception declaration In the following example, we define a simple custom exception class named "MyCustomErro" by subclassing Exception - class ... Read More
In Python 3, exception chaining allows one exception to be raised while preserving the context of the original exception. This provides a complete track, which makes it easy to understand how an error occurred during debugging. Python supports exception chaining explicitly using the raise ... from ... statement, or implicitly when a new exception is raised while handling another. Using raise ... from ... Statement This is the standard way to explicitly chain exceptions. The second exception is raised with a reference to the original exception using the from keyword, which helps to track the cause of the error. Example: ... Read More
Whenever Python comes across a variable or name that is not defined in the local or global namespace, it raises a NameError. This helps in debugging and ensures that variables are properly declared before using it. There are various ways to catch and handle a NameError in Python. The most common method is using a try-except block. Below are different approaches to catch a NameError exception - Using try-except block with NameError Using try-except-else block Using try-except-finally block Using try-except Block with NameError In Python, you ... Read More