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


Groups > comp.lang.python > #52629

Re: Check for the type of arguments

References <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date 2013-08-17 15:38 +0100
Subject Re: Check for the type of arguments
Newsgroups comp.lang.python
Message-ID <mailman.15.1376750347.23369.python-list@python.org> (permalink)

Show all headers | View raw


On 17 August 2013 13:34, Fernando Saldanha <fsaldan1@gmail.com> wrote:
> I am new to Python, with some experience in Java, C++ and R.
>
> Writing in other languages I usually check the type and values of function arguments. In the Python code examples I have seen this is rarely done.
>
> Questions:
>
> 1) Is this because it would be "unpythonic" or just because the examples are not really production code?

Unpythonic. Python duck-types so we tend to take things as long as the
seem like they work. This is really helpful if you want to provide a
custom type or data object to a function (or anything really). If it
looks like a duck and quacks like a duck... you can probably make a
duck sandwich.

> 2) If I still want to check the type of my arguments, do I
>
> a) use type() or is instance() to check for type?

You'd want to travel down this stack, choosing the first reasonable one:

• Don't check at all

• Check that it can do what you need it to do, such as by calling
"iter(input)" to check that it's iterable.

• Check using an ABC (http://docs.python.org/3/library/abc.html) with isinstance

• Check that it has the methods you need using hasattr

• Check using "isinstance(...)" against a type

• Check using "type(...) is"

The choices higher up are better than the choices lower down.

> b) use assert (I guess not),

"assert" is for things that *can't* be wrong (yet still sometimes
are). Don't normally assert user input, I'd say.

>raise a ValueError,

Sounds right.

>or sys.exit()?

No. You should never be throwing a SystemExit except at top-level.

> (I noticed that raising a ValueError does not stop execution when I am running the Interactive Interpreter under PTVS, which I find inconvenient, but it does stop execution when running the code non-interactively.)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Check for the type of arguments Fernando Saldanha <fsaldan1@gmail.com> - 2013-08-17 05:34 -0700
  Re: Check for the type of arguments Skip Montanaro <skip@pobox.com> - 2013-08-17 07:55 -0500
  Re: Check for the type of arguments Chris Angelico <rosuav@gmail.com> - 2013-08-17 14:31 +0100
  Re: Check for the type of arguments Joshua Landau <joshua@landau.ws> - 2013-08-17 15:38 +0100
  Re: Check for the type of arguments Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-17 15:46 +0000
  Re: Check for the type of arguments Fernando Saldanha <fsaldan1@gmail.com> - 2013-08-17 09:00 -0700

csiph-web