python equivalent to perl's qw()
Question:
I do this a lot in Perl:
printf "%8s %8s %8sn", qw(date price ret);
However, the best I can come up with in Python is
print '%8s %8s %8s' % (tuple("date price ret".split()))
I’m just wondering if there is a more elegant way of doing it? I’m fine if you tell me that’s it and no improvement can be made.
Answers:
Well, there’s definitely no way to do exactly what you can do in Perl, because Python will complain about undefined variable names and a syntax error (missing comma, perhaps). But I would write it like this (in Python 2.X):
print '%8s %8s %8s' % ('date', 'price', 'ret')
If you’re really attached to Perl’s syntax, I guess you could define a function qw
like this:
def qw(s):
return tuple(s.split())
and then you could write
print '%8s %8s %8s' % qw('date price ret')
which is basically Perl-like except for the one pair of quotes on the argument to qw
. But I’d hesitate to recommend that. At least, don’t do it only because you miss Perl – it only enables your denial that you’re working in a new programming language now 😉 It’s like the old story about Pascal programmers who switch to C and create macros
#define BEGIN {
#define END }
"date price ret".split()
QW()
is often used to print column headings using join()
in Perl. Column heads in the real-world are sometimes long — making join("t", qw())
very useful because it’s easier to read and helps to eliminate typos (e.g. "x","y"
or "xty"
). Below is a related approach in real-world Python:
print("t".join('''PubChemId Column ESImode Library.mzmed
Library.rtmed Metabolite newID Feature.mzmed Feature.rtmed
Count ppmDiff rtDiff'''.split()))
The triple quote string is a weird thing because it doubles as a comment. In this context, however, it is a string and it frees us from having to worry about line breaks (as qw()
would).
Thanks to the previous replies for reveling this approach.
I do this a lot in Perl:
printf "%8s %8s %8sn", qw(date price ret);
However, the best I can come up with in Python is
print '%8s %8s %8s' % (tuple("date price ret".split()))
I’m just wondering if there is a more elegant way of doing it? I’m fine if you tell me that’s it and no improvement can be made.
Well, there’s definitely no way to do exactly what you can do in Perl, because Python will complain about undefined variable names and a syntax error (missing comma, perhaps). But I would write it like this (in Python 2.X):
print '%8s %8s %8s' % ('date', 'price', 'ret')
If you’re really attached to Perl’s syntax, I guess you could define a function qw
like this:
def qw(s):
return tuple(s.split())
and then you could write
print '%8s %8s %8s' % qw('date price ret')
which is basically Perl-like except for the one pair of quotes on the argument to qw
. But I’d hesitate to recommend that. At least, don’t do it only because you miss Perl – it only enables your denial that you’re working in a new programming language now 😉 It’s like the old story about Pascal programmers who switch to C and create macros
#define BEGIN {
#define END }
"date price ret".split()
QW()
is often used to print column headings using join()
in Perl. Column heads in the real-world are sometimes long — making join("t", qw())
very useful because it’s easier to read and helps to eliminate typos (e.g. "x","y"
or "xty"
). Below is a related approach in real-world Python:
print("t".join('''PubChemId Column ESImode Library.mzmed
Library.rtmed Metabolite newID Feature.mzmed Feature.rtmed
Count ppmDiff rtDiff'''.split()))
The triple quote string is a weird thing because it doubles as a comment. In this context, however, it is a string and it frees us from having to worry about line breaks (as qw()
would).
Thanks to the previous replies for reveling this approach.