Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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; 'else:': 0.03; 'arguments': 0.07; 'try:': 0.07; 'collections': 0.09; 'instances.': 0.09; 'list).': 0.09; 'received:209.85.212.54': 0.09; 'tuple': 0.09; 'typeerror:': 0.09; 'types:': 0.09; 'unpack': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; '(but': 0.15; '(assuming': 0.16; '(rather': 0.16; 'above?': 0.16; 'cc:name:python list': 0.16; 'created.': 0.16; 'feature?': 0.16; 'given)': 0.16; 'hashable': 0.16; 'immutable,': 0.16; 'namedtuple': 0.16; 'namedtuples': 0.16; 'object).': 0.16; 'url:gmane': 0.16; 'wrote:': 0.17; 'url:article': 0.17; 'module': 0.19; 'import': 0.21; 'info.': 0.22; 'object.': 0.22; 'tuples': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'obj': 0.29; 'objects': 0.29; "i'm": 0.29; 'code': 0.31; 'point': 0.31; 'url:python': 0.32; 'could': 0.32; 'function.': 0.33; 'turns': 0.33; 'values.': 0.33; 'problem': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'url:org': 0.36; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'takes': 0.39; 'where': 0.40; 'think': 0.40; 'john': 0.60; 'provide': 0.62; 'between': 0.63; 'more': 0.63; 'differences': 0.65; 'hoping': 0.72; 'special': 0.73; '2013': 0.84; 'anywhere.': 0.84; 'oscar': 0.84; 'reid': 0.84; 'replacements': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=QTj6VNkNRsPKWKGCVvg9i/GEGE4qPAqHWDnAzBdWaKM=; b=vFZD5JxOjNB01J55GHDQnmOMP0pYShxBlE9rly+vonyNc2krOn/06/1vRWjcAckmtz Lsb6uu2+R0UUcTxuKO6UmXRtZNDUliTA/HpQMnMTElifJYzF8QIsd2uC88x8DyCVZUPX rgL0O4Uj3Wc2KRuPuhvEPurJyXF36/cTJcSa4+bH9MZUM0j4CUjcuGcuQyLhM0h+VHt2 OHqlhaPNfeisVDyI/daY4vcSRuEwQKfPUwiXmXhC4cajq+MO4o/jSmt0dgv0dw7xn3OL WA9sjdTZv3P6Ry81FbF9SXDM9jfX/Ycmy/4AqG2mvGs7zbplPKQNQZnuMO4ytzChgXNj ElsQ== X-Received: by 10.220.149.11 with SMTP id r11mr14771543vcv.44.1361189014183; Mon, 18 Feb 2013 04:03:34 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> References: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> From: Oscar Benjamin Date: Mon, 18 Feb 2013 12:03:14 +0000 Subject: Re: Differences creating tuples and collections.namedtuples To: John Reid Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361189017 news.xs4all.nl 6980 [2001:888:2000:d::a6]:35158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39083 On 18 February 2013 11:47, John Reid wrote: > Hi, > > I was hoping namedtuples could be used as replacements for tuples in all instances. namedtuples are not really intended to serves as tuples anywhere. They are intended to provide lightweight, immutable, hashable objects with *named* (rather than numbered) values. > There seem to be some differences between how tuples and namedtuples are created. For example with a tuple I can do: > > a=tuple([1,2,3]) > > with namedtuples I get a TypeError: > > from collections import namedtuple > B=namedtuple('B', 'x y z') > b=B([1,2,3]) For a namedtuple you need to unpack the arguments b = B(*[1, 2, 3]) or b = B(1, 2, 3) > > TypeError: __new__() takes exactly 4 arguments (2 given) >> (3)() > 1 from collections import namedtuple > 2 B=namedtuple('B', 'x y z') > ----> 3 b=B([1,2,3]) > > I'm seeing this problem because of the following code in IPython: > > def canSequence(obj): > if isinstance(obj, (list, tuple)): > t = type(obj) > return t([can(i) for i in obj]) > else: > return obj What is the point of the code above? If obj is a list or a tuple you create a new list or tuple with the same data and then return it otherwise you just return the object. What about: def canSequence(obj): return obj Or is it that you want to copy the object (but only when it is a tuple or list). Then how about def canSequence(obj): if isinstance(obj, (list, tuple)): return obj[:] else: return obj Note that this turns namedtuples into tuples. It might be better to catch TypeError rather than special casing the types: def canSequence(obj): try: return obj[:] except TypeError: return obj Or perhaps it would be better to use the copy module (assuming that the purpose is to copy the object). > > where obj is a namedtuple and t([can(i) for i in obj]) fails with the TypeError. See http://article.gmane.org/gmane.comp.python.ipython.user/10270 for more info. > > Is this a problem with namedtuples, ipython or just a feature? I think that it is a problem with the canSequence function. Oscar