0

I would like to use a lambda function to return values. This could be handled in a function, however would prefer the use of lambda for this exercise. In general, I want to have the lambda return either a date or 0 by passing a second parameter.

import datetime
none_func=lambda x: 0 if x is None else x
print(none_func(None))
print(none_func(7))
print(none_func(datetime.datetime(2020,1,1)))
### Here, update the lambda function to take an additional parameters, like returns a date if None as well
none_func=lambda x: datetime.datetime(1900,1,1) if x is None else x
print(none_func(None))
print(none_func(7))

Updated for clarification:

I would like to convert the coalesce function to a lambda.

def coalesce(x,return_schema='int'):
    if x is None and return_schema=='int': return 0
    elif x is None and return_schema=='date': return datetime.datetime(1900,1,1)
    else: return x

print(coalesce(7))
print(coalesce(None))
print(coalesce(None,return_schema='date'))
print(coalesce(datetime.datetime.today(),return_schema='date'))
kdb
  • 123
  • 1
  • 7
  • Lambdas can take multiple parameters, what is the problem? – dspencer Mar 12 '20 at 07:39
  • What issue are you facing? What is your expected output? – Sayandip Dutta Mar 12 '20 at 07:39
  • 1
    I think your main question is how to write a lambda expression with optional parameters. Am I right? – SomethingSomething Mar 12 '20 at 07:52
  • 1
    Surprisingly, it is possible: `>>> f = lambda a, b=0 : a + b ;>>> f(5) ;5 >>> f(5, 2) ;7 ` – SomethingSomething Mar 12 '20 at 07:55
  • You have the technical answer below... but it's totally unpythonic - it's using a lambda for no good reason (lambdas are supposed to be _anonymous_ functions) and turns a simple readable code into a complex one-liner that requires twice much effort to read and understand. – bruno desthuilliers Mar 12 '20 at 08:10
  • I agree with you. Later to realize its best just to use a defined function instead. For the most part, I was trying to understand how to use a lambda to replace it, however, as you pointed out a labda in this instance would make for a difficult read. – kdb Mar 12 '20 at 09:17

2 Answers2

1

Use the function would be a better option, but if you want to convert it to lambda you can try something like this:

import datetime

f = lambda x,return_schema="int": 0  if x is None and return_schema=='int' else ( datetime.datetime(1900,1,1) if x is None and return_schema=='date' else x)
print(f(7))
print(f(None))
print(f(None, return_schema='date'))
print(f(datetime.datetime.today(), return_schema='date'))

I hope it helps.

Ángel Igualada
  • 891
  • 9
  • 13
0

Are you looking for something along the lines of:


func = lambda x, y: 'default' if (x is None or y is None) else x

func(1, 2)        # returns 1
func(None, 1)     # returns 'default'
func(1, None)     # returns 'default'
func(None, None)  # returns 'default'

Or if you only want to do something based on the second parameter just change the if statement to test the value of that.

Chad Lamb
  • 564
  • 3
  • 9