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

The Archive Base Latest Questions

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

Possible Duplicate: What is a lambda and what is an example implementation? Here is

  • 0

Possible Duplicate:
What is a lambda and what is an example implementation?

Here is the code for a lambda (from Byte of Python):

def make_repeater(n):
    return lambda s: s * n

twice = make_repeater(2)

print twice('word')
print twice(5)

The output is this:

wordword
10

Can someone please explain how the lambda works in longform? how are word and 5 passed to s in the lambda function?

thanks.

  • 1 1 Answer
  • 1 View
  • 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-27T21:13:51+00:00Added an answer on May 27, 2026 at 9:13 pm

    As Jake described already, your make_repeater returns another function with n being bound to 2 (this is called a closure). So your code is roughly equivalent to:

    twice = lambda s: s * 2
    
    print twice('word')
    print twice(5)
    

    Which is in turn roughly equivalent to:

    def twice(s):
        return s * 2
    
    print twice('word')
    print twice(5)
    

    Which is in turn roughly equivalent to:

    print 'word' * 2
    print 5 * 2
    

    So what you actually do is:

    • evaluate the expression 'word' * 2, which results in 'wordword' (multiplication of strings is defined by Python as repeating the string the given number of times)
    • evaluate the expression 5 * 2, which results in 10 (this should not surprise you)

    The fact that your lambda function doesn’t care about the type of its argument and dynamically decides at runtime which method of multiplication is correct, is called dynamic typing.

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

Sidebar

Related Questions

Possible Duplicate: What are ‘closures’ in .NET? I am currently looking at lambda expression
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Bind Vs Lambda? My use of std::bind had dropped to 0 now
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: C# Lambda ( => ) For instance Messenger.Default.Register<AboutToCloseMessage>(this, (msg) => { if
Possible Duplicate: Why is it bad to use a iteration variable in a lambda
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: How would I identify if a website originates from a mobile browser?

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.