ignoring a part of returned tuples

N

noamtm

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.
 
B

Bruno Desthuilliers

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...
 
N

noamtm

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!
 
M

Marc 'BlackJack' Rintsch

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
 
N

noamtm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top