[New-bugs-announce] [issue28241] Nested fuctions Unexpected behaviour when stored in a list and called after.

artxyz report at bugs.python.org
Wed Sep 21 11:12:53 EDT 2016


New submission from artxyz:

Python 2.7.11
GCC 4.8.4

Getting weird results when define a nested function in a loop and store them in a list

    x = list()
    for i in xrange(5):
        def FUN():
            print i
        x.append(FUN)

Calling functions from list using index works fine:

    for i in xrange(5):
        print x[i]
        x[i]()
    # prints 0 1 2 3 4

Calling function using iteration through the sequence yields  wrong results, despite current function (f) changes:

    for f in x:
        print f
        f()
    # prints 4 4 4 4 4

----------
components: Interpreter Core
files: nested_fun_bug.py
messages: 277155
nosy: artxyz
priority: normal
severity: normal
status: open
title: Nested fuctions Unexpected behaviour when stored in a list and called after.
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file44781/nested_fun_bug.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28241>
_______________________________________


More information about the New-bugs-announce mailing list