Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 3 of 854
How can a Python function return a function?
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 MoreHow can we define a Python function at runtime?
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 the function dynf() to get the output as shown import types dynf = types.FunctionType(compile('print("Really Works")', 'dyn.py', 'exec'), {}) dynf() Output Really Works Defining a Python function at runtime can be useful in a variety of situations, such as when you need to ...
Read MoreWhat are Python function attributes?
Python is made up of objects but can also be used in other ways. Similarly, functions are also made up of objects, since they are objects they have attributes like identifier, title, inputs, outputs, etc. The value of an attribute cannot be changed once it has been set. To set the value of an attribute we use a function setattr and to know what value has been set as the attribute we use a function getattr. To access the attributes of a function we use the ‘.’ Operator. Some of the attributes have a special functionality such that they can ...
Read MoreHow can we create recursive functions in Python?
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 Case: This is the stopping point. It tells ...
Read MoreHow to get a list of parameter names inside Python function?
To get a list of parameter names inside a Python function, you can use the inspect module. This module provides several functions that let you examine the properties of Python objects, including function signatures, parameter names, and default values. Using inspect.getfullargspec() The inspect.getfullargspec() function returns a named tuple containing information about a function's parameters, including argument names, variable args, keyword args, and default values ? Example import inspect def aMethod(arg1, arg2): pass print(inspect.getfullargspec(aMethod)) def foo(a, b, c=4, *arglist, **keywords): pass print(inspect.getfullargspec(foo)) The output of the above ...
Read MoreHow to wrap python object in C/C++?
To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) { ...
Read MoreNumber of Equivalent Domino Pairs in Python
Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0
Read MoreMaximum Nesting Depth of Two Valid Parentheses Strings in Python
Suppose we have a string, that string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and it satisfies these properties −It is the empty string, orIt can be written as AB, where A and B are VPS's, orIt can be written as (A), where A is a VPS.We can also define the nesting depth depth(S) of any VPS S like below −depth("") = 0depth(A + B) = max of depth(A), depth(B), where A and B are VPS'sdepth("(" + A + ")") = 1 + depth(A), where A is a ...
Read MoreProgram to check whether one value is present in BST or not in Python
Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.So, if the input is likeval = 7, then the output will be True, as 7 is present in the tree.To solve this, we will follow these steps−Define a function solve() . This will take root, valif root is null, thenreturn Falseif data of root is same as val, thenreturn Trueif data of root < val, thenreturn solve(left of root, val)return solve(right of root, val)Let us see the following implementation to get better understanding−class TreeNode: ...
Read MoreProgram to find contiguous intervals of a unique array in Python
Suppose we have a list of unique numbers called nums. We have to find a sorted 2D matrix of numbers where each list represents an inclusive interval summarizing number that are contiguous in nums.So, if the input is like nums = [10, 11, 12, 15, 16, 17, 28, 30], then the output will be [[10, 12], [15, 17], [28, 28], [30, 30]], as in the list [10 to 12], [15 to 17] are contiguous, and 28 and 30 are there, they are represented as [28 to 28] and [30 to 30].To solve this, we will follow these steps−sort the list ...
Read More