DEV Community

Discussion on: Writing functions in Python

Collapse
 
waylonwalker profile image
Waylon Walker

Keep up the work Chris.

An extension of functions are lambda functions. They are one liners that make it easy to embed them inline with other functions.

mult = lambda x,y: x * y

mult(2,2)
Enter fullscreen mode Exit fullscreen mode

Generally its not best practice to name a lambda like this though, and linters will yell and tell you to just make a regular function. Their power is embedding in other objects easily.

node(lambda x: x * 2, 'input_dataset', 'input_mult_2')
Enter fullscreen mode Exit fullscreen mode

Here is a simple example of something I do often to create kedro nodes. I make simple functions right inline of creating the DAG node.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yeah had Lambda on my sheet, so they are basically your shorthand arrow function compared to JavaScript?
Or why are they introduced?

I must say I find the full write-out more readable at the moment, but might come in handy for sure!