Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9891 > unrolled thread
| Started by | Micah <larson.micah@gmail.com> |
|---|---|
| First post | 2011-07-19 10:00 -0700 |
| Last post | 2011-07-19 15:18 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Micah <larson.micah@gmail.com> |
|---|---|
| Date | 2011-07-19 10:00 -0700 |
| Subject | Re: 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]
| From | Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> |
|---|---|
| Date | 2011-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]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-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