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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:55:04+00:00 2026-06-07T19:55:04+00:00

parameters confuse me a lot when i using defer function with twisted. i consider

  • 0

parameters confuse me a lot when i using defer function with twisted.

i consider if the function’s parameter is a INT-VALUE, or other basic types besides of list and dict, it should pass-by value not reference.

from twisted.internet import defer, reactor

def deferFunc(x):
    print "11111 %d" % x
    d = defer.Deferred()
    reactor.callLater(1, d.callback, x)
    return d

@defer.inlineCallbacks
def inlineDeferFunc(x):
    print "11111 %d" % x
    d = defer.Deferred()
    reactor.callLater(1, d.callback, x)
    result = yield d
    defer.returnValue(result)

def loop():
    x = [1,2,3,4]
    d = defer.succeed(0)
    for i in x:
        d.addCallback(lambda _ : inlineDeferFunc(i))

if __name__ == '__main__':
    loop()

    try:
        reactor.run()
    except:
        reactor.stop()

the result is :

11111 1
11111 4
11111 4
11111 4

and the result confused me.

how can i get the right output like 1,2,3,4

  • 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-07T19:55:07+00:00Added an answer on June 7, 2026 at 7:55 pm

    how can i get the right output like 1,2,3,4

    like this:

    def loop():
        x = [1,2,3,4]
        d = defer.succeed(0)
        for i in x:
            d.addCallback(lambda _, i = i : inlineDeferFunc(i))
    
    
    
    11111 1
    11111 2
    11111 3
    11111 4
    

    note that we are copying the values in the function definition i = i.
    the problem was that the lambda grabs the parent environment to access the variable i after all how it would know what value it is, for performance reasons I don’t think it copies it during definition, but the parent environment keeps being updated by the for loop.

    here is a simple test.

    >>> def test_closures():
    ...     i = 2
    ...     def test():
    ...         return i
    ...     i = 3
    ...     return test
    ... 
    >>> test_closures()()
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Use of 'const' for function parameters For example, void Func(int para); If
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Can I get the parameters of the last function called in traceback? How? I
Should I implement named parameters in Java using Hash tables? I saw this entry:
I am using the following parameters for my simulation on Geforce GT 220 card
I tried using fopen in C, the second parameter is the open mode. The
I am confused by the form of parameters for function pointers. The following two:
what parameters should i use to start a console app minimized or hidden? is
How do you pass parameters in ASP.net MVC over two views, like a wizard?
I changed some parameters in a Visual Studio C++ project, and now I don't

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.