Found 10535 Articles for Python

How can a Python function return a function?

Sarika Singh
Updated on 14-Apr-2025 15:45:33

18K+ Views

In Python, functions are treated as first-class objects, which means that all functions in Python are treated like any other object or variable. You can assign a function to a variable, pass it as an argument to another function, return it from a function, or even store it in data structures like lists. One very useful feature in Python is that a function can return another function. This means you can define a function inside another function and return it as a result. This is used in advanced programming concepts like closures, decorators, and higher-order functions. Returning a Simple Function ... Read More

How to retrieve source code from Python objects?

Sarika Singh
Updated on 11-Apr-2025 14:50:14

15K+ Views

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: Retrieve Function Source Code This code defines a simple function my_function(), that accepts two arguments and returns the sum of those arguments. We then retrieve the source code of the my_function function using the inspect.getsource() function and store it in the source code variable. Finally, the source code is output to the console. The inspect.getsource() function operates by reading the function's source code from the file in which it ... Read More

How to use a global variable in a Python function?

Sarika Singh
Updated on 11-Apr-2025 14:55:53

4K+ Views

We can use a global variable inside a Python function by declaring it using the global keyword. This allows us to read or modify a variable that is defined outside the function's local scope. By default, any variable you create or change inside a function is considered a local variable (only used inside that function). If we try to change a global variable without declaring it as global, Python will create a new local variable with the same name, and the actual global variable will remain unchanged. In this article, we will understand what global variables are, how to use ... Read More

How can we overload a Python function?

Sarika Singh
Updated on 11-Apr-2025 14:32:32

4K+ Views

Function overloading means defining multiple functions with the same name but different arguments. This is a built-in feature in many programming languages like C++ or Java. However, in Python, function overloading is not directly supported, but we can achieve it using some techniques. Python uses a concept called dynamic typing, which allows us to define functions that can take different types and numbers of arguments. We cannot create multiple functions with the same name, but we can still achieve similar functionality using methods like default arguments, variable-length arguments, or special modules like functools.singledispatch. Using Default Arguments One way to overload ... Read More

How can we create recursive functions in Python?

Sarika Singh
Updated on 11-Apr-2025 14:30:18

4K+ Views

Recursion is a process where a function calls itself repeatedly with new input values, slowly moving toward a condition where it stops. This stopping condition is called the base case. It prevents the function from running endlessly and allows it to return a final result. In Python, we can create recursive functions by simply defining a function that calls itself. Every recursive function has two main components − Recursive Case: This is when the function calls itself to solve a smaller part of the problem. It keeps the process going. Base ... Read More

Advertisements