By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

printing variables

P: n/a
hi
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
eg print var* ??
i don't want to do :

print var1, var2, var3, var4 ......etc...
thanks

Oct 6 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

s9************@yahoo.com wrote:
hi
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
eg print var* ??
i don't want to do :

print var1, var2, var3, var4 ......etc...
thanks
| >>var1 = 1
| >>var2 = 2
| >>variant = 3
| >>variegated = 4
| >>' '.join(str(v) for k, v in locals().iteritems() if
k.startswith('var'))
| '1 2 3 4'

*BUT* why do you start off with those things in separate variables
instead of in some container (list, dict, object simulating a "record"
or "struct", ...?

Oct 6 '06 #2

P: n/a
s9************@yahoo.com wrote:
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
do you often (or always) treat these as a collection ? why not just put
the values in a list (or tuple) instead ?

</F>

Oct 6 '06 #3

P: n/a
s9************@yahoo.com wrote:
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
do you often (or always) treat these as a collection ? why not just put
the values in a list (or tuple) instead ?

</F>

Oct 6 '06 #4

P: n/a
s9************@yahoo.com wrote:
hi
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
eg print var* ??
i don't want to do :

print var1, var2, var3, var4 ......etc...
thanks

If you really don't want an array (list, or tuple) of these (i.e.,
var[1], var[2], var[3], ...), then you can get at a dictionary that
contains all the local variables -- names and values -- like this:

>>var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
locals()
{'var4': '....', 'var1': 'blah', 'var3': 'blahblahblah', 'var2':
'blahblah', '__builtins__': <module '__builtin__' (built-in)>,
'__file__': '/home/gherron/.startup.py', '__name__': '__main__',
'__doc__': None}
>>for v,k in locals().items():
.... if v.startswith('var'):
.... print k
....
.....
blah
blahblahblah
blahblah
>>>

Gary Herron

Oct 6 '06 #5

P: n/a
On Oct 5, 9:47 pm, s99999999s2...@yahoo.com wrote:
hi
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
eg print var* ??
i don't want to do :

print var1, var2, var3, var4 ......etc...
def print_vars(var_dict, prefix=''):
var_names = sorted(name for name in var_dict if
name.startswith(prefix))
for name in var_names:
print var_dict[name],
print

def example():
var1 = 'blah'
var2 = 'blahblah'
var3 = 'spam'
var4 = 'eggs'
ignored_var = 'foo'
print_vars(locals(), 'var')

Oct 6 '06 #6

P: n/a

Fredrik Lundh wrote:
s9************@yahoo.com wrote:
say i have variables like these
>
var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..
>
bcos all the variable names start with "var", is there a way to
conveniently print those variables out...

do you often (or always) treat these as a collection ? why not just put
the values in a list (or tuple) instead ?

</F>
Thanks all for the answers. Yup, i think i will use dicts/tuples/lists
instead...

Oct 6 '06 #7

P: n/a
s9************@yahoo.com wrote:
Thanks all for the answers. Yup, i think i will use dicts/tuples/lists
instead...
Even though you should be using some kind of container, you can still
do what you origianlly asked for with relative ease...you just have to
use the evil eval function (gasp!):

for i in xrange(5):
if i == 0: continue
print eval("var%d" % i)

Regards,
Jordan

Oct 6 '06 #8

P: n/a
On 2006-10-06 04:50:33 +0200, s9************@yahoo.com wrote:
say i have variables like these

var1 = "blah"
var2 = "blahblah"
var3 = "blahblahblah"
var4 = "...."
var5 = "..."..

bcos all the variable names start with "var", is there a way to
conveniently print those variables out...
eg print var* ??
i don't want to do :

print var1, var2, var3, var4 ......etc...
Don't do this:
>>import fnmatch
var1, var2, var3 = "foo", "bar", "baz"
for k in fnmatch.filter(locals(), "var*"):
.... print locals()[k]
....
foo
baz
bar

This is evil.
It's unpythonic.
This is yet another way to do it - QED.

Gerrit.
Oct 6 '06 #9

P: n/a
On 10/6/06, Gerrit Holl <ge****@nl.linux.orgwrote:
>import fnmatch
var1, var2, var3 = "foo", "bar", "baz"
for k in fnmatch.filter(locals(), "var*"):
... print locals()[k]
...
foo
baz
bar

This is evil.
It's unpythonic.
It's so evil, Perl 4 would look upon it in scorn.

-- Theerasak
Oct 6 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.