Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:51:43+00:00 2026-06-11T20:51:43+00:00

From what I understand, Python lambda’s only allow expressions, not statements. I have a

  • 0

From what I understand, Python lambda’s only allow expressions, not statements. I have a case where I an using len() in a lambda expression and am trying to get the length of the return value of a function call. However, that function call has the potential to return None, which breaks len(). Is there a graceful way around this?

Example:

def foo(obj_list, field):
    return maxk(obj_list, key=lambda obj: len(nested_getattr(obj, field)))

In the above, maxk() is a version of max() that accepts a key argument. I’m on Python 2.4 (and cannot use anything higher at present), so I have a custom implementation of max() that takes a key argument, sourced from here (refer to posts #140122 and #140143). nested_getattr() is another utility function that acts like getattr() but can fetch an attribute nested within another. That can be sourced from here.

What the example function does (this is the actual function I am using with variables/names changed) is go through a list of objects (obj_list) and compare the length of the value in field and returns the object in the list whose field is the largest overall.

However, if the attribute specified in field for each object returns None, then len() will choke with a TypeError. I think I could get around this using an inline conditional, but I would have to call nested_getattr() twice, once for the check, and possibly a second time if its return value is not None. I would rather like to cache its return value to a variable and run that on a conditional before deciding what to return (or just have the generator expression skip over it entirely).

What’s a good way to handle this? I am open to other implementations of this function, maxk(), or nested_getattr() (if need be).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:51:44+00:00Added an answer on June 11, 2026 at 8:51 pm

    Just define the function separately as a regular function and then pass it into the maxk:

    def getLen(obj):
        val = nested_getattr(obj, field)
        if val is None:
            return 0
        else:
            return len(val)
    return maxk(obj, key=getLen)
    

    Lambdas are meant to be used when it is easier to write an inline single-expression function that it would be to write a separate function. If it becomes harder to think of how to write the lambda than to just write a normal function, just write a normal function. There’s no point spending time how to fit it into a lambda, because the lambda doesn’t gain you anything in that case.

    Edit: Lambdas are bytecode-identical to non-lambda functions that do the same thing.

    >>> f = lambda x: x**2
    >>> def g(x):
    ...     return x**2
    >>> import dis
    >>> dis.dis(f)
      1           0 LOAD_FAST                0 (x)
                  3 LOAD_CONST               1 (2)
                  6 BINARY_POWER        
                  7 RETURN_VALUE        
    >>> dis.dis(g)
      2           0 LOAD_FAST                0 (x)
                  3 LOAD_CONST               1 (2)
                  6 BINARY_POWER        
                  7 RETURN_VALUE     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a C++ extension for Python using SWIG. From what I understand
I'm trying to understand the python compiler/interpreter process more clearly. Unfortunately, I have not
I have been using Python for about a year now, coming from a mostly
I have a question about the map function in Python. From what I understand,
From what I understand using $this->db->insert() escapes the values: http://codeigniter.com/user_guide/database/active_record.html#insert Note: All values are
I am trying to understand the Python ctypes module. I have put together a
My question is regarding i18n in Python. From what I understand, it involves: Create
I understand that in Python a string is simply an expression and a string
I have only learnt python for for few months and totally a newb in
I'm creating a Qt application using PyQt4 in Python. I have chosen the Dialog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.