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 9 1386 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", ...? 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> 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> 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
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')
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... 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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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),...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |