AW: [Tutor] (no subject)
Werkstudent 010
010.Werkstudent@MCHF.SIEMENS.DE
Mon, 5 Jul 1999 16:05:42 +0200
Thanks Michael,
I believe, that which had stumped me, was my use of doulbe instead of single quotes when trying to pass my argument tuple. It resulted in each letter of a string being considered a seperate argument, and my called routines screamed about too many arguments, until it occured to me, to use skinny quotes.
Thanks again,
David Ungemach
-----Ursprüngliche Nachricht-----
Von: Michael P. Reilly [mailto:arcege@shore.net]
Gesendet am: Montag, 5. Juli 1999 15:54
An: Werkstudent 010
Cc: tutor@python.org
Betreff: Re: [Tutor] (no subject)
[Charset iso-8859-1 unsupported, filtering to ASCII...]
>
> Greetings,
>
> the thread module allows one to start a new thread with "thread.start_new_thread(routine-name, (tuple))",
> but what does it do under internall? I want to execute a method by passing it in a similar manner, with its tuple of arguments, e.g. "doit(routine-name, (tuple))". How do I get my python interpreter to believe me. What is the sequence of "routine-name" and tuple that my python interpreter needs from me, complete with parenthesis, or none, etc. etc.?
>
> thanks
David,
Python has a function to call functions in the fashion you want, called
"apply". The format is:
retvalue = apply(function, args[, keywds])
The function must be a function object or bound method, args must be a
tuple, and the optional keywds should be a dictionary.
>>> def foo(a, b=1, *c, **d):
... print a, b, c, d
...
>>> foo(1)
>>> apply(foo, (1,))
1 1 () {}
>>> foo(1, 2, 3)
1 2 (3,) {}
>>> apply(foo, (1, 2, 3))
1 2 (3,) {}
>>> foo(1, b=2, c=3)
1 2 () {'c': 3}
>>> apply(foo, (1,) {'b': 2, 'c': 3})
1 2 () {'c': 3}
>>>
The start_new_thread() function is similar, but must start differently
because of how threads are started. I suggest reading the source if you
are really interested (Modules/threadmodule.c), but it is not important
for what it seems you need. :)
For more information, read:
Python Language Reference, section 5.3.4: "Calls"
http://www.python.org/doc/current/ref/calls.html
Python Language Reference, section 7.5: "Function definitions"
http://www.python.org/doc/current/ref/function.html
Python Library Reference, section 2.3 "Built-in Functions: apply"
http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-181
-Arcege
--
------------------------------------------------------------------------
| Michael P. Reilly, Release Engineer | Email: arcege@shore.net |
| Salem, Mass. USA 01970 | |
------------------------------------------------------------------------
_______________________________________________
Tutor maillist - Tutor@python.org
http://www.python.org/mailman/listinfo/tutor