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


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

Re: Return and set

Started byMicah <larson.micah@gmail.com>
First post2011-07-19 10:00 -0700
Last post2011-07-19 15:18 -0400
Articles 3 — 3 participants

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


Contents

  Re: Return and set Micah <larson.micah@gmail.com> - 2011-07-19 10:00 -0700
    Re: Return and set Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-19 13:12 -0400
    Re: Return and set Terry Reedy <tjreedy@udel.edu> - 2011-07-19 15:18 -0400

#9891 — Re: Return and set

FromMicah <larson.micah@gmail.com>
Date2011-07-19 10:00 -0700
SubjectRe: Return and set
Message-ID<94577ba1-63ef-4a1b-a150-b004a20755c1@glegroupsg2000goo.googlegroups.com>
That sounds artificially backwards; why not let getToken() reuse peekToken()?

def peek(self):
    if self.tok is None:
        try:
            self.tok = self.gen.next()
        except StopIteration:
            self.tok = NULL
    return self.tok

def pop(self):
    token = self.peek()
    self.tok = None
    return token

[toc] | [next] | [standalone]


#9894

FromBilly Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com>
Date2011-07-19 13:12 -0400
Message-ID<j04dtu$rvs$1@speranza.aioe.org>
In reply to#9891
On 07/19/2011 01:00 PM, Micah wrote:
> That sounds artificially backwards; why not let getToken() reuse peekToken()?
>
> def peek(self):
>      if self.tok is None:
>          try:
>              self.tok = self.gen.next()
>          except StopIteration:
>              self.tok = NULL
>      return self.tok
>
> def pop(self):
>      token = self.peek()
>      self.tok = None
>      return token

I actually like this way better, thanks!

--
Bill

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


#9917

FromTerry Reedy <tjreedy@udel.edu>
Date2011-07-19 15:18 -0400
Message-ID<mailman.1274.1311103132.1164.python-list@python.org>
In reply to#9891
On 7/19/2011 1:00 PM, Micah wrote:
> That sounds artificially backwards; why not let getToken() reuse peekToken()?
>
> def peek(self):
>      if self.tok is None:
>          try:
>              self.tok = self.gen.next()

If this is changed (as intended for the iteration protocol) to

              self.tok = next(self.gen)

then this code (and OP's version) should run as is on both Py2.6/7 and Py3.

>          except StopIteration:
>              self.tok = NULL
>      return self.tok
>
> def pop(self):
>      token = self.peek()
>      self.tok = None
>      return token


-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


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


csiph-web