Deprecated: list comp leak use

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Reedy

    Deprecated: list comp leak use

    Announcement/Warning

    In the current implementation of list comprehensions, for-part control
    variables leak to the surrounding context. Example:[color=blue][color=green][color=darkred]
    >>> [i for i in [1,2]][/color][/color][/color]
    [1, 2][color=blue][color=green][color=darkred]
    >>> print i[/color][/color][/color]
    2
    The name i was bound within the list comprehension. The Reference
    Manual currently says nothing about this behavior.

    On py-dev today (now yesterday), after GvR described how the leak
    could be plugged, and other developers urged that it be so plugged,
    GvR declared use of such leaked bindings as deprecated, and the
    leakage subject to cessation in a future version .

    Since people have posted at least one clever hack exploiting the
    leakage (for simulating assignment expressions), I though this
    followup might be useful.

    This decision and any future patches will *NOT* affect for-loop
    variables. The following search idiom, for instance, will continue to
    work:

    for item in seq:
    if pred(item): break
    else:
    item = default #usually sentinal value such as None
    # item is now first item meeting pred criterion or default

    Terry J. Reedy


  • Ville Vainio

    #2
    Re: Deprecated: list comp leak use

    "Terry Reedy" <tjreedy@udel.e du> writes:
    [color=blue]
    > Announcement/Warning[/color]

    /Good news of the week :-).

    --
    Ville Vainio http://www.students.tut.fi/~vainio24

    Comment

    • John Roth

      #3
      Re: Deprecated: list comp leak use


      "Terry Reedy" <tjreedy@udel.e du> wrote in message
      news:c8ydnYL_P9 UILgmiRVn-jw@comcast.com. ..[color=blue]
      > Announcement/Warning
      >
      > In the current implementation of list comprehensions, for-part control
      > variables leak to the surrounding context. Example:[color=green][color=darkred]
      > >>> [i for i in [1,2]][/color][/color]
      > [1, 2][color=green][color=darkred]
      > >>> print i[/color][/color]
      > 2
      > The name i was bound within the list comprehension. The Reference
      > Manual currently says nothing about this behavior.[/color]

      However, the tutorial for 2.2.3 not only describes it, but it
      gives an example of how it can be used.

      John Roth[color=blue]
      >
      > Terry J. Reedy
      >
      >[/color]


      Comment

      • Terry Reedy

        #4
        Re: Deprecated: list comp leak use


        "John Roth" <newsgroups@jhr othjr.com> wrote in message
        news:vpa6a9a8s0 cj60@news.super news.com...[color=blue][color=green]
        > > The name i was bound within the list comprehension. The Reference
        > > Manual currently says nothing about this behavior.[/color]
        >
        > However, the tutorial for 2.2.3 not only describes it,[/color]

        That I see
        [color=blue]
        >but it gives an example of how it can be used.[/color]

        That I don't, unless you mean this[color=blue][color=green][color=darkred]
        >>> x # the final value for range(5)[/color][/color][/color]
        4

        Thanks. I will point this out to appropriate persons.

        Terry J. Reedy


        Comment

        Working...