Python try/except

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

xGeovanni
Posts: 36
Joined: Thu Oct 06, 2011 7:03 pm UTC

Python try/except

Postby xGeovanni » Mon Jan 02, 2012 9:45 pm UTC

I have code similar to this:

Code: Select all

try:
    int(x)
    y = True
except:
    pass

if y:
    x -= 1
    ...


this is the problem area, I assure you

The error is that it x is still a string, and the strange thing is that y is true, why is it that one of these changes is made but not the other?
and these errors appeared while x could become an integer, by the way.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Python try/except

Postby b.i.o » Mon Jan 02, 2012 10:11 pm UTC

This has nothing to do with the try-except statement: "int(x)" doesn't modify x, it returns a new value. So you want "x = int(x)" to make it work. Or some other name, because switching the type of a variable midway through a function is a great way to write confusing code.

masher
Posts: 821
Joined: Tue Oct 23, 2007 11:07 pm UTC
Location: Melbourne, Australia

Re: Python try/except

Postby masher » Mon Jan 02, 2012 10:19 pm UTC

b.i.o wrote: Or some other name, because switching the type of a variable midway through a function is a great way to write confusing code.


Another reason why I'm uneasy around duck-typed languages..

xGeovanni
Posts: 36
Joined: Thu Oct 06, 2011 7:03 pm UTC

Re: Python try/except

Postby xGeovanni » Mon Jan 02, 2012 10:24 pm UTC

b.i.o wrote:This has nothing to do with the try-except statement: "int(x)" doesn't modify x, it returns a new value. So you want "x = int(x)" to make it work. Or some other name, because switching the type of a variable midway through a function is a great way to write confusing code.


Thanks, I can't believe I forgot that.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Python try/except

Postby b.i.o » Tue Jan 03, 2012 5:14 am UTC

masher wrote:Another reason why I'm uneasy around duck-typed languages..

...you can do the same thing in most statically typed languages. If Python were statically typed, the compiler would've complained in this case, but in most languages there's nothing stopping you reusing a name for a different type of thing midway through a function.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Python try/except

Postby EvanED » Tue Jan 03, 2012 6:39 am UTC

You can't, however, change the type of a variable on one branch only.

User avatar
cemper93
Posts: 209
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Python try/except

Postby cemper93 » Wed Jan 04, 2012 6:38 pm UTC

If the error you are catching with the except-statement is only the error that the string x may not actually contain a number, it's better to specify the error you are catching:

Code: Select all

except ValueError: pass

Also, you don't need the variable y. It's more concise to use an else-statement.

Code: Select all

try:
  x = int(x)
except:
  pass
else:
  x -= 1

This will only run the else-block if there was no error.

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Python try/except

Postby RoadieRich » Wed Jan 04, 2012 8:43 pm UTC

If we're going into that, then silencing exceptions like that is almost always bad practice. In the above example, what other errors are going to happen further down the page that could have been caught up here with a descriptive error message?
73, de KE8BSL loc EN26.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Python try/except

Postby b.i.o » Wed Jan 04, 2012 11:33 pm UTC

EvanED wrote:You can't, however, change the type of a variable on one branch only.

Right. I'm not arguing that static types are bad (I spend all day programming in OCaml, which I love), just that the specific thing being referenced to wasn't actually a problem solved by having static types.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests