printing variables

S

s99999999s2003

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
 
F

Fredrik Lundh

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

John Machin

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", ...?
 
F

Fredrik Lundh

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

Gary Herron

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:

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

Gary Herron
 
D

Dan Bishop

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')
 
S

s99999999s2003

Fredrik said:
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...
 
M

MonkeeSage

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
 
G

Gerrit Holl

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:
.... print locals()[k]
....
foo
baz
bar

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

Gerrit.
 

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,787
Messages
2,569,630
Members
45,334
Latest member
66Zeinab9

Latest Threads

Top