Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'arguments': 0.07; 'function,': 0.07; 'arguments,': 0.09; 'collections': 0.09; 'instances.': 0.09; 'tuple': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'passing': 0.15; '"*"': 0.16; 'created.': 0.16; 'feature?': 0.16; 'given)': 0.16; 'namedtuple': 0.16; 'namedtuples': 0.16; 'tuple)': 0.16; 'url:gmane': 0.16; 'wrote:': 0.17; 'items.': 0.17; 'url:article': 0.17; 'thanks,': 0.18; 'import': 0.21; 'info.': 0.22; 'tuples': 0.22; 'example': 0.23; 'specified': 0.23; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'separate': 0.27; 'obj': 0.29; "i'm": 0.29; 'code': 0.31; 'url:python': 0.32; 'could': 0.32; 'problem': 0.33; 'to:addr :python-list': 0.33; 'hi,': 0.33; 'list': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'two': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'john': 0.60; 'more.': 0.62; 'between': 0.63; 'more': 0.63; 'differences': 0.65; 'received:74.208': 0.71; 'hoping': 0.72; 'reid': 0.84; 'replacements': 0.84; 'do:': 0.91 Date: Mon, 18 Feb 2013 07:11:24 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Differences creating tuples and collections.namedtuples References: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> In-Reply-To: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:BA14J041ti5w/4xt12FzwJkTw9zlN18p5un2SqoNBrh fRLxmglA7zGJ/Gmgn4ZlToAl8wCqvk78XPIwPL6DpZ8HrnA9Z7 DZJe8+fzRIPL82+kWbsTTszBHyakL+BhbeZBL5eTiIn/xIyx7w +EQwbXNkh+vXQ3IYEqYHft1wYvO65y28Hx/OQEAQnDkyOpCb8F rjJ9i1ZedxVOYAWF6zDDbxtTVc2SYEq3yPft+Cz0GmPkbt88ZW f0uRJtlmmEJGMPcDw7m7zoIy4SrA4hwtflPjjoIiSVq2Vju/0p rLri5c7JP1ATVkAdGsNg+AKuJc+aIPplWssYnzrcVJHxvb/wQ= = 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: 1361189497 news.xs4all.nl 6946 [2001:888:2000:d::a6]:41052 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39085 On 02/18/2013 06:47 AM, John Reid wrote: > Hi, > > I was hoping namedtuples could be used as replacements for tuples in all instances. 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]) You are passing a single list to the constructor, but you specified that the namedtuple was to have 3 items. So you need two more. > > 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 > > 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? > > Thanks, > John. > If you want one item (list or tuple) to act like 3 separate arguments, you could use the "*" operator: b = B( *[1,2,3] ) or in your canSequence function, if you want a namedTuple return t(*[can(i) for i in obj]) -- DaveA