[Tutor] Fwd: timed functions? Timeouts?
Kent Johnson
kent37 at tds.net
Tue May 20 20:04:21 CEST 2008
Forwarding to the list.
---------- Forwarded message ----------
From: W W <srilyk at gmail.com>
Date: Tue, May 20, 2008 at 12:31 PM
Subject: Re: [Tutor] timed functions? Timeouts?
To: Kent Johnson <kent37 at tds.net>
On Tue, May 20, 2008 at 7:18 AM, Kent Johnson <kent37 at tds.net> wrote:
>> Then I'm a little confused by the * and ** - they look just like the
>> pointer and pointer to a pointer in C++, but do they perform the same
>> function in python?
>
> No, these are not pointers, they allow passing arbitrary lists and
> dicts of arguments. I don't know of a good writeup of this syntax;
> here are some pointers:
> http://bytes.com/forum/thread25464.html
Ahhh, now I see (I think). Here's what I've discovered.
The * unpacks the list into a tuple, creating a "new" variable that as
far as I can tell, dies when the function ends.
** creates a copy of the dictionary, rather than passing the actual
dictionary, so it's possible to operate on it, leaving the original
dict intact.
Neither value is actually considered an argument:
>>> def SomeFunction(**foo):
... print foo
...
>>> mydict = {'abc':123}
>>> SomeFunction(mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: SomeFunction() takes exactly 0 arguments (1 given)
>>> SomeFunction(**mydict)
{'abc': 123}
----
Using a list without * doesn't create an error at all, instead it
yields results I didn't expect:
>>> def SomeFunction(*foo):
... print foo
...
>>> mylist = ['a', 'b', 'c']
>>> SomeFunction(mylist)
(['a', 'b', 'c'],)
----
Specifically, it doesn't unpack the list, it just packs the list into a tuple.
With a little more experimentation I've discovered that you can
manually enter data, instead of using a list:
>>> def another_function(*foo):
... print foo
...
>>> another_function('hello', 'spork', 1,2,3)
('hello', 'spork', 1, 2, 3)
Now I just have to figure out exactly how thread/threading uses these tuples!
Thanks again!
-Wayne
--
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
More information about the Tutor
mailing list