Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python3: 'where' keyword

Reply
Thread Tools

python3: 'where' keyword

 
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      01-08-2005
Darn space-eater google groups Here is it again, at teh risk of
generating controversy

..def gcd(a, b):
.. where:
.. a: int, b: int
.. return c where:
.. c: int
.. while a:
.. a, b = b%a, a
.. return b
more examples can be found at aroberge.blogspot.com

André

 
Reply With Quote
 
 
 
 
oren@REMOVETHIS1.hishome.net
Guest
Posts: n/a
 
      01-08-2005
When I first saw this I thought: "hmmm... this seems as redundant as
adding a repeat/until loop to Python; there's no chance in hell it will
ever be accepted by the community or Guido, but I actually kinda like
it". It's nice to see mostly positive reactions to this idea so far.

I think it's a really ingenious solution to the the anonymous function
problem - don't make it anonymous! A short, throwaway name with a very
localized scope is as good as a truly anonymous function and feels more
Pythonic to me. We thought we wanted a better syntax than lambda for
anonymous functions but Andrey shows that perhaps it wasn't what we
really need. What we need is a solution to quickly and cleanly generate
bits of callable code without polluting the containing namespace,
without having to think too hard about unique names and while making
their temporary and local nature clear from the context. Anonymity
isn't one of the requirements.

I really liked Nick Coghlan's property example. The names 'get' and
'set' are too short and generic to be used without a proper scope but
with this syntax they are just perfect.

Here's another example:

w = Widget(color=Red, onClick=onClick, onMouseOver=onMouseOver) where:
.. def onClick(event): do_this(event.x, event.y, foo)
.. def onMouseOver(event): someotherwidget.do_that()

The "onClick=onClick" part seems a bit redundant, right? So how about
this:

w = Widget(**kw) where:
.. color = Red
.. def onClick(event): do_this(event.x, event.y, blabla)
.. def onMouseOver(event): someotherwidget.do_that()
.. x, y = 100, 200
.. kw = locals()

I'm not really sure myself how much I like this. It has a certain charm
but also feels like abuse of the feature. Note that "w =
Widget(**locals()) where:" would produce the wrong result as it will
include all the values in the containing scope, not just those defined
in the where block.

Oren

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      01-08-2005
Andrey Tatarinov wrote:
> >>> print words[3], words[5] where:
> >>> words = input.split()

>
> - defining variables in "where" block would restrict their visibility to
> one expression


Then your example above doesn't work... print takes a
sequence of expressions, not a tuple as you seem to think.

-Peter
 
Reply With Quote
 
Carlos Ribeiro
Guest
Posts: n/a
 
      01-08-2005
On Sat, 08 Jan 2005 12:53:05 -0500, Peter Hansen <(E-Mail Removed)> wrote:
> Andrey Tatarinov wrote:
> > >>> print words[3], words[5] where:
> > >>> words = input.split()

> >
> > - defining variables in "where" block would restrict their visibility to
> > one expression

>
> Then your example above doesn't work... print takes a
> sequence of expressions, not a tuple as you seem to think.


I found it strange that he had chosen to make the example with
"print", that is a statement. I'm not sure how could it be made to
work with both expressions and statements, it just seems strange...

Overall, I found the idea interesting. It seems like a obvious
counterpart to "with", in the sense that both act as modifiers to the
scoping rules. I think it can be made to work, and that it would lead
to elegant & readable code, but there are still lots of things to
consider: exception handling, fast name lookup in the "where" block,
access to symbols outside the "where" block, just to name a few.

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
mail: (E-Mail Removed)
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-08-2005
Peter Hansen wrote:
> Andrey Tatarinov wrote:
> > >>> print words[3], words[5] where:
> > >>> words = input.split()

> >
> > - defining variables in "where" block would restrict their

visibility to
> > one expression

>
> Then your example above doesn't work... print takes a
> sequence of expressions, not a tuple as you seem to think.


You misunderstand. There "where" is not part of the expression but the
statement. The above example would be a modified print statement, a
print...where statement, if you will. Under this suggestion, there
would be modified versions of various simple statements.

This wouldn't be a problem parsing, of course, because "where" would be
a keyword.


--
CARL BANKS

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-08-2005
"Carl Banks" <(E-Mail Removed)> writes:
> You misunderstand. There "where" is not part of the expression but the
> statement. The above example would be a modified print statement, a
> print...where statement, if you will. Under this suggestion, there
> would be modified versions of various simple statements.


You mean I can't say

# compute sqrt(2) + sqrt(3)
x = (sqrt(a) where:
a = 2.) \
+ sqrt (a) where:
a = 3.

Hmmm.
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-09-2005

Bengt Richter wrote:
> And, is the whole thing after the '=' an expression? E.g.,
>
> x = ( foo(x) where:
> x = math.pi/4.0
> ) where:
> def foo(x): print 'just for illustration', x


How would that be any improvement over this?

.. x = foo(x) where:
.. x = math.pi/4.0
.. def foo(x): print 'just for illustration', x

Can anyone think of a use case for embedding "where" inside an
expression as opposed to making it part of a simple statement? And, if
so, is the benefit of it worth the massive hit in readability.


> or is this legal?
>
> for y in ([foo(x) for x in bar] where:
> bar = xrange(5)
> ): baz(y) where:
> def baz(arg): return arg*2


Here, I can only hope not. One reason I proposed a where...do syntax
is so that, if you wanted to localize a variable to a for loop or some
other compound statement, you could do it with a minimum of fuss.

.. where:
.. bar = xrange(5)
.. def baz(arg): return arg*2
.. do:
.. for y in [foo(x) for x in bar]:
.. baz(y)


> Not trying to sabotage the idea, really, just looking for

clarification

That's ok. For it fly, it's got to be able to withstand the flak.
--
CARL BANKS

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-09-2005

Paul Rubin wrote:
> "Carl Banks" <(E-Mail Removed)> writes:
> > You misunderstand.


BTW, Peter, I guess I should have said "I misunderstand, but it can be
legal if you consider it part of the statements", since it appears the
author did intend it to be part of an expression.


> > There "where" is not part of the expression but the
> > statement. The above example would be a modified print statement,

a
> > print...where statement, if you will. Under this suggestion, there
> > would be modified versions of various simple statements.

>
> You mean I can't say
>
> # compute sqrt(2) + sqrt(3)
> x = (sqrt(a) where:
> a = 2.) \
> + sqrt (a) where:
> a = 3.
>
> Hmmm.


What would be the advantage of that over this?

.. x = sqrt(a) + sqrt(b) where:
.. a = 2.0
.. b = 3.0

Where would making "where" part of an expression rather than part of
the statement help? Can you think of a place? ("That it makes Python
more like LISP" is not a good enough answer for me, BTW. But feel free
to try.


--
CARL BANKS

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-09-2005
"Carl Banks" <(E-Mail Removed)> writes:
> > # compute sqrt(2) + sqrt(3)
> > x = (sqrt(a) where:
> > a = 2.) \
> > + sqrt (a) where:
> > a = 3.
> >
> > Hmmm.

>
> What would be the advantage of that over this?
>
> . x = sqrt(a) + sqrt(b) where:
> . a = 2.0
> . b = 3.0


The idea of "where" is to allow re-using variable names instead of
having to keep track of which ones are in use. I just tried to give a
very simple example of how you might do that more than once in a
statement.
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      01-09-2005
Nick Coghlan wrote:
> I have a different suggestion for this.
>
> 'as' is used for renaming in import statements. 'as' will be used for

exception
> naming in Python 3k.
>
> So let's use it for expression naming in 'if' statements, too.
>
> if someregexp.match(s) as m:
> # blah using m
> elif someotherregexp.match(s) as m:
> # blah using m



What if the condition you wanted to test wasn't the same as the thing
you want to save? In other words, how would you convert this?

.. where:
.. m = something()
.. if m > 20:
.. do_something_with(m)

What you propose works for typical regexps idiom but not for the
slightly more general case. However, I could see why some people might
not like the where...if syntax I proposed; it's kind of choppy and not
exactly easy to follow at a first glance.

As a compromise, howabout:

.. if m > 20 where m=something():
.. do_something_with(m)

In this case, the m=something() is NOT an assignment statement, but
merely a syntax resembling it. The "where m=something()" is part of
the if-statement, not the if-expression. It causes m to be visisble in
the if-expression and the if-block.

It (or your suggestion) could work with a while-loop too.

.. while line where line=f.readline():
.. do_something_with(line)


The main problem here (as some would see it) is that you can't do
something this:

.. if m > 20 where (def m(): a(); b()):


--
CARL BANKS

 
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
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
Like keyword Patrick ASP .Net 1 01-26-2005 01:25 AM
Re: Why does using keyword require a new scope? Jip from Paris ASP .Net 0 08-25-2003 08:02 AM
Re: Why does using keyword require a new scope? Herman Eldering ASP .Net 0 08-23-2003 11:32 PM



Advertisments