PowerShell Anonymous Functions

Functions defined without a name are called Anonymous Functions because they are not declared by the standard approach by using the keyworddef in Python or keyword function in PowerShell.

An anonymous function in PowerShell can be implemented by defining a script block which can access the arguments passed to the script block using the automatic variable and$args can evaluate an expression depending upon arguments supplied during the function call. To know more about Script blocks and automatic variable read the help documentation of these in PowerShell:

Get-Help About_Script_Blocks
Get-Help About_Automatic_Variables

Assign the script block to a variable, and now this variable can be treated as a PowerShell function or in other words you can call them anonymous function because it doesn’t have a name by which it can be referred or called. In order to call this anonymous function, you have to use the call operator ( & ) followed by the variable name that stores the script block and you can also pass the arguments to the parameters separated by spaces like in the following code sample which will evaluate the expression inside the code block and return results:


Anonymous Function

PowerShell also has a second approach that allows you to define named parameters using the param() statement inside the script block, like in the following example:

Anonymous Function

There is a third approach which utilizes the PowerShell Type Accelerator: [ScriptBlock] to defines a script block using the static method create()

Anonymous Function

Python Anonymous Function or Lambda Function

In Python, you can use the keyword lambda to create small anonymous functions that can have multiple arguments and an expression that is evaluated in a single statement on basis of the arguments passed. And because of the keyword: lambda it is also called as Lambda Functions.

The syntax of lambda function in Python:
lambda [arg1 [,arg2,.....argn]]: expression

Just like we did in PowerShell we define the arguments in PowerShell script block, similarly, we then use these arguments in the expression to return a value in Python Lambda functions.

NOTE: 

This is just a teaser content from my book in form of a blog post!

Book URL: https://leanpub.com/PowerShell-to-Python

If you want to know more about Functions and types of Functions, Parameters (default/mandatory)  in Python and PowerShell, then read my book (below) which is still in progress, on lean publishing format.

Buy early, pay less, free updates!


My New Book: PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for a longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump-start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast-changing IT scenarios to DevOps and Cloud to the future – Data ScienceArtificial Intelligence (AI) and Machine Learning; Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at [email protected]

Optical Character Recognition

Subscribe to our mailing list

* indicates required