printing variables

Dan Bishop danb_83 at yahoo.com
Thu Oct 5 23:08:48 EDT 2006


On Oct 5, 9:47 pm, s99999999s2... at 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')




More information about the Python-list mailing list