473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1408

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mr. B | last post by:
This is driving me NUTZ!!! I've been screwing around on this for a week now. And I have tried to find examples similar to what I have (nada). Got lots of streaming a TXT file... bah! I am...
2
by: Jack Russell | last post by:
Gee vb.net makes me feel dumb! My understanding is that I set up and do all of my printing in the printdocument printpage and handler using various graphics methods such as drawstring. Is...
2
by: gerb | last post by:
Hello, I realise this is not a pure Javascript question, and that VBscript is probably involved at some point, though not as much as I fear. If you opened this item looking for a pute Javascript...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
0
by: Jason Hunt | last post by:
Hello All, So far I've been using the "regular" way of printing with PrintDocument, by maintaining the X and Y positions, drawing rectangles, lines, strings of text, etc. I'd like to have a...
2
by: Hexman | last post by:
Hello All, I'm now getting into the reporting phase of my application development. I need to print not only continuous page reports from a datasource (database, datagridview, listboxes, etc),...
2
by: =?Utf-8?B?TUFUVA==?= | last post by:
What i am trying to accomplish is programatically creating Invoices and then Printing them out. I don't really need to "View" them before they print. How I am trying to do this is I have created...
2
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.