Re: What's the use of the else in try/except/else?

2009-05-22 Thread Lawrence D'Oliveiro
In message 8dc983db-b8c4-4897- a58b-969ca5f8e...@g20g2000vba.googlegroups.com, Beni Cherniavsky wrote: And yes, it's icky - not because of the ``else`` but because aquisition-release done correctly is always an icky pattern. Only in the presence of exceptions. --

Re: What's the use of the else in try/except/else?

2009-05-17 Thread Beni Cherniavsky
[Long mail. You may skip to the last paragraph to get the summary.] On May 12, 12:35 pm, Steven D'Aprano wrote: To really be safe, that should become: try:     rsrc = get(resource) except ResourceError:     log('no more resources available')     raise else:     try:        

Re: What's the use of the else in try/except/else?

2009-05-14 Thread Andre Engels
On Thu, May 14, 2009 at 6:39 AM, ma mabdelka...@gmail.com wrote: A really great use for try/except/else would be if an object is implementing its own __getitem__ method, so you would have something like this: class SomeObj(object):    def __getitem__(self, key):                try:        

Re: What's the use of the else in try/except/else?

2009-05-13 Thread Lawrence D'Oliveiro
In message pan.2009.05.12.09.35...@remove.this.cybersource.com.au, Steven D'Aprano wrote: On Tue, 12 May 2009 09:20:36 +, Steven D'Aprano wrote: It seems pretty straightforward to me. Except of course such a pattern won't work ... I rest my case. --

Re: What's the use of the else in try/except/else?

2009-05-13 Thread Scott David Daniels
greg wrote: kj wrote: Wow. As rationales for syntax constructs go, this has got to be the most subtle one I've ever seen... It's to avoid masking bugs. Suppose you accidentally wrote try: v = mumble.field sys.warming('field was actually there?') except AttributeError: pass

Re: What's the use of the else in try/except/else?

2009-05-13 Thread Steven D'Aprano
On Wed, 13 May 2009 20:44:27 +1200, Lawrence D'Oliveiro wrote: In message pan.2009.05.12.09.35...@remove.this.cybersource.com.au, Steven D'Aprano wrote: On Tue, 12 May 2009 09:20:36 +, Steven D'Aprano wrote: It seems pretty straightforward to me. Except of course such a pattern

Re: What's the use of the else in try/except/else?

2009-05-13 Thread ma
A really great use for try/except/else would be if an object is implementing its own __getitem__ method, so you would have something like this: class SomeObj(object): def __getitem__(self, key): try: #sometype of assertion here based on key type

Re: What's the use of the else in try/except/else?

2009-05-13 Thread Steven D'Aprano
On Thu, 14 May 2009 00:39:35 -0400, ma wrote: A really great use for try/except/else would be if an object is implementing its own __getitem__ method, so you would have something like this: class SomeObj(object): def __getitem__(self, key): try:

Re: What's the use of the else in try/except/else?

2009-05-13 Thread ma
That's great to know! Thanks for that explanation, I am refactoring something and I was going to make ample use of assertion as I thought it was the same as C's assertion without the NDEBUG flag. On Thu, May 14, 2009 at 1:03 AM, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On

Re: What's the use of the else in try/except/else?

2009-05-12 Thread Lawrence D'Oliveiro
In message gu7f97$mt...@reader1.panix.com, kj wrote: I know about the construct: try: # do something except ...: # handle exception else: # do something else ...but I can't come with an example in which the same couldn't be accomplished with [no else] I'd agree. If you

Re: What's the use of the else in try/except/else?

2009-05-12 Thread Steven D'Aprano
On Tue, 12 May 2009 20:23:25 +1200, Lawrence D'Oliveiro wrote: In message gu7f97$mt...@reader1.panix.com, kj wrote: I know about the construct: try: # do something except ...: # handle exception else: # do something else ...but I can't come with an example in which the

Re: What's the use of the else in try/except/else?

2009-05-12 Thread Steven D'Aprano
On Tue, 12 May 2009 09:20:36 +, Steven D'Aprano wrote: On Tue, 12 May 2009 20:23:25 +1200, Lawrence D'Oliveiro wrote: In message gu7f97$mt...@reader1.panix.com, kj wrote: I know about the construct: try: # do something except ...: # handle exception else: # do

Re: What's the use of the else in try/except/else?

2009-05-12 Thread Peter Pearson
On 12 May 2009 09:35:36 GMT, Steven D'Aprano wrote: [snip] To really be safe, that should become: try: rsrc = get(resource) except ResourceError: log('no more resources available') raise else: try: do_something_with(rsrc) finally: rsrc.close()

Re: What's the use of the else in try/except/else?

2009-05-12 Thread Carl Banks
On May 12, 2:35 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 12 May 2009 09:20:36 +, Steven D'Aprano wrote: On Tue, 12 May 2009 20:23:25 +1200, Lawrence D'Oliveiro wrote: In message gu7f97$mt...@reader1.panix.com, kj wrote: I know about the construct:

Re: What's the use of the else in try/except/else?

2009-05-12 Thread greg
kj wrote: Wow. As rationales for syntax constructs go, this has got to be the most subtle one I've ever seen... It's to avoid masking bugs. Suppose you accidentally wrote try: v = mumble.field sys.warming('field was actually there?') except AttributeError: pass Then you

Re: What's the use of the else in try/except/else?

2009-05-11 Thread kj
In w_adny5q3jzyxjrxnz2dnuvz_u2dn...@pdx.net Scott David Daniels scott.dani...@acm.org writes: kj wrote: ... I can't come with an example in which the same couldn't be accomplished with try: # do something # do something else except ...: # handle exception The only

What's the use of the else in try/except/else?

2009-05-10 Thread kj
I know about the construct: try: # do something except ...: # handle exception else: # do something else ...but I can't come with an example in which the same couldn't be accomplished with try: # do something # do something else except ...: # handle exception The

Re: What's the use of the else in try/except/else?

2009-05-10 Thread Scott David Daniels
kj wrote: ... I can't come with an example in which the same couldn't be accomplished with try: # do something # do something else except ...: # handle exception The only significant difference I can come up with is that in the second form, the except clause may be masking some