DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Creepy Ways to Invoke a Function in Python: Lambda

Creepy Ways to Invoke a Function in Python: Lambda

A big data expert explores one of the more powerful, and fun, facets of Python, lamba functions, and how they can be used in data science.

Matt Hughes user avatar by
Matt Hughes
·
Sep. 07, 18 · Tutorial
Like (3)
Save
Tweet
Share
5.67K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Whenever I begin learning a new language, I immediately get super cocky and say out loud, “I know everything, I’m a genius how hard can it be!?”

... before those beastly bus terminal cops come and ask me to leave.

But, as always, while snoozing under the bridge covered in my own soil, I perk up and realize that this particular language has handed me a challenge.

Python has a couple of very sexy ways of invoking a function.

Even the name is sexy, isn't it? Python.

I never digress.

Sure you can just define a function...

def hello(first_name, last_name):
print("Hello World "+first_name+last_name) 
return

Then call it...

hello(“Matt”, “Hughes”)

But that's is so 90's.

Image title

"Hey nerd, did that function come with a free Beanie Baby?"

And, yeah, I know Python was invented in the '50s to defeat Hitler's Spanish Armada, but sometimes old things can still seem new.

Upon first exploring the lambda operator my brain contracted and shat out the word “No” several times.

(Shat is not a curse word.)

The lambda operator is a quick and useful way of declaring, using, and throwing away a small function...

The general expression of a lambda function is this...

lambda argument_list: expression

The lambda operator is useful when you just want a simple calculation done on a set of data ONCE. You don’t have to name it and you can use it with other operators like Map or Filter which makes it an extremely powerful tool...

myList = [0,1,2,3,4,5,6,7]
result = map(lambda x: x * 2, myList)

The above code block will apply the map operator to your little lambda function, multiplying everything in your list by 2.

Some developers don't use this technique, mainly for readability reasons. Some will claim that it is more difficult to study code like this then code with obviously declared functions.  

On the other hand, many will contest that the lambda function is MORE readable and constructive than its generic counterpart. In my opinion, it comes down to a situational question.

A lambda call could theoretically stretch out to hundreds or even thousands of characters. I'm not sure you're going to find anyone claiming that this makes code easier to follow. Quite to the contrary, and probably a good spot to construct a function in its original intended shape (or an entire Class, but let's not get into that just yet).

However you end up using it, lambda can be a fantastic, fast (and fun) alternative.

Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Asynchronous Messaging Service
  • Keep Your Application Secrets Secret
  • A Beginner's Guide to Infrastructure as Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: