12

In MATLAB it is possible to create function handles with something like

myfun=@(arglist)body

This way you can create functions on the go without having to create M-files.

Is there an equivalent way in Python to declare functions and variables in one line and to call them later?

2
  • 1
    Are you looking for lambda? What's wrong with def? Commented Apr 21, 2011 at 11:14
  • Just to correct the nomenclature: A function handle and an anonymous function are two different things. A handle is a reference to a function. An anonymous function is a lambda, a function created on the spot that doesn't have a name (in MATLAB they must have a single statement). The only way to refer to an anonymous function is through a handle, so these sometimes get confused. Commented Nov 9, 2023 at 16:41

3 Answers 3

17

Python's lambda functions are somewhat similar:

In [1]: fn = lambda x: x**2 + 3*x - 4

In [2]: fn(3)
Out[2]: 14

However, you can achieve similar effects by simply defining fn() as a function:

In [1]: def fn(x):
   ...:   return x**2 + 3*x - 4
   ...: 

In [2]: fn(4)
Out[2]: 24

"Normal" (as opposed to lambda) functions are more flexible in that they allow conditional statements, loops etc.

There's no requirement to place functions inside dedicated files or anything else of that nature.

Lastly, functions in Python are first-class objects. This means, among other things, that you can pass them as arguments into other functions. This applies to both types of functions shown above.

Sign up to request clarification or add additional context in comments.

2 Comments

Function handles can also be passed as arguments in Matlab.
@MatlabSorter: In python a function (or a lambda) can be passed as an argument to another function. You don't need a "handle", you just pass the function itself.
15

This is not quite the full answer. In matlab, one can make a file called funct.m:

function funct(a,b)
   disp(a*b)
end

At the command line:

>> funct(2,3)
     6

Then, one can create a function handle such as:

>> myfunct = @(b)funct(10,b))

Then one can do:

   >> myfunct(3)
       30

A full answer would tell how to do this in python.

Here is how to do it:

def funct(a,b):
    print(a*b)

Then:

myfunct = lambda b: funct(10,b)

Finally:

>>> myfunct(3)
30

Comments

0

Turns out there is something that goes all the way back to 2.5 called function partials that are pretty much the exact analogy to function handles.

from functools import partial
def myfun(*args, first="first default", second="second default", third="third default"):
    for arg in args:
        print(arg)
    print("first: " + str(first))
    print("second: " + str(second))
    print("third: " + str(third))

mypart = partial(myfun, 1, 2, 3, first="partial first")

mypart(4, 5, second="new second")
1
2
3
4
5
first: partial first
second: new second
third: third default

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.