A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2001-December/018841.html below:

[Python-Dev] nested scopes confusion

[Python-Dev] nested scopes confusion [Python-Dev] nested scopes confusionThomas Heller thomas.heller@ion-tof.com
Tue, 4 Dec 2001 20:35:19 +0100
I would have expected the following code to print
1, 2, ..., 9. Instead it prints 25, 25, 25, .. 25.

def functions():
    result = []
    for i in range(10):
        def mth(*args): return i
        result.append(mth)
    i = 25
    return result

for mth in functions():
    print mth()

Reading PEP227, I can (barely) understand why it behaves this way.

How do I achieve the desired effect? Note that the default argument
trick (def mth(i=i): ...) does not work because *args is present.

Thomas




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4