• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Data Dependence

Data Dependence

Learn about software development. Covering topics such as Coding in Python and JavaScript, developer productivity and more.

  • Home
  • Top Resources
  • Who Are You?
  • Show Search
Hide Search
You are here: Home / Programming / [Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Lambda Expressions

[Video Series] Taking Your Python Skills to the Next Level With Pythonic Code – Lambda Expressions

· · 2 Comments

This is the last post in a series covering Pythonic code written by Michael Kennedy of Talk Python To Me. Be sure to catch the whole series with 5 powerful Pythonic recommendations and over 45 minutes of video examples.

Python treats functions as first class citizens. This provides a tonne of flexibility, but what exactly does it mean? Well, functions are basically instances of a type; just like the string ‘cat’ or the object ‘new_user’ created from the Customer class. With them we can:

  • Pass functions between methods
  • Compare them
  • Create private closures using functions within functions
  • And more…

The key part we want to focus upon is the first feature: Passing function between methods. This allows us to write code (methods or functions) that are not fully implemented but instead depend on the caller to supply the implementation.

PASSING FUNCTIONS BETWEEN METHODS

Here’s an example. The following method trims a sequence of numbers down to a smaller list and returns those numbers. But the method doesn’t decide which numbers matter. Rather the caller supplies a method to make that decision:

def find_special_numbers(special_selector, limit=10):
    found = []
    n = 0
    while len(found) < limit:
        if special_selector(n):
            found.append(n)
        n += 1
    return found

If we had a function like this that could determine whether a number was odd:

def check_for_odd(n):
    return n % 2 == 1

Then we could combine that to get just the odd numbers:

find_special_numbers(check_for_odd, 25)

But writing separate methods for these adds no value and only detracts from our solution. That’s where lambda methods come in.

USING LAMBDA EXPRESSIONS

We can use lambda methods as small, inline blocks of code (which are just functions like any other) that are used directly where they are called. Here’s the same thing with lambdas (notice no other check method):

find_special_numbers(lambda i: i % 2 == 1, 25)

Nice huh? Lambdas solve a bunch of problems elegantly in Python. Check out this video to see them in action!

VIDEO: LAMBDA EXPRESSIONS

You can find the code for this video on GitHub.

Michael Kennedy
@mkennedy

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list and follow me on Twitter so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.

I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.

I’m friendly, so feel free to say hello!

Twitter is the best place for a chat.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)

Related

Programming lambda expressions, python, pythonic

Primary Sidebar

LEVEL UP with the Data Dependence Newsletter! Subscribe!


Top Posts & Pages

  • How to Find Unclosed Tags and Brackets Using a Stack
  • How to Build a GUI in Python 3.5
  • Mocking in Python - How to Bypass Expensive and External Code Within Your Tests
  • How to Logically Group Your Python Code into Modules
  • A Quick Guide to Slicing in Python - Become a Python Ninja

Data Dependence

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

  • Home
  • Top Resources
  • Who Are You?