Python Articles

Page 3 of 854

How to run Python functions from command line?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 2K+ Views

To run Python functions from the command line, you save the function in a .py file and then invoke it using the Python interpreter. There are several approaches − using sys.argv, the -c flag, or the argparse module. Using sys.argv with __name__ Guard The most common approach is to use sys.argv to read command-line arguments and the if __name__ == "__main__" guard to call your function when the script is executed directly. The first item in sys.argv is the script name, and subsequent items are the arguments passed. Note that all command-line arguments are received as strings, so you must ...

Read More

Are Python functions objects?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 425 Views

Yes, Python functions are full objects. Python creates function objects when you use a def statement or a lambda expression. Like any other object, functions can be assigned to variables, passed as arguments, returned from other functions, and even have custom attributes. Functions Have a Type and Support Attributes Since functions are objects, they have a type (function) and you can assign custom attributes to them ? Example def foo(): pass foo.score = 20 print(type(foo)) print(foo.score) print(type(lambda x: x)) The output of the above code is ? 20 ...

Read More

How can a Python function return a function?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 19K+ 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 can we define a Python function at runtime?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 107 Views

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 More

What are Python function attributes?

Tarun Chandra
Tarun Chandra
Updated on 12-Mar-2026 7 Views

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 More

How can we create recursive functions in Python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 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 Case: This is the stopping point. It tells ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 3K+ Views

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 More

How to wrap python object in C/C++?

Gireesha Devara
Gireesha Devara
Updated on 12-Mar-2026 651 Views

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 More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 374 Views

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 More

Maximum Nesting Depth of Two Valid Parentheses Strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 386 Views

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 More
Showing 21–30 of 8,532 articles
« Prev 1 2 3 4 5 854 Next »
Advertisements