Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Feature suggestion -- return if true

Reply
Thread Tools

Feature suggestion -- return if true

 
 
zildjohn01
Guest
Posts: n/a
 
      04-11-2011
This is an idea I've had bouncing around in my head for a long time
now. I propose the following syntax:

return? expr

be expanded to

_temp = expr
if _temp: return _temp

It's a pattern I use all the time in my code, and although it's a bit
unorthodox, IMO it's concise, readable, and easily understandable.

Thoughts?
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 9:17 AM, zildjohn01 <(E-Mail Removed)> wrote:
> This is an idea I've had bouncing around in my head for a long time
> now. I propose the following syntax:


Maybe this is more appropriare for the python-ideas list ?

> * *return? expr


This syntax does not fit well within python ideology.

> be expanded to
>
> * *_temp = expr
> * *if _temp: return _temp


This could be simplified to just:

return expr or None

And more to the point... If your calee is relying
on the result of this function, just returning the
evaluation of "expr" is enough.

cheers
James

--
-- James Mills
--
-- "Problems are solved by method"
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 10:27 AM, James Mills
<(E-Mail Removed)> wrote:
> This could be simplified to just:
>
> return expr or None
>
> And more to the point... If your calee is relying
> on the result of this function, just returning the
> evaluation of "expr" is enough.


I'm thinking here that that's not a solution; he'll have more code to
follow. An example of what I think he's trying to do:

def fac(n):
# attempt to get from a cache
return? cache[n]
# not in cache, calculate the value
ret=1 if n<=1 else fac(n-1)*n
# and cache and return it
cache[n]=ret; return ret

If the rest of the function can be implemented as an expression, it
might be possible to use:

return expr or other_expr

But in the example of a lookup cache, that wouldn't work so easily -
assignment isn't an expression. If 'x=y' had a value as it does in C,
the above function could become:

def fac(n):
return cache[n] or (cache[n]=1 if n<=1 else fac(n-1)*n)

which is a reasonable one-liner, albeit not the most efficient
factorial implementation. Is there a simple and Pythonic way to do
this?

BTW, assume for the purposes of discussion that the return? expr is a
complex one, such that it's well worth evaluating only once (maybe
even has side effects).

Chris Angelico
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 10:46 AM, Chris Angelico <(E-Mail Removed)> wrote:
> def fac(n):
> * *return cache[n] or (cache[n]=1 if n<=1 else fac(n-1)*n)


Hmm. The function-call version of dictionary assignment IS legal in an
expression, but it's getting stupid...

def fac(n):
return cache.get(n) or (cache.__setitem__(n,1 if n<=1 else
fac(n-1)*n) or cache[n])

Chris Angelico
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-12-2011
On 2011-04-12, James Mills <(E-Mail Removed)> wrote:
> On Tue, Apr 12, 2011 at 9:17 AM, zildjohn01 <(E-Mail Removed)> wrote:
>> This is an idea I've had bouncing around in my head for a long time
>> now. I propose the following syntax:

>
> Maybe this is more appropriare for the python-ideas list ?
>
>> ?? ??return? expr

>
> This syntax does not fit well within python ideology.
>
>> be expanded to
>>
>> ?? ??_temp = expr
>> ?? ??if _temp: return _temp

>
> This could be simplified to just:
>
> return expr or None


How is that the same?

return? something() return something() or None
return? somethingelse() return somethingelse() or None
log("didn't find an answer") log("didn't find an answer")
raise ValueError raise ValueError

Are you saying the two snippets above are equivalent?

--
Grant


 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 11:44 AM, Grant Edwards <(E-Mail Removed)> wrote:
> How is that the same?
>
> *return? something() * * * * * * * * *return something() or None
> *return? somethingelse() * * * * * * *return somethingelse() or None
> *log("didn't find an answer") * * * * log("didn't find an answer")
> *raise ValueError * * * * * * * * * * raise ValueError
>
> Are you saying the two snippets above are equivalent?


def foo(n):
x = n < 5
if x:
return x

is functionally equivalent to:

def foo(n):
return n < 5

--
-- James Mills
--
-- "Problems are solved by method"
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      04-12-2011
On 2011-04-12, James Mills <(E-Mail Removed)> wrote:
> On Tue, Apr 12, 2011 at 11:44 AM, Grant Edwards <(E-Mail Removed)> wrote:
>> How is that the same?
>>
>> ??return? something() ?? ?? ?? ?? ?? ?? ?? ?? ??return something() or None
>> ??return? somethingelse() ?? ?? ?? ?? ?? ?? ??return somethingelse() or None
>> ??log("didn't find an answer") ?? ?? ?? ?? log("didn't find an answer")
>> ??raise ValueError ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? raise ValueError
>>
>> Are you saying the two snippets above are equivalent?

>
> def foo(n):
> x = n < 5
> if x:
> return x
>
> is functionally equivalent to:
>
> def foo(n):
> return n < 5


That's not what I asked.

You stated that

return? <expr>

was equivalent to

return <expr> or None

If that was the case then the two code snippets _I_ posted should be
equivalent:

return? something() return something() or None
return? somethingelse() return somethingelse() or None
log("didn't find an answer") log("didn't find an answer")
raise ValueError raise ValueError

If the two snipped above are not equivalent, then

return? <expr>

is isn't equivalent to

return <expr> or None

--
Grant


 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 12:18 PM, Jason Swails <(E-Mail Removed)> wrote:
> This is only true if n < 5.* Otherwise, the first returns None and the
> second returns False.


Which is why I said:

return expr or None

But hey let's argue the point to death!

cheers
James

--
-- James Mills
--
-- "Problems are solved by method"
 
Reply With Quote
 
Zero Piraeus
Guest
Posts: n/a
 
      04-12-2011
:

>> This is only true if n < 5.* Otherwise, the first returns None and the
>> second returns False.

>
> Which is why I said:
>
> return expr or None
>
> But hey let's argue the point to death!


Ok

I think the point is that OP doesn't want to return *at all* if expr
is False - presumably because there are further statements after the
proposed 'conditional' return.

Anyway,

return? expr

isn't very pythonic - so how about one of these?

return expr if True
return expr else continue

I kid, I kid ...

-[]z.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      04-12-2011
On Tue, Apr 12, 2011 at 12:20 PM, James Mills
<(E-Mail Removed)> wrote:
> On Tue, Apr 12, 2011 at 12:18 PM, Jason Swails <(E-Mail Removed)> wrote:
>> This is only true if n < 5.* Otherwise, the first returns None and the
>> second returns False.

>
> Which is why I said:
>
> return expr or None
>
> But hey let's argue the point to death!


That's still not equivalent. "return expr or None" will always
terminate the function. The OP's request was for something which would
terminate the function if and only if expr is non-false.

Chris Angelico
 
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
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
Suggestion for an "imagemap-like" location feature -- methods? KatB ASP .Net 4 01-22-2005 01:46 AM
feature suggestion flexibal Python 3 12-24-2004 11:36 PM
Does true ^ true return false? Siemel Naran C++ 19 06-18-2004 11:06 AM
difference between return true; and return false; w i l l Javascript 4 07-04-2003 10:05 PM



Advertisments