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


Groups > comp.lang.python > #52629

Re: Check for the type of arguments

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'interpreter': 0.05; 'is"': 0.07; 'arguments,': 0.09; 'input,': 0.09; 'stack,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"assert"': 0.16; 'code?': 0.16; 'ptvs,': 0.16; 'rarely': 0.16; 'say.': 0.16; 'subject:type': 0.16; 'systemexit': 0.16; 'url:html)': 0.16; 'valueerror': 0.16; 'valueerror,': 0.16; 'java,': 0.16; 'do,': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'normally': 0.19; 'examples': 0.20; 'cc:addr:python.org': 0.22; 'choices': 0.24; 'questions:': 0.24; 'tend': 0.24; 'helpful': 0.24; 'looks': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'right.': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'usually': 0.31; 'assert': 0.31; 'probably': 0.32; 'languages': 0.32; 'url:python': 0.33; 'running': 0.33; 'guess': 0.33; 'noticed': 0.34; 'subject:the': 0.34; "i'd": 0.34; 'except': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'c++': 0.36; 'choosing': 0.36; 'raising': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'sometimes': 0.38; 'url:library': 0.38; 'anything': 0.39; 'does': 0.39; 'august': 0.61; 'lower': 0.61; 'new': 0.61; 'no.': 0.61; 'url:3': 0.61; 'first': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'to:addr:gmail.com': 0.65; 'production': 0.68; 'abc': 0.84; 'subject:Check': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=18ZD6i4Ev6atAXU1We4/bplvM6oFYtHExPF0LcKNPYs=; b=y8EQRpWRExgR41fHNBXezmVeTnK2wMMEtJ2VZSZsyJ2uX9szXBcRptEd7gbpLEdewQ asHgf4hORUxr44ZDytdF1FdgVQTOfvUKrkTCIfYURceF0Zg0Ys9QR6v6NhMSkOqN5aCf aOXUXavycqP9DiH0W/j2FD1rog2mqgcnQzNQNXJiLrlzYNg+eP0NnA68LtX6jikojaJw qRuDelCYhC/iD1NkOlbgZ7zQ5vrtL0LUt6m4wdp5ezEJqAXBGnJW53ECw9YjJro2A2Ha ZVj46hDmCjMhYrrq7iNQ5ktG4srfTGIfgE3hxH+teWLvOCBjf1TbGqM/U4L5q8YjTjk/ EGfQ==
X-Received by 10.152.2.226 with SMTP id 2mr3083613lax.14.1376750338009; Sat, 17 Aug 2013 07:38:58 -0700 (PDT)
MIME-Version 1.0
Sender joshua.landau.ws@gmail.com
In-Reply-To <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com>
References <185a0a88-9515-43e6-ae65-73d86b0299e7@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date Sat, 17 Aug 2013 15:38:17 +0100
X-Google-Sender-Auth LjLntRYkpN9QiyxnxFSBdKr868c
Subject Re: Check for the type of arguments
To Fernando Saldanha <fsaldan1@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15.1376750347.23369.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376750347 news.xs4all.nl 15924 [2001:888:2000:d::a6]:49889
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52629

Show key headers only | 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