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


Groups > comp.lang.python > #80863

Re: isiter builtin

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'builtin': 0.09; 'expectation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type,': 0.09; '*always*': 0.16; 'checking,': 0.16; 'choice,': 0.16; 'finney': 0.16; 'garrett': 0.16; 'hasattr(obj,': 0.16; 'ignoring': 0.16; "object's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'written.': 0.16; 'exception': 0.16; 'typing': 0.19; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; 'usually': 0.31; 'provided,': 0.31; 'writes:': 0.31; 'probably': 0.32; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'returning': 0.36; 'should': 0.36; 'wrong': 0.37; 'being': 0.38; 'ben': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'use.': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'times': 0.62; 'reach': 0.63; 'great': 0.65; 'skip:\xe2 10': 0.65; 'believe': 0.68; 'received:125': 0.84; '_free': 0.91; 'choice.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: isiter builtin
Date Sun, 16 Nov 2014 19:23:36 +1100
References <CALW7ahx-7JqxWANNX=JpNFqc0rEghDNMa2kMNiHUYaNzR5WgPw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:K1/aOHKuhNo4eRuIZLXosVrRbHg=
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15876.1416126228.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416126228 news.xs4all.nl 2907 [2001:888:2000:d::a6]:58860
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:80863

Show key headers only | View raw


Garrett Berg <googberg@gmail.com> writes:

> However, there are times when I want to do type checking, and the
> builtin function *isinstance* is of great use.

I would advise that when you think you want type checking, you are
probably being overly restrictive.

> However, this function fails to be satisfactory in returning whether
> the object is a valid iterator. The call hasattr(obj, '__iter__') also
> fails because str and bytes types both have that

Yes, because both ‘str’ and ‘bytes’ objects are valid iterables.


Using ‘isinstance’ in particular is a code smell: it breaks duck typing
(checking not for the object's type, but for how the object behaves,
which is usually what matters) and is almost always the wrong choice.

Not *always* the wrong choice, which is why it's provided, and which is
why I call it a code smell: a strong but not infallible sign something
is wrong with the code as written.

When you reach for ‘isinstance’ (LBYL, which is un-Pythonic), instead
ask yourself: ignoring what type this object is, how do I want it to
behave, and should I just use it in the expectation that it will raise
an exception if it doesn't support that (EAFP, which is very Pythonic)?

-- 
 \     “What is needed is not the will to believe but the will to find |
  `\       out, which is the exact opposite.” —Bertrand Russell, _Free |
_o__)                           Thought and Official Propaganda_, 1928 |
Ben Finney

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


Thread

Re: isiter builtin Ben Finney <ben+python@benfinney.id.au> - 2014-11-16 19:23 +1100

csiph-web