471,348 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

ignoring a part of returned tuples

Hi,

Some functions, like os.walk(), return multiple items packed as a
tuple:

for (dirpath, dirnames, filenames) in os.walk(...):

Now, if you don't care about one of the tuple members, is there a
clean way to ignore it, in a way that no unused variable is being
created?

What I wanted is:
for (dirpath, , filenames) in os.walk(...):

But that doesn't work.

Thanks, Noam.

Jul 4 '07 #1
5 5658
On Jul 4, 4:08 pm, noamtm <noa...@gmail.comwrote:
What I wanted is:
for (dirpath, , filenames) in os.walk(...):

But that doesn't work.
for (dirpath, _, filenames) in os.walk(...):

Jul 4 '07 #2
noamtm a écrit :
Hi,

Some functions, like os.walk(), return multiple items packed as a
tuple:

for (dirpath, dirnames, filenames) in os.walk(...):

Now, if you don't care about one of the tuple members, is there a
clean way to ignore it,
Yes : just ignore it !-)
in a way that no unused variable is being
created?
the term 'variable' in Python can be somewhat misleading. You have
objects, and you have names bound to objects. In your case, whether you
bind it to a name or not, the object will be created, so it wont make
much differences.
What I wanted is:
for (dirpath, , filenames) in os.walk(...):

But that doesn't work.
A common idiom is to use '_' for unused values, ie:

for (dirpath, _, filenames) in os.walk(...):

You could also just bind the whole tuple to a ssinngle name then
subscript it:

for infos in os.walk(...):
# now dirpath is infos[0] and filenames is infos[2]

but this won't buy you much...
Jul 4 '07 #3
On Jul 4, 11:29 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
A common idiom is to use '_' for unused values, ie:

for (dirpath, _, filenames) in os.walk(...):
That's what I need - this avoids PyLint telling me that I have an
unused variable, and also makes it clear that this value is not used.

Thanks!

Jul 4 '07 #4
On Wed, 04 Jul 2007 05:25:01 -0700, noamtm wrote:
On Jul 4, 11:29 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
>A common idiom is to use '_' for unused values, ie:

for (dirpath, _, filenames) in os.walk(...):

That's what I need - this avoids PyLint telling me that I have an
unused variable, and also makes it clear that this value is not used.
Pylint also "allows" the name `dummy` without complaining. That makes it
even clearer and doesn't clash with the meaning of `_` when `gettext` is
used.

It's possible to configure many checks in Pylint. For this check it's
possible to give a regular expression for names you don't care if they are
unused.

Ciao,
Marc 'BlackJack' Rintsch
Jul 4 '07 #5
Pylint also "allows" the name `dummy` without complaining. That makes it
even clearer and doesn't clash with the meaning of `_` when `gettext` is
used.
Thanks, that's even better!

Noam.

Jul 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Paul Bramscher | last post: by
66 posts views Thread by Mike Meyer | last post: by
8 posts views Thread by Vince Morgan | last post: by
21 posts views Thread by Andrew Ward | last post: by
10 posts views Thread by Will McGugan | last post: by
reply views Thread by John K Masters | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.