definitions with lambda

Messages in english

Modérateur : xcasadmin

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

definitions with lambda

Message par compsystems » mer. mars 13, 2019 3:35 pm

Hello

Is it possible to define functions with lambda for a block of expressions?, currently only works for an expression


def add_(x, y): return x + y

add_ = lambda x, y : x + y # ok

now

Code : Tout sélectionner

def calculator( oper, a, b ):
    if "addition" == oper:
        return a + b
    elif "subtraction" == oper:
        return a - b
    elif "multiplication" == oper:
        return a * b        
    elif "division" == oper:
        return a / b
    else:
        return None
ok


but

Code : Tout sélectionner

calculator =  lambda oper, a, b:
    if "addition" == oper:
        return a + b
    elif "subtraction" == oper:
        return a - b
    elif "multiplication" == oper:
        return a * b        
    elif "division" == oper:
        return a / b
    else:
        return None
error

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: definitions with lambda

Message par parisse » mer. mars 13, 2019 6:42 pm

lambda is supported if the definition is on 1 line.

Répondre