Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; '16,': 0.03; 'builtin': 0.09; 'correct,': 0.09; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'checking,': 0.16; 'collections': 0.16; 'dict': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garrett': 0.16; 'hasattr(obj,': 0.16; 'iterable': 0.16; 'iterable,': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterators,': 0.16; 'loop.': 0.16; 'normally,': 0.16; 'over:': 0.16; 'range,': 0.16; 'rarely': 0.16; 'slight': 0.16; 'which,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'basically': 0.19; '>>>': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'bytes': 0.24; 'module,': 0.24; 'skip:i 40': 0.24; 'cc:2**0': 0.24; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'though.': 0.31; 'becomes': 0.33; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'raising': 0.36; 'returning': 0.36; 'doing': 0.36; 'nov': 0.38; 'list,': 0.38; 'pm,': 0.38; 'that,': 0.38; 'use.': 0.39; 'simply': 0.61; "you're": 0.61; 'times': 0.62; 'different': 0.65; 'great': 0.65; '100%': 0.77; "'for'": 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=a0dyCabIhYx2R2wqIPe/QwKoXhjMrUN2/zoDkuygiSk=; b=Ur/eIWkzt0qmfMnxUXU6U/s8Chnf5bbrXsTu4AQK7Ic6iGOsEwkFEtmlCAL7Ce7teY peQcUO294v9DvVotA1xyVCOK15xPP0dEKfXXe50wQTFefTYJFl76sRPHrPubHNaSvTKv 1cBNUySysGbjVw7Yez4ftAYJ9ruP/Cf84zOVTY9AWTTRScdY5SLX3wv3SEgZm0dXu9DC UqXfB2QW0/ismxRksgCBukj/cWAR6CmYg370BDDlRUPwWP5Qjh6LAktLsAiwfrUrAWlN IbDwm+na+vne80onvaup/y6Z7QlLOMeMm47al3qumGlp+5BnEXNDRBP9oSuQ3lat/ERr kPJQ== MIME-Version: 1.0 X-Received: by 10.43.76.199 with SMTP id zf7mr637938icb.57.1416125650733; Sun, 16 Nov 2014 00:14:10 -0800 (PST) In-Reply-To: References: Date: Sun, 16 Nov 2014 19:14:10 +1100 Subject: Re: isiter builtin From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416125654 news.xs4all.nl 2955 [2001:888:2000:d::a6]:55901 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80862 On Sun, Nov 16, 2014 at 6:57 PM, Garrett Berg wrote: > However, there are times when I want to do type checking, and the builtin > function isinstance is of great use. 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, and are rarely what you mean when you are asking if something is an > iterator (how often have you iterated over a string?) But the correct answer is that str and bytes are both iterable. So what you're asking is not whether something's a valid iterator, but whether it is a non-string-iterable, which is a definitely different thing. And that's basically what your gist is doing, although there's a slight error in its docstring: you talk about "iterators", but you're actually looking for "iterables". In Python, an iterable is something which you can iterate over: calling iter() on it will return something (rather than raising TypeError), which means you can use it in a 'for' loop. An iterator is an iterable which, when you call iter() on it, returns *itself*. Normally, you get an iterator by calling iter() on something iterable, if that makes sense. For example: >>> iter([1,2,3,4]) >>> iter(_) >>> iter(range(10)) >>> iter(_) >>> iter(globals()) >>> iter(_) The list, range, and dict (as returned by globals()) are all iterable; the ...iterator objects are iterators. I suspect that your use-case is actually looking for iterables, not iterators, so the fix is simply a docs change. At that point, your function becomes 100% correct, and a perfectly idiomatic way to express "iterable that isn't one of these types". It doesn't need to be a builtin, though. Incidentally, if you're doing a lot of isinstance tests like this, you might want to look at the collections module, which has some 'base classes' which can be used like that. >>> isinstance([1,2,3],collections.Iterable) True ChrisA