Newbie naive question ... int() throws ValueError

Devin Jeanpierre jeanpierreda at gmail.com
Sat May 12 14:25:17 EDT 2012


On Sat, May 12, 2012 at 8:27 AM, Karl Knechtel <zahlman at gmail.com> wrote:
> I really wish gmail picked up the mailing list as a default reply-to address...

There is some labs thing that makes "reply to all" the default if you
click the button on the top-right. Unfortunately, that applies for
non-mailing-lists too...

> The easiest way is to try it. In fact, in most cases, this will be
> easier than looking it up in the documentation could ever be, because
> looking something up in documentation requires you to read through a
> bunch of documentation until you find key info like 'raises FooError
> in bar circumstance', and then interpret it; by trial and error, you
> see exactly what happens right away, and you can do it by just banging
> out a couple of lines on the REPL.

Eh, this doesn't make sense. Before I would ever try the int()
function, I would've read about it in the documentation -- otherwise
how would I know what it does at all? How would I even know that it exists?

What having to try-it-and-see does is give me extra steps to know what
it does. Instead of only reading the documentation, now I have to both
read the documentation *and* try it out in the interactive interpreter
to see its behaviour in a bunch of undocumented error cases.

What should actually happen is that the error cases should be
documented explicitly, so that I don't have to run around in the REPL
or orthogonal bits of documentation trying to figure out the behaviour
of the function.

> You should have already read the documentation for things like
> ValueError as a part of learning the language (i.e. the first time
> something raised a ValueError and you wanted to know what that meant);
> or at the very least you should have a good idea of what the
> "standard" exceptions are all named, and *develop an intuition* for
> which apply to what circumstances. As noted by others, you should not
> expect `int` to throw a `TypeError` when fed a bad string, because a
> `TypeError` means "something is wrong with the type of some object",
> and the problem with the bad string isn't that it's a string (its
> type), but that the string contents are not interpretable as int (its
> value, hence ValueError).

And yet this is what ord does. Every rule has an exception.

I concede that reading the docs on ValueError is probably worth doing
the first time you see it. Although, it's also one of those exceptions
that has a "clear" name, so it isn't something everyone will do. I'm
not sure if I ever did it. And certainly the OP never looked there.

-- Devin



More information about the Python-list mailing list