Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Python-ideas] explicitation lines in python ?

Reply
Thread Tools

[Python-ideas] explicitation lines in python ?

 
 
geremy condra
Guest
Posts: n/a
 
      07-12-2010
On Sun, Jul 11, 2010 at 11:39 PM, Carl M. Johnson
<(E-Mail Removed)> wrote:
> On Sun, Jun 27, 2010 at 8:25 PM, Nick Coghlan <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know two lines are a pare parallel lines lovecreatesbeauty C Programming 11 04-28-2006 03:32 AM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments