Rajendra Dharmkar has Published 540 Articles

How to access Python objects within objects in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 31-Mar-2023 16:48:16

In Python, it's common to have objects within objects. To access objects within objects, you can use the dot notation. Let's take a look at an example. Example 1 class Person: def __init__(self, name, age): self.name ... Read More

Why and how are Python functions hashable?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 31-Mar-2023 16:45:46

An object is said to be hashable if it has a hash value that remains the same during its lifetime. It has a __hash__() method and it can be compared to other objects. For this, it needs the __eq__() or __cmp__()method. If hashable objects are equal when compared, then they ... Read More

How to retrieve source code from Python objects?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 31-Mar-2023 16:05:19

Using the inspect module, you can retrieve the source code of a Python function, method, class, or module. The following example demonstrates how to retrieve a function's source code: Example import inspect def my_function(x, y): return x + y source_code = inspect.getsource(my_function) print(source_code) ... Read More

What does built-in class attribute __bases__ do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 29-Mar-2023 17:45:16

In Python, the built-in class attribute __bases__ is used to get the tuple of base classes of a class. When you define a new class in Python, you can specify one or more base classes from which the new class will inherit. The base classes are specified in parentheses after ... Read More

What are base overloading methods in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 29-Mar-2023 17:36:50

In Python, base overloading methods are the methods that we can override in the derived class to change the behavior of the object. The base overloading methods are pre-defined special methods in Python that can be used to define how operators and built-in functions should work on user-defined classes. By ... Read More

What does built-in class attribute __module__ do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Mar-2023 17:20:23

The __module__ attribute is a built-in class attribute in Python that is used to get the name of the module in which a class is defined. This attribute is useful when you want to check the name of the module in which a particular class is defined. Here are some ... Read More

How to get a list of parameter names inside Python function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Mar-2023 19:36:05

To extract the number and names of the arguments from a function or function[something] to return ("arg1", "arg2"), we use the inspect module. The given code is written as follows using inspect module to find the parameters inside the functions `aMethod` and `foo`. Example import inspect ... Read More

How can we create recursive functions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Mar-2023 19:15:25

Recursion is a programming technique, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition follows recursion, we call this function a recursive function. A recursive function has to be terminated ... Read More

How can we overload a Python function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Mar-2023 18:55:19

In Python, you can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two or more parameters. This is known as method overloading. . In the given code, there is a ... Read More

How can we define a Python function at runtime?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Mar-2023 18:21:57

We can define a python function and execute it at runtime by importing the types module and using its function types.FunctionType() as follows This code works at the python prompt as shown. First we import the types module. Then we run the command dynf=…; then we call ... Read More

1 2 3 4 5 ... 54 Next
Advertisements