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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:55:51+00:00 2026-05-22T02:55:51+00:00

When I run the following script, both lambda’s run os.startfile() on the same file

  • 0

When I run the following script, both lambda’s run os.startfile() on the same file — junk.txt. I would expect each lambda to use the value “f” was set to when the lambda was created. Is there a way to get this to function as I expect?

import os


def main():
    files = [r'C:\_local\test.txt', r'C:\_local\junk.txt']
    funcs = []
    for f in files:
        funcs.append(lambda: os.startfile(f))
    print funcs
    funcs[0]()
    funcs[1]()


if __name__ == '__main__':
    main()
  • 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-22T02:55:51+00:00Added an answer on May 22, 2026 at 2:55 am

    One way is to do this:

    def main():
        files = [r'C:\_local\test.txt', r'C:\_local\junk.txt']
        funcs = []
        for f in files:
            # create a new lambda and store the current `f` as default to `path`
            funcs.append(lambda path=f: os.stat(path))
        print funcs
    
        # calling the lambda without a parameter uses the default value
        funcs[0]() 
        funcs[1]()
    

    Otherwise f is looked up when the function is called, so you get the current (after the loop) value.

    Ways I like better:

    def make_statfunc(f):
        return lambda: os.stat(f)
    
    for f in files:
        # pass the current f to another function
        funcs.append(make_statfunc(f))
    

    or even (in python 2.5+):

    from functools import partial
    for f in files:
        # create a partially applied function
        funcs.append(partial(os.stat, f))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
When I run the following script, the event always fires on page load. I
I copied the following script and run it to have it listen on port
I am following this tutorial and I've run the command ruby script\server and successfully
I get the following error from the SQL Script I am trying to run:
In batch script, I can run an R script with the following syntax: Rterm.exe
I'm following this tutorial (seems good) for Rails. After I run ruby script/generate scaffold
I run the following Gert's extract command to the data dump file which format
When I'm trying to run the following code in IE:- <html> <head> <script language=javascript>
I'm trying to run the following little Python script: #!/usr/local/bin/python2.5 import sys, subprocess child

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.