Passing arguments to threading objects
Jeff Shannon
jeff at ccvcorp.com
Tue Apr 23 19:33:21 EDT 2002
In article
<pan.2002.04.23.22.39.14.487844.11225 at teich.garten.DigitalProject
s.com>, news.ASkwar at DigitalProjects.com says...
> def mache(name, max):
> for i in range(max):
> print `name` + ': ' + `i`
> time.sleep(random.random())
>
> class myThread(threading.Thread):
> def run(self, name, max):
> for i in range(max):
> print `name` + ': ' + `i`
Aahz already answered your original question, but I wanted to add
that you should really learn to use string formatting. Your
print statements above are much cleaner and more efficient if you
instead use this:
print "%s: %d" % (name, I)
The format specifiers embedded in the string (%s, %d) mark the
location and the type of data to substitute. Python will try to
replace the %s with a string, and the %d with a (decimal) number.
Data is drawn from the tuple at the end of the expression, with
the first specifier being replaced by the first item in the
tuple, etc. (The number of specifiers must match the length of
the tuple.)
This is typically easier to follow than lots of string additions.
It's also more efficient -- when you do multiple string
additions, Python creates a new temporary string for *each*
addition, whereas the string formatting requires only one new
string no matter how many format specifiers you use. In your
example performance isn't a big issue, but especially if this is
done in a tight loop, the resulting difference in efficiency can
be huge.
--
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list