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


Groups > comp.lang.python > #63109

Re: Strange behaviour with a for loop.

Path csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'string': 0.09; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'iterate': 0.09; 'spelled': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; '%s\\n"': 0.16; "'test',": 0.16; 'be:': 0.16; 'filename)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'string:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '(in': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'instance,': 0.24; 'sean': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'yields': 0.31; 'file': 0.32; 'another': 0.32; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'list': 0.37; 'list.': 0.37; 'pm,': 0.38; 'explain': 0.39; 'does': 0.39; "you're": 0.61; 'first': 0.61; 'useful.': 0.68; "'test'": 0.84; 'items,': 0.91; '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=GXYHP9NJgOcXeTEgIrWEk+Al8DPe8FydavO2LTwFjr0=; b=Ww+pj/b3V2pLWiAms3ietfdK49jY0/bW4mAyjUikGQM96Sxu3L5W+uJ6tgR3M14Cx9 2FO2nKMr9PY1x4d9gsL86CiJ/LA6aTPoY/yWgxADE3cTZcS+nvVKXau1envmlG2j7XP0 RoXB0M96L8p/b4Ux6BtiLw1RQQ4eUjnPr+Y9p/zWJwXcL0w90uyY3BphZ9BIfeR4nirG dkO4x/7x2sE+XFex04I4xde5JrMqvhF3pz9kAYEo92Sbh333pXUvuVENuzpuhuFWw8Cz kvC4sfOmW+2Nk0t2yZ4iVALXVOfC+j8Yp1GbJV1kLttmzDhd4BrVJmtEICvayGXsmEN4 hLNw==
MIME-Version 1.0
X-Received by 10.68.247.6 with SMTP id ya6mr94683909pbc.45.1388812735218; Fri, 03 Jan 2014 21:18:55 -0800 (PST)
In-Reply-To <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com>
References <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com>
Date Sat, 4 Jan 2014 16:18:55 +1100
Subject Re: Strange behaviour with a for loop.
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <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 <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.4889.1388812744.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388812744 news.xs4all.nl 2939 [2001:888:2000:d::a6]:54810
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63109

Show key headers only | View raw


On Sat, Jan 4, 2014 at 3:03 PM, Sean Murphy <mhysnq1964@icloud.com> wrote:
>   filenames = sys.argv[1:]
>
> for filename in filenames:
>   print ("filename is: %s\n" %filename)

versus

>   filenames = sys.argv[1]
>
> for filename in filenames:
>   print ("filename is:  %s\n" % filename)

The first one is slicing sys.argv, so it returns another list. For
instance, sys.argv might be:

['foo.py', 'test', 'zxcv']

in which case sys.argv[1:] would be:

['test', 'zxcv']

which is still a list. But sys.argv[1] is a single string:

'test'

Now, when you use that in a for loop, you iterate over it. Iterating
over a list yields its items, as you'd expect. Iterating over a string
yields its characters. That's why you see it spelled out. Instead of
iterating over a list of file names, you're iterating over a single
file name, which isn't (in this case) all that useful.

Does that explain what you're seeing?

ChrisA

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


Thread

Re: Strange behaviour with a for loop. Chris Angelico <rosuav@gmail.com> - 2014-01-04 16:18 +1100

csiph-web