468,306 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

printing variables

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
9 1243

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

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

Similar topics

2 posts views Thread by Jack Russell | last post: by
10 posts views Thread by Jeff B. | last post: by
6 posts views Thread by Bill | last post: by
reply views Thread by Jason Hunt | last post: by
2 posts views Thread by =?Utf-8?B?TUFUVA==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.