Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3003 > unrolled thread

Feature suggestion -- return if true

Started byzildjohn01 <zildjohn01@gmail.com>
First post2011-04-11 16:17 -0700
Last post2011-04-12 11:12 -0400
Articles 7 on this page of 67 — 29 participants

Back to article view | Back to comp.lang.python


Contents

  Feature suggestion -- return if true zildjohn01 <zildjohn01@gmail.com> - 2011-04-11 16:17 -0700
    Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 10:27 +1000
      Re: Feature suggestion -- return if true Grant Edwards <invalid@invalid.invalid> - 2011-04-12 01:44 +0000
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 12:12 +1000
          Re: Feature suggestion -- return if true Grant Edwards <invalid@invalid.invalid> - 2011-04-12 02:18 +0000
            Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 12:44 +1000
              Re: Feature suggestion -- return if true Grant Edwards <invalid@invalid.invalid> - 2011-04-12 13:42 +0000
              Re: Feature suggestion -- return if true "Martin v. Loewis" <martin@v.loewis.de> - 2011-04-17 12:03 +0200
                Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-18 09:36 +1000
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 12:20 +1000
        Re: Feature suggestion -- return if true Zero Piraeus <schesis@gmail.com> - 2011-04-11 22:43 -0400
        Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-12 12:44 +1000
        Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-12 12:49 +1000
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 12:59 +1000
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 13:01 +1000
          Re: Feature suggestion -- return if true Nobody <nobody@nowhere.com> - 2011-04-12 07:08 +0100
            Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-12 16:21 +1000
              Re: Feature suggestion -- return if true Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-12 10:52 +0000
              Re: Feature suggestion -- return if true scattered <tooscattered@gmail.com> - 2011-04-12 03:55 -0700
                Re: Feature suggestion -- return if true "Colin J. Williams" <cjw@ncf.ca> - 2011-04-12 10:01 -0400
              Re: Feature suggestion -- return if true Grant Edwards <invalid@invalid.invalid> - 2011-04-12 13:50 +0000
          Re: Feature suggestion -- return if true Grant Edwards <invalid@invalid.invalid> - 2011-04-12 13:44 +0000
        Re: Feature suggestion -- return if true Westley Martínez <anikom15@gmail.com> - 2011-04-12 07:05 -0700
          Re: Feature suggestion -- return if true scattered <tooscattered@gmail.com> - 2011-04-12 07:58 -0700
            Re: Feature suggestion -- return if true Westley Martínez <anikom15@gmail.com> - 2011-04-12 15:45 -0700
            Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-13 08:52 +1000
          Re: Feature suggestion -- return if true zildjohn01 <zildjohn01@gmail.com> - 2011-04-12 11:25 -0700
            Re: Feature suggestion -- return if true Terry Reedy <tjreedy@udel.edu> - 2011-04-12 15:14 -0400
            Re: Feature suggestion -- return if true alex23 <wuwei23@gmail.com> - 2011-04-12 21:05 -0700
      Re: Feature suggestion -- return if true Teemu Likonen <tlikonen@iki.fi> - 2011-04-12 21:00 +0300
        Re: Feature suggestion -- return if true Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-12 12:25 -0600
          Re: Feature suggestion -- return if true Neil Cerutti <neilc@norwich.edu> - 2011-04-12 20:13 +0000
            Re: Feature suggestion -- return if true Neil Cerutti <neilc@norwich.edu> - 2011-04-12 20:16 +0000
        Re: Feature suggestion -- return if true Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-12 12:29 -0600
        Re: Feature suggestion -- return if true Paul Rudin <paul.nospam@rudin.co.uk> - 2011-04-12 19:28 +0100
        Re: Feature suggestion -- return if true Chris Rebert <clp2@rebertia.com> - 2011-04-12 13:26 -0700
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-13 08:34 +1000
        Re: Feature suggestion -- return if true Ethan Furman <ethan@stoneleaf.us> - 2011-04-12 16:06 -0700
          Re: Feature suggestion -- return if true Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-21 12:13 +0200
        Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-13 09:12 +1000
        Re: Feature suggestion -- return if true Westley Martínez <anikom15@gmail.com> - 2011-04-12 16:15 -0700
        Re: Feature suggestion -- return if true Ethan Furman <ethan@stoneleaf.us> - 2011-04-12 16:48 -0700
          Re: Feature suggestion -- return if true Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-13 00:03 +0000
            Re: Feature suggestion -- return if true Ethan Furman <ethan@stoneleaf.us> - 2011-04-12 19:42 -0700
            Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-13 13:00 +1000
            Re: Feature suggestion -- return if true James Mills <prologic@shortcircuit.net.au> - 2011-04-13 13:28 +1000
        Re: Feature suggestion -- return if true Teemu Likonen <tlikonen@iki.fi> - 2011-04-13 13:58 +0300
    Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-12 10:46 +1000
      Re: Feature suggestion -- return if true Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-04-17 16:21 +1200
        Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-17 14:31 +1000
        Re: Feature suggestion -- return if true Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-17 08:45 +0000
          Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-17 19:07 +1000
            Re: Feature suggestion -- return if true Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-17 15:23 +0000
          Re: Feature suggestion -- return if true Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-04-18 12:25 +1200
          Re: Feature suggestion -- return if true Dave Angel <davea@ieee.org> - 2011-04-17 22:04 -0400
          Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-18 12:10 +1000
            Re: Feature suggestion -- return if true Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-04-19 12:35 +1200
              Re: Feature suggestion -- return if true Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-04-19 09:42 +0300
                Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-19 17:01 +1000
        Re: Feature suggestion -- return if true "D'Arcy J.M. Cain" <darcy@druid.net> - 2011-04-17 08:33 -0400
          Re: Feature suggestion -- return if true Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-17 15:10 +0000
          Re: Feature suggestion -- return if true aahz@pythoncraft.com (Aahz) - 2011-04-18 09:11 -0700
        Re: Feature suggestion -- return if true Greg Ewing <greg.ewing@canterbury.ac.nz> - 2011-04-18 11:09 +1200
    Re: Feature suggestion -- return if true Chris Angelico <rosuav@gmail.com> - 2011-04-12 10:51 +1000
    Re: Feature suggestion -- return if true Paul Rubin <no.email@nospam.invalid> - 2011-04-11 23:58 -0700
      Re: Feature suggestion -- return if true John Roth <johnroth1@gmail.com> - 2011-04-12 06:16 -0700
      Re: Feature suggestion -- return if true Mel <mwilson@the-wire.com> - 2011-04-12 11:12 -0400

Page 4 of 4 — ← Prev page 1 2 3 [4]


#3400

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-04-17 15:10 +0000
Message-ID<4dab02f7$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#3394
On Sun, 17 Apr 2011 08:33:47 -0400, D'Arcy J.M. Cain wrote:

> On Sun, 17 Apr 2011 16:21:53 +1200
> Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
>> My idiom for fetching from a cache looks like this:
>> 
>>    def get_from_cache(x):
>>      y = cache.get(x)
>>      if not y:
>>        y = compute_from(x)
>>        cache[x] = y
>>      return y
> 
> I prefer not to create and destroy objects needlessly.
> 
>  def get_from_cache(x):
>    if not x in cache:
>      cache[x] = compute_from(x)
>    return cache[x]

What object(s) do you think are being created and destroyed needlessly?

(This is not a rhetorical question.)


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#3491

Fromaahz@pythoncraft.com (Aahz)
Date2011-04-18 09:11 -0700
Message-ID<iohnqs$i7o$1@panix5.panix.com>
In reply to#3394
In article <mailman.461.1303043638.9059.python-list@python.org>,
D'Arcy J.M. Cain <darcy@druid.net> wrote:
>On Sun, 17 Apr 2011 16:21:53 +1200
>Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
>> My idiom for fetching from a cache looks like this:
>> 
>>    def get_from_cache(x):
>>      y = cache.get(x)
>>      if not y:
>>        y = compute_from(x)
>>        cache[x] = y
>>      return y
>
>I prefer not to create and destroy objects needlessly.
>
> def get_from_cache(x):
>   if not x in cache:
>     cache[x] = compute_from(x)
>   return cache[x]

Aside from Steven's entirely appropriate pointed question, I find this
more readable:

    if x not in cache:

Without testing, I'm not sure, but I believe it's more efficient, too
(creates fewer bytecodes).
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22

[toc] | [prev] | [next] | [standalone]


#3434

FromGreg Ewing <greg.ewing@canterbury.ac.nz>
Date2011-04-18 11:09 +1200
Message-ID<mailman.483.1303081774.9059.python-list@python.org>
In reply to#3368
D'Arcy J.M. Cain wrote:
> On Sun, 17 Apr 2011 16:21:53 +1200
> Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
> 
>>   def get_from_cache(x):
>>     y = cache.get(x)
>>     if not y:
>>       y = compute_from(x)
>>       cache[x] = y
>>     return y
> 
> I prefer not to create and destroy objects needlessly.

How does that create objects needlessly?

>  def get_from_cache(x):
>    if not x in cache:
>      cache[x] = compute_from(x)
>    return cache[x]

That looks up the cache *twice* for every access. Mine
only does one lookup when the item is already in the
cache.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#3010

FromChris Angelico <rosuav@gmail.com>
Date2011-04-12 10:51 +1000
Message-ID<mailman.235.1302569468.9059.python-list@python.org>
In reply to#3003
On Tue, Apr 12, 2011 at 10:46 AM, Chris Angelico <rosuav@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#3030

FromPaul Rubin <no.email@nospam.invalid>
Date2011-04-11 23:58 -0700
Message-ID<7xoc4cymz7.fsf@ruckus.brouhaha.com>
In reply to#3003
zildjohn01 <zildjohn01@gmail.com> writes:
>     _temp = expr
>     if _temp: return _temp

I'm trying to figure out a context where you'd even want that, and I'm
thinking that maybe it's some version of a repeat-until loop?  Python
doesn't have repeat-until and it's been proposed a few times.

[toc] | [prev] | [next] | [standalone]


#3044

FromJohn Roth <johnroth1@gmail.com>
Date2011-04-12 06:16 -0700
Message-ID<0edd1d5c-9daa-4e23-be10-9014743deb0d@k22g2000yqh.googlegroups.com>
In reply to#3030
On Apr 12, 12:58 am, Paul Rubin <no.em...@nospam.invalid> wrote:
> zildjohn01 <zildjoh...@gmail.com> writes:
> >     _temp = expr
> >     if _temp: return _temp
>
> I'm trying to figure out a context where you'd even want that, and I'm
> thinking that maybe it's some version of a repeat-until loop?  Python
> doesn't have repeat-until and it's been proposed a few times.

I've wanted that a few times, frequently in a situation where I'm
doing validity checking, and I either want to continue after calling a
validity check or return immediately with some kind of error object.
For example:

returnif self.isitaboojum(snark)

This works nicely if the checking routine either returns False or an
error object.

It also works if the method is doing some kind of a ladder evaluation
of several distinct conditions, and it wants to return the first one
it finds.

Also, I wouldn't want a returnif in a loop. Following on with this
idea, loop control would be more of a breakif or continueif statement.

John Roth

[toc] | [prev] | [next] | [standalone]


#3055

FromMel <mwilson@the-wire.com>
Date2011-04-12 11:12 -0400
Message-ID<io1q47$qkr$1@speranza.aioe.org>
In reply to#3030
Paul Rubin wrote:

> zildjohn01 <zildjohn01@gmail.com> writes:
>>     _temp = expr
>>     if _temp: return _temp
> 
> I'm trying to figure out a context where you'd even want that, and I'm
> thinking that maybe it's some version of a repeat-until loop?  Python
> doesn't have repeat-until and it's been proposed a few times.

I can imagine

    return? tree_node.left
    return? tree_node.right

although my real code would probably be more like

    if tree_node.left is not None:
        return "left", tree_node.left
    if tree_node.right is not None:
        return "right", tree_node.right

where adding the "left" and "right" markers makes the return? feature 
impossible to use.

The proposed feature reminds me of the `zod` function (was that the actual 
name?) that returned 0 rather than bringing on a ZeroDivideError.  It would 
cement a strange corner-case into the language.

	Mel.

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.python


csiph-web