#33939: Change docs for transaction.on_commit to suggest functools.partial 
instead
of lambda
-----------------------------------------+------------------------
               Reporter:  Alex Morega    |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  4.0
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 I've recently got bitten by a silly bug:

 {{{#!python
 for n in range(10):
     transaction.on_commit(lambda: do_something(n))
 }}}

 Of course, instead of calling `do_something(0)`, `do_something(1)` ...,
 it's going to call `do_something(9)` 10 times.

 The docs could help by suggesting
 [https://docs.python.org/3/library/functools.html#functools.partial
 functools.partial] instead of `lambda`. The above example becomes:

 {{{#!python
 for n in range(10):
     transaction.on_commit(partial(do_something, n))
 }}}

 If this change is a good idea I can have a go at making a PR.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33939>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182b74936f0-2a2de634-72f0-4ff7-a2b3-43f6d2a4d3c5-000000%40eu-central-1.amazonses.com.

Reply via email to