Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
python3: 'where' keyword
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 98 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andrey Tatarinov  
View profile  
 More options Jan 7 2005, 12:56 pm
Newsgroups: comp.lang.python
From: Andrey Tatarinov <elephan...@dezcom.mephi.ru>
Date: Fri, 07 Jan 2005 20:56:41 +0300
Local: Fri, Jan 7 2005 12:56 pm
Subject: python3: 'where' keyword
Hi.

It would be great to be able to reverse usage/definition parts in
haskell-way with "where" keyword. Since Python 3 would miss lambda, that
would be extremly useful for creating readable sources.

Usage could be something like:

 >>> res = [ f(i) for i in objects ] where:
 >>>     def f(x):
 >>>         #do something

or

 >>> print words[3], words[5] where:
 >>>     words = input.split()

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

- it's more easy to read sources when you know which part you can skip,
compare to

 >>> def f(x):
 >>>     #do something
 >>> res = [ f(i) for i in objects ]

in this case you read definition of "f" before you know something about
it usage.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Jan 7 2005, 1:17 pm
Newsgroups: comp.lang.python
From: Steven Bethard <steven.beth...@gmail.com>
Date: Fri, 07 Jan 2005 11:17:39 -0700
Local: Fri, Jan 7 2005 1:17 pm
Subject: Re: python3: 'where' keyword

How often is this really necessary?  Could you describe some benefits of
this?  I think the only time I've ever run into scoping problems is with
lambda, e.g.

     [lambda x: f(x) for x, f in lst]

instead of

     [lambda x, f=f: for x, f in lst]

Are there other situations where you run into these kinds of problems?

> - it's more easy to read sources when you know which part you can skip,
> compare to

>  >>> def f(x):
>  >>>     #do something
>  >>> res = [ f(i) for i in objects ]

> in this case you read definition of "f" before you know something about
> it usage.

Hmm...  This seems very heavily a matter of personal preference.  I find
that your where clause makes me skip the 'res' assignment to read what
the 'res' block contains.  I had to read it twice before I actually
looked at the list comprehension.  Of course, I'm sure I could be
retrained to read it the right way, but until I see some real benefit
from it, I'd rather not have to.

TOOWTDI-ily-yrs,

Steve


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Jan 7 2005, 1:23 pm
Newsgroups: comp.lang.python
From: Steven Bethard <steven.beth...@gmail.com>
Date: Fri, 07 Jan 2005 11:23:28 -0700
Local: Fri, Jan 7 2005 1:23 pm
Subject: Re: python3: 'where' keyword

Steven Bethard wrote:
> How often is this really necessary?  Could you describe some benefits of
> this?  I think the only time I've ever run into scoping problems is with
> lambda, e.g.

>     [lambda x: f(x) for x, f in lst]

> instead of

>     [lambda x, f=f: for x, f in lst]

Sorry, bad example, this should have looked something more like:

     [lambda y: f(x, y) for x, f in lst]

...

     [lambda y, x=x, f=f: f(x, y) for x, f in lst]

where you actually need the lambda.

Steve


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donn Cave  
View profile  
 More options Jan 7 2005, 2:53 pm
Newsgroups: comp.lang.python
From: Donn Cave <d...@u.washington.edu>
Date: Fri, 07 Jan 2005 11:53:37 -0800
Local: Fri, Jan 7 2005 2:53 pm
Subject: Re: python3: 'where' keyword
In article <xdCdnUz9muXfT0PcRVn...@comcast.com>,
 Steven Bethard <steven.beth...@gmail.com> wrote:

Note that he says "would be extremely useful for creating readable
sources", so the "these kinds of problems" he would have been thinking
of would be where source was not as readable as it could be.  You seem
to be concerned about something else.

I don't by any means agree that this notation is worth adopting, and
in general I think this kind of readability issue is more or less a lost
cause for a language with Python's scoping rules, but the motive makes
sense to me.  One way to look at it might be, if I observe that "words"
is assigned to in a where clause, then I  know it will not be used
elsewhere in the surrounding scope so I can forget about it right away.
If the name does occur elsewhere, it evidently refers to something else.

   Donn Cave, d...@u.washington.edu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AdSR  
View profile  
 More options Jan 7 2005, 3:04 pm
Newsgroups: comp.lang.python
From: AdSR <a...@poczta.onet.pl>
Date: Fri, 07 Jan 2005 21:04:28 +0100
Local: Fri, Jan 7 2005 3:04 pm
Subject: Re: python3: 'where' keyword

Andrey Tatarinov wrote:
> Hi.

> It would be great to be able to reverse usage/definition parts in
> haskell-way with "where" keyword. Since Python 3 would miss lambda, that
> would be extremly useful for creating readable sources.

> Usage could be something like:

>  >>> res = [ f(i) for i in objects ] where:
>  >>>     def f(x):
>  >>>         #do something

I don't know haskell, but it looks SQL-ish to me (only by loose
association). And it's not that unpythonic - it resembles

 >>> res = [x for x in sequence if x.isOk()]

> or

>  >>> print words[3], words[5] where:
>  >>>     words = input.split()

Here's a shorter version:

 >>> print input.split()[3:5:2]

(Does it qualify as obfuscated Python code? :) )

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

> - it's more easy to read sources when you know which part you can skip,

Yes, I like the readability of it, too.

> compare to

>  >>> def f(x):
>  >>>     #do something
>  >>> res = [ f(i) for i in objects ]

> in this case you read definition of "f" before you know something about
> it usage.

When I first read your post, I thought "Well, just one more of those
Py3k ideas that appear on c.l.py every day." But as I look at the latter
example, I think you have just scratched my itch. The same thing has
bugged me more than once in my code.

I think this idea is of the same kind as the @decorator syntax. Guido
moved an operation to a point in the code where it was more visible. You
moved an operation to a more local context where (pun not intended) it
really belongs.

I'm usually rather conservative about Python syntax (@decorators,
absolute/relative imports, if-else operator), but this one could appear
in Python tomorrow and that would be too far in the future for me ;)

Cheers,

AdSR


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 7 2005, 4:28 pm
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 07 Jan 2005 13:28:33 -0800
Local: Fri, Jan 7 2005 4:28 pm
Subject: Re: python3: 'where' keyword

Donn Cave <d...@u.washington.edu> writes:
> I don't by any means agree that this notation is worth adopting, and
> in general I think this kind of readability issue is more or less a lost
> cause for a language with Python's scoping rules, but the motive makes
> sense to me.  

But we're talking about the mythical/hypothetical Python 3, so maybe
there's a chance of fixing the scoping rules, which it seems to me are
currently pretty badly broken.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Coghlan  
View profile  
 More options Jan 8 2005, 1:08 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Sat, 08 Jan 2005 16:08:43 +1000
Local: Sat, Jan 8 2005 1:08 am
Subject: Re: python3: 'where' keyword

Andrey Tatarinov wrote:
> Hi.

> It would be great to be able to reverse usage/definition parts in
> haskell-way with "where" keyword. Since Python 3 would miss lambda, that
> would be extremly useful for creating readable sources.

> Usage could be something like:

>  >>> res = [ f(i) for i in objects ] where:
>  >>>     def f(x):
>  >>>         #do something

Hmm, this is actually a really interesting idea. Avoiding accidental namespace
conflicts is certainly one of the advantages of using lambdas.

This idea has the virtue of being able to do the same thing, but have full
access to Python's function syntax and assignment statements in the 'expression
local' suite.

In fact, any subexpressions in a complicated expression can be extracted and
named for clarity without fear of screwing up the containing namespace, which
would be an enormous boon for software maintainers.

It also allows the necessary but uninteresting setup for an expression to be
moved "out of the way", bringing the expression that does the real work to
prominence.

 From the interpreter's point of view, the meaning would probably be something like:

namespace = locals()
exec where_suite in globals(), namespace
exec statement in globals(), namespace
res = namespace["res"]
del namespace

Making the 'where' clause part of the grammar for the assignment statement
should be enough to make the above example parseable, too.

The clause might actually make sense for all of the simple statement forms in
the grammar which contain an expression:
   expression statement
   assignment statement
   augmented assignment statement
   del statement
   print statement
   return statement
   yield statement
   raise statement
   exec statement

The clause really isn't appropriate for break, continue, import or global
statements, as they don't contain any expressions :)

For compound statements, a where clause probably isn't appropriate, as it would
be rather unclear what the where clause applied to.

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 8 2005, 1:30 am
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 07 Jan 2005 22:30:32 -0800
Local: Sat, Jan 8 2005 1:30 am
Subject: Re: python3: 'where' keyword

Nick Coghlan <ncogh...@iinet.net.au> writes:
> > Usage could be something like:
> >  >>> res = [ f(i) for i in objects ] where:
> >  >>>     def f(x):
> >  >>>         #do something

> Hmm, this is actually a really interesting idea. Avoiding accidental
> namespace conflicts is certainly one of the advantages of using lambdas.

I like it too.  Seems a little perl-ish, but what the hey.

> In fact, any subexpressions in a complicated expression can be
> extracted and named for clarity without fear of screwing up the
> containing namespace, which would be an enormous boon for software
> maintainers.

Sure, why not:

    x = (-b + sqrt(discriminant))/(2*a) where:
          discriminant = b*b - 4*a*c

Maybe you could just have a where: suite that binds variables
within the suite:

    where:
       def f(x):
          #do something
    res = [ f(i) for i in objects ]

    where:
       discriminant = b*b - 4*a*c
    x = (-b + sqrt(discriminant))/(2*a)

Syntax is
   where:
      suite
   statement

the suite has its own scope so any variable created there is local to
the suite plus the following statement.  The scope vanishes after the
statement.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Coghlan  
View profile  
 More options Jan 8 2005, 1:42 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Sat, 08 Jan 2005 16:42:16 +1000
Local: Sat, Jan 8 2005 1:42 am
Subject: Re: python3: 'where' keyword

Nick Coghlan wrote:
> It also allows the necessary but uninteresting setup for an expression
> to be moved "out of the way", bringing the expression that does the real
> work to prominence.

Killer app for this keyword:

class C(object):

   x = property(get, set) where:
     def get(self):
       return "Silly property"
     def set(self, val):
       self.x = "Told you it was silly"

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Coghlan  
View profile  
 More options Jan 8 2005, 1:47 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Sat, 08 Jan 2005 16:47:16 +1000
Local: Sat, Jan 8 2005 1:47 am
Subject: Re: python3: 'where' keyword

Paul Rubin wrote:
> the suite has its own scope so any variable created there is local to
> the suite plus the following statement.  The scope vanishes after the
> statement.

The second part of the idea is to give the statement greater prominence and
'hide' the uninteresting setup (the contents of the where clause).

Putting the statement of interest after the where suite still gives the benefits
of avoiding namespace clutter, but doesn't really help readability (it makes it
worse, if you ask me).

Particularly, what if the next statement is a compound statement?

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
michele.simion...@gmail.com  
View profile  
 More options Jan 8 2005, 2:04 am
Newsgroups: comp.lang.python
From: michele.simion...@gmail.com
Date: 7 Jan 2005 23:04:26 -0800
Local: Sat, Jan 8 2005 2:04 am
Subject: Re: python3: 'where' keyword

> But we're talking about the mythical/hypothetical Python 3, so maybe
> there's a chance of fixing the scoping rules, which it seems to me
are
> currently pretty badly broken.

I don't think the current scoping rules will be changed in Python 3.0.
I can't give you the link right now, but there are threads about the
scope rules in
python-dev, with various people protesting and Guido saying that he
wants to
keep them as they are.

         Michele Simionato


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Banks  
View profile  
 More options Jan 8 2005, 3:02 am
Newsgroups: comp.lang.python
From: "Carl Banks" <invalidem...@aerojockey.com>
Date: 8 Jan 2005 00:02:15 -0800
Local: Sat, Jan 8 2005 3:02 am
Subject: Re: python3: 'where' keyword

Right.  But you know that as soon as you add this to simple
expressions, a bunch of people are going to come here whining about how
they don't get to use where with if-expressions.

Frankly, they might have a point here.  Although we have replacing
lambda expressions on our minds, I have in mind a different problem
that a where-statement would solve perfectly.  But it would have to be
used with an if-expression.

However, I think it might not be so hard.  Let's take Paul Rubin's
advice and precede the if statement with where.  Let's also allow
"elif" clauses to be replaced with "else where ... if" clauses.  That
which is bound in the while-block would be visible in both the
if-expression and if-block.

Then we could do this:

. where:
.     m = someregexp.match(somestring)
. if m:
.     blah blah blah
. else where:
.     m = someotherregexp.match(somestring)
. if m:
.     blah blah blah

We might want to spell "else where" instead as "elwhere", to match
"elif", but that's not important now.  This would take away one of the
major minor annoyances of Python.  (In fact, I've suggested something
like this as a solution to the set-and-test idiom, which Python makes
difficult, only I used the keyword "suppose" instead of "where".)

Ok, but if you do that, now you have people whining that "where" comes
after some expressions, and before others.  (This would not bother me
one bit, BTW, but I'm pretty sure I'd lose the popular vote on this
one.)

So, let's go all out and say that while could precede any statement.
We now have consistency.  Well, that really wouldn't work for the
if-statement, though, because then how could we apply a different
while-block to an else clause?  We'd have to treat if-statements
specially anyways.  So we don't have consistency.

My solution would be to propose two different where statements: a
where...do statement, and a separate where...if statement.  The
where...do statement would look like this:

. where:
.     def whatever(): pass
. do:
.     blah blah use whatever blah

It has the advantage of being able to apply the where bindings to
several statements, and is, IMO, much cleaner looking than simply
applying where's bindings to the single following unindented statement.

I would recommend against where...while and where...for statements.
They can't accomplish anything you couldn't do with a break statement
inside the block, and it's not obvious whether the where clause gets
executed once or for each loop (since it's physically outside the loop
part).

One question: what do you do with a variable bound inside a while-block
that has the same name as a local variable?  (Or, horrors, a
surrounding while-block?)  I'm inclined to think it should be illegal,
but maybe it would be too restrictive.
Anyways, I like this idea a lot.

+1

--
CARL BANKS


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "python3: accessing the result of 'if'" by Nick Coghlan
Nick Coghlan  
View profile  
 More options Jan 8 2005, 3:30 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Sat, 08 Jan 2005 18:30:25 +1000
Local: Sat, Jan 8 2005 3:30 am
Subject: Re: python3: accessing the result of 'if'

Carl Banks wrote:
> Right.  But you know that as soon as you add this to simple
> expressions, a bunch of people are going to come here whining about how
> they don't get to use where with if-expressions.

> Frankly, they might have a point here.  Although we have replacing
> lambda expressions on our minds, I have in mind a different problem
> that a where-statement would solve perfectly.  But it would have to be
> used with an if-expression.

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

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 8 2005, 4:55 am
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 08 Jan 2005 01:55:03 -0800
Local: Sat, Jan 8 2005 4:55 am
Subject: Re: python3: accessing the result of 'if'

Nick Coghlan <ncogh...@iinet.net.au> writes:
> 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

Certainly an improvement over what we have now.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "python3: 'where' keyword" by AdSR
AdSR  
View profile  
 More options Jan 8 2005, 5:30 am
Newsgroups: comp.lang.python
From: AdSR <a...@poczta.onet.pl>
Date: Sat, 08 Jan 2005 11:30:57 +0100
Local: Sat, Jan 8 2005 5:30 am
Subject: Re: python3: 'where' keyword

Nick Coghlan wrote:

> Killer app for this keyword:

> class C(object):

>   x = property(get, set) where:
>     def get(self):
>       return "Silly property"
>     def set(self, val):
>       self.x = "Told you it was silly"

Hey, this is super-elegant!

AdSR


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 8 2005, 5:34 am
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 08 Jan 2005 02:34:08 -0800
Local: Sat, Jan 8 2005 5:34 am
Subject: Re: python3: 'where' keyword

AdSR <a...@poczta.onet.pl> writes:
> > Killer app for this keyword:
> > class C(object):
> >   x = property(get, set) where:
> >     def get(self):
> >       return "Silly property"
> >     def set(self, val):
> >       self.x = "Told you it was silly"

> Hey, this is super-elegant!

Heh, even further:

    z = C() where:
       class C(object):
          ...

Lets you make anonymous classes and singleton objects.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bengt Richter  
View profile  
 More options Jan 8 2005, 6:31 am
Newsgroups: comp.lang.python
From: b...@oz.net (Bengt Richter)
Date: Sat, 08 Jan 2005 11:31:08 GMT
Local: Sat, Jan 8 2005 6:31 am
Subject: Re: python3: 'where' keyword

Yes, that is cool and it _is_ an interesting idea. Are suites nestable? E.g., is this legal?

   x = term1 + term2 where:
       term1 = a*b where:
           a = 123
           b = 456
       term2 = math.pi

Reminds me of some kind of weird let ;-)

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

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

Not trying to sabotage the idea, really, just looking for clarification ;-)

Regards,
Bengt Richter


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrey Tatarinov  
View profile  
 More options Jan 8 2005, 6:43 am
Newsgroups: comp.lang.python
From: Andrey Tatarinov <elephan...@dezcom.mephi.ru>
Date: Sat, 08 Jan 2005 14:43:36 +0300
Local: Sat, Jan 8 2005 6:43 am
Subject: Re: python3: 'where' keyword

yes, all your examples are correct. And that's the way I'd like to use
this feature.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrey Tatarinov  
View profile  
 More options Jan 8 2005, 6:44 am
Newsgroups: comp.lang.python
From: Andrey Tatarinov <elephan...@dezcom.mephi.ru>
Date: Sat, 08 Jan 2005 14:44:44 +0300
Local: Sat, Jan 8 2005 6:44 am
Subject: Re: python3: 'where' keyword

Nick Coghlan wrote:
>> It also allows the necessary but uninteresting setup for an expression
>> to be moved "out of the way", bringing the expression that does the
>> real work to prominence.
> Killer app for this keyword:

> class C(object):

>   x = property(get, set) where:
>     def get(self):
>       return "Silly property"
>     def set(self, val):
>       self.x = "Told you it was silly"

oh, that's great! I can't imagine prettier example

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
André  
View profile  
 More options Jan 8 2005, 11:01 am
Newsgroups: comp.lang.python
From: "André" <andre.robe...@gmail.com>
Date: 8 Jan 2005 08:01:43 -0800
Local: Sat, Jan 8 2005 11:01 am
Subject: Re: python3: 'where' keyword
At the risk of generating controversy, here's another type of example:

def gcd(a, b):
where:
a: int, b: int
return c where:
c: int
while a:
a, b = b%a, a
return b

More can be found at http://aroberge.blogspot.com

Andre


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
André  
View profile  
 More options Jan 8 2005, 11:14 am
Newsgroups: comp.lang.python
From: "André" <andre.robe...@gmail.com>
Date: 8 Jan 2005 08:14:31 -0800
Local: Sat, Jan 8 2005 11:14 am
Subject: Re: python3: 'where' keyword
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é


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
o...@removethis1.hishome.net  
View profile  
 More options Jan 8 2005, 12:07 pm
Newsgroups: comp.lang.python
From: o...@REMOVETHIS1.hishome.net
Date: 8 Jan 2005 09:07:42 -0800
Local: Sat, Jan 8 2005 12:07 pm
Subject: Re: python3: 'where' keyword
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Hansen  
View profile  
 More options Jan 8 2005, 12:53 pm
Newsgroups: comp.lang.python
From: Peter Hansen <pe...@engcorp.com>
Date: Sat, 08 Jan 2005 12:53:05 -0500
Local: Sat, Jan 8 2005 12:53 pm
Subject: Re: python3: 'where' keyword

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Ribeiro  
View profile  
 More options Jan 8 2005, 5:17 pm
Newsgroups: comp.lang.python
From: Carlos Ribeiro <carribe...@gmail.com>
Date: Sat, 8 Jan 2005 20:17:08 -0200
Local: Sat, Jan 8 2005 5:17 pm
Subject: Re: python3: 'where' keyword

On Sat, 08 Jan 2005 12:53:05 -0500, Peter Hansen <pe...@engcorp.com> 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: carribe...@gmail.com
mail: carribe...@yahoo.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carl Banks  
View profile  
 More options Jan 8 2005, 6:55 pm
Newsgroups: comp.lang.python
From: "Carl Banks" <invalidem...@aerojockey.com>
Date: 8 Jan 2005 15:55:49 -0800
Local: Sat, Jan 8 2005 6:55 pm
Subject: Re: python3: 'where' keyword

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 98   Newer >
« Back to Discussions « Newer topic     Older topic »