Conditional statement in a one line lambda function in python?

后端 未结 6 1596
借酒劲吻你
借酒劲吻你 2020-12-12 22:45

Apologies if this has been asked before, but I couldn\'t see it anywhere.

Essentially I\'ve come across a scenario where i need to make use of an if statement inside

相关标签:
6条回答
  • 2020-12-12 23:06

    I found I COULD use "if-then" statements in a lambda. For instance:

    eval_op = {
        '|'  : lambda x,y: eval(y) if (eval(x)==0) else eval(x),
        '&'  : lambda x,y: 0 if (eval(x)==0) else eval(y),
        '<'  : lambda x,y: 1 if (eval(x)<eval(y)) else 0,
        '>'  : lambda x,y: 1 if (eval(x)>eval(y)) else 0,
    }
    
    0 讨论(0)
  • 2020-12-12 23:07

    By the time you say rate = lambda whatever... you've defeated the point of lambda and should just define a function. But, if you want a lambda, you can use 'and' and 'or'

    lambda(T): (T>200) and (200*exp(-T)) or (400*exp(-T))
    
    0 讨论(0)
  • 2020-12-12 23:09

    The right way to do this is simple:

    def rate(T):
        if (T > 200):
            return 200*exp(-T)
        else:
            return 400*exp(-T)
    

    There is absolutely no advantage to using lambda here. The only thing lambda is good for is allowing you to create anonymous functions and use them in an expression (as opposed to a statement). If you immediately assign the lambda to a variable, it's no longer anonymous, and it's used in a statement, so you're just making your code less readable for no reason.

    The rate function defined this way can be stored in an array, passed around, called, etc. in exactly the same way a lambda function could. It'll be exactly the same (except a bit easier to debug, introspect, etc.).


    From a comment:

    Well the function needed to fit in one line, which i didn't think you could do with a named function?

    I can't imagine any good reason why the function would ever need to fit in one line. But sure, you can do that with a named function. Try this in your interpreter:

    >>> def foo(x): return x + 1
    

    Also these functions are stored as strings which are then evaluated using "eval" which i wasn't sure how to do with regular functions.

    Again, while it's hard to be 100% sure without any clue as to why why you're doing this, I'm at least 99% sure that you have no reason or a bad reason for this. Almost any time you think you want to pass Python functions around as strings and call eval so you can use them, you actually just want to pass Python functions around as functions and use them as functions.

    But on the off chance that this really is what you need here: Just use exec instead of eval.

    You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:

    exec(my_function_string, my_globals, my_locals)
    

    In 2.7, exec is a statement, not a function—but you can still write it in the same syntax as in 3.x (as long as you don't try to assign the return value to anything) and it works.

    In earlier 2.x (before 2.6, I think?) you have to do it like this instead:

    exec my_function_string in my_globals, my_locals
    
    0 讨论(0)
  • 2020-12-12 23:14

    Yes, you can use the shorthand syntax for if statements.

    rate = lambda(t): (200 * exp(-t)) if t > 200 else (400 * exp(-t))
    

    Note that you don't use explicit return statements inlambdas either.

    0 讨论(0)
  • 2020-12-12 23:20

    Use the exp1 if cond else exp2 syntax.

    rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)
    

    Note you don't use return in lambda expressions.

    0 讨论(0)
  • 2020-12-12 23:22

    In case you want to be lazier:

    #syntax lambda x : (false,true)[Condition]
    

    In your case:

    rate = lambda(T) : (400*exp(-T),200*exp(-T))[T>200]
    
    0 讨论(0)
提交回复
热议问题