Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'tree': 0.05; 'builtin': 0.09; 'bytes,': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:github': 0.09; 'python': 0.11; 'def': 0.12; 'bytearray': 0.16; 'checking,': 0.16; 'false)': 0.16; 'garrett': 0.16; 'hasattr(obj,': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'iterators': 0.16; 'likewise': 0.16; 'list)': 0.16; 'rarely': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'str)': 0.16; 'str,': 0.16; 'wrote:': 0.18; 'input': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'propose': 0.24; 'asking': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'mix': 0.30; 'work.': 0.31; 'code': 0.31; 'checking': 0.33; "i'd": 0.34; 'created': 0.35; 'problem.': 0.35; 'something': 0.35; 'objects': 0.35; 'there': 0.35; 'false': 0.36; 'returning': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'fact': 0.38; 'that,': 0.38; 'short': 0.38; 'use.': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'new': 0.61; 'simple': 0.61; 'here:': 0.62; 'times': 0.62; 'address': 0.63; 'great': 0.65; 'determine': 0.67; 'inline': 0.74; 'walk': 0.74 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: isiter builtin Date: Sun, 16 Nov 2014 10:04:16 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd98f0.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416128673 news.xs4all.nl 2829 [2001:888:2000:d::a6]:39531 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 2811845220 X-Received-Bytes: 4821 Xref: csiph.com comp.lang.python:80866 Garrett Berg wrote: > I have been working with python for a few years now, and two of my > favorite features about it are iterators and duck typing. The fact that > every iterator under the sun can be accessed with a simple for loop is one > of the most amazing features of python. > > 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?) > > I propose a new builtin to address this problem. I have created code for > it here: > > https://gist.github.com/cloudformdesign/de9b54d30547ddd28ec4 I see no reason not to inline this short function: > def isiter(obj, exclude=(str, bytes, bytearray)): > '''Returns True if object is an iterator. > Returns False for str, bytes and bytearray objects > by default''' > return (False if isinstance(obj, exclude) > else True if hasattr(obj, '__iter__') > else False) Why not return hasattr(obj, "__iter__") and not isinstance(obj, exclude) > This will allow simple type checking on an input to determine whether it > is an iterator. You mean "iterable". I'd like to see a few of your use-cases. I think most the time you don't mix arbitrary iterables, so to walk a tree like ["alpha", "beta", [3, "delta", ["epsilon", "zeta"]]] isinstance(obj, list) would be sufficient. Likewise for ["alpha", iter("abc"), ["beta", ["gamma"]]] isinstance(obj, str) would work.