[Python-ideas] explicitation lines in python ?

Discussion in 'Python' started by geremy condra, Jul 12, 2010.

  1. On Sun, Jul 11, 2010 at 11:39 PM, Carl M. Johnson
    <> wrote:
    > On Sun, Jun 27, 2010 at 8:25 PM, Nick Coghlan <> wrote:
    >
    >> The availability of "nonlocal" binding semantics also makes the
    >> semantics much easier to define than they were in those previous
    >> discussions (the lack of clear semantics for name binding statements
    >> with an attached local namespace was the major factor blocking
    >> creation of a reference implementation for this proposal back then).
    >>
    >> For example:
    >>
    >>  c = sqrt(a*a + b*b) where:
    >>    a = retrieve_a()
    >>    b = retrieve_b()
    >>
    >> could translate to something like:
    >>
    >>  def _anon(): # *(see below)
    >>    nonlocal c
    >>    a = retrieve_a()
    >>    b = retrieve_b()
    >>    c = sqrt(a*a + b*b)
    >>  _anon()
    >>
    >> *(unlike Python code, the compiler can make truly anonymous functions
    >> by storing them solely on the VM stack. It already does this when
    >> executing class definitions):

    >
    > I like this idea, but I would tweak it slightly. Maybe we should say
    >
    > EXPRESSION where:
    >    BLOCK
    >
    > is equivalent to
    >
    > def _():
    >    BLOCK
    >    return EXPRESSION
    > _()
    >
    > That way, c = a where: a = 7 would be equivalent to
    >
    > def _():
    >   a = 7
    >   return a
    > c = _()
    >
    > One advantage of this equivalence is it would make it easier to work
    > around a longstanding scoping gotcha. A naïve coder might expect this
    > code to print out numbers 0 to 4:
    >
    >    >>> fs = []
    >    >>> for n in range(5):
    >    ...     def f():
    >    ...         print(item)
    >    ...     fs.append(f)
    >    ...
    >    >>> [f() for f in fs]
    >    4
    >    4
    >    4
    >    4
    >    4
    >    [None, None, None, None, None]
    >
    > I think we all have enough experience to know this isn’t a totally
    > unrealistic scenario. I personally stumbled into when I was trying to
    > create a class by looping through a set of method names.
    >
    > To get around it, one could use a where clause like so:
    >
    > fs = []
    > for n in range(5):
    >    fs.append(f) where:
    >        shadow = n
    >        def f():
    >            print(shadow)
    >
    > This would print out 0 to 4 as expected and be equivalent to
    >
    >    >>> fs = []
    >    >>> for n in range(5):
    >    ...     def _():
    >    ...         shadow = n
    >    ...         def f():
    >    ...             print(shadow)
    >    ...         fs.append(f)
    >    ...     _()
    >    ...
    >    >>> [f() for f in fs]
    >    0
    >    1
    >    2
    >    3
    >    4
    >    [None, None, None, None, None]
    >
    > I think a where-clause with def-like namespace semantics would be a
    > positive addition to Python, once the moratorium is up.
    >
    > -- Carl Johnson


    +1 from me, FWIW

    Geremy Condra
     
    geremy condra, Jul 12, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,741
  2. Joe Wright
    Replies:
    0
    Views:
    555
    Joe Wright
    Jul 27, 2003
  3. lovecreatesbeauty

    How to know two lines are a pare parallel lines

    lovecreatesbeauty, Apr 27, 2006, in forum: C Programming
    Replies:
    11
    Views:
    694
    Old Wolf
    Apr 28, 2006
  4. Replies:
    1
    Views:
    487
    Jonathan Mcdougall
    Dec 6, 2005
  5. Replies:
    9
    Views:
    138
    Bill H
    May 20, 2008
Loading...

Share This Page