Found 10805 Articles for Python

How can a Python function return a function?

Rajendra Dharmkar
Updated on 31-Aug-2023 02:34:02

14K+ Views

Python supports first-class functions. In fact, all functions in python are first- class functions. Python may return functions from functions, store functions in collections such as lists and generally treat them as you would any variable or object. A function can return a function in Python because functions are treated as first-class objects. This means that you can assign a function to a variable, pass it as an argument to another function, or use it as a return value in a function. Defining functions in other functions and returning functions are all possible. In this code, the outer function defines ... Read More

How to retrieve source code from Python objects?

Rajendra Dharmkar
Updated on 31-Aug-2023 02:44:11

12K+ 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 import inspect def my_function(x, y): return x + y source_code = inspect.getsource(my_function) print(source_code) Output def my_function(x, y): return x + y 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. ... Read More

How to use a global variable in a Python function?

Tarun Chandra
Updated on 31-Aug-2023 02:48:43

3K+ Views

There are 2 types of variables in Python namely Local variables and Global variables. Local variables mean the variables that are declared inside a function or inside a method whose impact or scope is only present inside that specific block and it doesn't affect the program outside that block. Global variables mean the variables that are declared outside any function or method and these variables have an impact or scope through the entire program. We can also instantiate global variables inside a function by using a global keyword, if we want to declare global variables outside a function then we ... Read More

How can we overload a Python function?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:15:57

3K+ Views

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 class with one method, sayHello(). We rewrite as shown below. The first parameter of this method is set to None, which gives us the option to call it with or without a parameter. An object is created based on the class, and we call its method using zero and one parameter. To ... Read More

How can we create recursive functions in Python?

Rajendra Dharmkar
Updated on 28-Sep-2023 01:19:18

3K+ Views

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 before it can be used in a program. It terminates, if, with every recursive call, the solution of the problem becomes smaller and moves towards a base case, where the problem can be solved without further recursion. A recursion can lead to an infinite loop, if the base case is not ... Read More

Advertisements