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 8534669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:14:58+00:00 2026-06-11T10:14:58+00:00

Is it possible to use a try except block inside of a lambda function?

  • 0

Is it possible to use a try except block inside of a lambda function? I need the lambda function to convert a certain variable into an integer, but not all of the values will be able to be converted into integers.

  • 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-11T10:14:59+00:00Added an answer on June 11, 2026 at 10:14 am

    Nope. A Python lambda can only be a single expression. Use a named function.

    It is convenient to write a generic function for converting types:

    def tryconvert(value, default, *types):
        for t in types:
            try:
                return t(value)
            except (ValueError, TypeError):
                continue
        return default
    

    Then you can write your lambda:

    lambda v: tryconvert(v, 0, int)
    

    You could also write tryconvert() so it returns a function that takes the value to be converted; then you don’t need the lambda:

    def tryconvert(default, *types):
        def convert(value):
            for t in types:
                try:
                    return t(value)
                except (ValueError, TypeError):
                    continue
            return default
        # set name of conversion function to something more useful
        namext = ("_%s_" % default) + "_".join(t.__name__ for t in types)
        if hasattr(convert, "__qualname__"): convert.__qualname__ += namext
        convert.__name__ += namext
        return convert
    

    Now tryconvert(0, int) returns a function convert_0_int that takes a value and converts it to an integer, and returns 0 if this can’t be done. You can use this function right away (not saving a copy):

    mynumber = tryconert(0, int)(value)
    

    Or save it to call it later:

    intconvert = tryconvert(0, int)
    # later...
    mynumber = intconvert(value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: When to use try/catch blocks? Main method code entirely inside try/catch: Is
Is it possible to use TRY CATCH blocks in SQL Selects? For stuff similar
Possible Duplicate: How to catch exceptions I have not really had to use try
I really often use: try try with finally so I'm interesting if is possible
I try to use single quotes as much as possible and I've noticed that
I need a control having these features: It should be possible use it in
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
I have a function with logic that looks like this: doStuff1() try: doStuff2() except
Is it possible to use Except() for two List's that have two different classes
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I

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.