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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:50:49+00:00 2026-05-27T12:50:49+00:00

How shall I define a constant lambda function in python? I need it to

  • 0

How shall I define a constant lambda function in python?

I need it to evaluation expressions like

lam (array[1,2,3,4,5])

For now I used

lam = lambda t: 1 + t*0 

It works but is it too wasteful?

  • 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-05-27T12:50:50+00:00Added an answer on May 27, 2026 at 12:50 pm

    if you just want to have a function that returns the same thing, no matter what arguments it’s called with, That’s A-OK! You are not in any way obligated to use any of your arguments.

    In python, lambda is a function without a name (and some other, unrelated limitations)

    If you are going to take the lambda expression and immediately assign its return value to a variable, you are giving a function a name. Don’t do that, just define a regular function. You should reach for lambda when you need to pass a function to another function, and the function you want to use is little, and doesn’t even merit a name (like when it always returns 1). Python has a few such “high order functions” (functions that take other functions as arguments), map, filter and reduce are in the built in namespace.

    #never!
    always_return_one = lambda ignored_argument: 1
    
    #OK: functions with names are def'ed not lambda'd
    def always_return_one(ignored_argument):
        return 1
    
    #Also OK: pass the lambda to another function as soon as you spell it.
    modified_list = some_highorder_function(lambda ignored: 1, [1, 2, 3, 4, 5])
    

    Some of the most used high order functions in python have a special syntax. In the case above, if the function was map, you can use a list comprehension like so:

    modified_list = [1 for ignored in [1, 2, 3, 4, 5]]
    

    which reads a little easier and is consistently faster!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to define inside a class a constant which value is the
First, defining two constant expressions without parentheses is my fault: #define BIG_INTERVAL 60 *
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
Hi I shall try to explain clearly what I need to be able to
I have a few reasons to define a type for a fixed length array
I'm using a library where they used paths to define input and output files
I am using ASP.NET MVC 2 (.NET 3.5), and need to manually define what
I think I shall reframe my question from Where should you use BlockingQueue Implementations
I wonder why software shall be deployed with its related debugging symbols. What are
I have a canvas in Flex that shall be able only to be scrolled

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.