Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

python3: 'where' keyword

 
 
michele.simionato@gmail.com
Guest
Posts: n/a
 
      01-08-2005

> 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

 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      01-08-2005
Nick Coghlan wrote:
> 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

>

[snip]
> For compound statements, a where clause probably isn't appropriate,

as it would
> be rather unclear what the where clause applied to.


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

 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      01-08-2005
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 | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-08-2005
Nick Coghlan <(E-Mail Removed)> 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.
 
Reply With Quote
 
AdSR
Guest
Posts: n/a
 
      01-08-2005
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-08-2005
AdSR <(E-Mail Removed)> 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.
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      01-08-2005
On Sat, 08 Jan 2005 16:42:16 +1000, Nick Coghlan <(E-Mail Removed)> wrote:

>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"
>


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
 
Reply With Quote
 
Andrey Tatarinov
Guest
Posts: n/a
 
      01-08-2005
Bengt Richter 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"

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

....
> 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


yes, all your examples are correct. And that's the way I'd like to use
this feature.
 
Reply With Quote
 
Andrey Tatarinov
Guest
Posts: n/a
 
      01-08-2005
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
 
Reply With Quote
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      01-08-2005
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

 
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