func(*params)

D

David Duerrenmatt

Hi there

For some reasons, I've to use Python 1.5.2 and am looking for a workaround:

In newer Python versions, I can call a function this way:

func = some_function
func(*params)

Then, the list/tuple named params will automatically be "expanded" and
n=len(params) arguments will be submitted.

Python 1.5.2 doesn't support this kind of function call. I could use
some workaround like:

func(params[0],params[1]...)

but since the number of items in params varies and depends on the mapped
function "some_function", this isn't a good solution.

Another idea is to use exec(), don't know whether this is safe...

Any recommondations or tricks?


Thanks,
david
 
P

Peter Otten

David said:
For some reasons, I've to use Python 1.5.2 and am looking for a
workaround:

In newer Python versions, I can call a function this way:

func = some_function
func(*params)

Use

apply(func, params)

Peter
 
N

Nick Smallbone

David said:
Hi there

For some reasons, I've to use Python 1.5.2 and am looking for a workaround:

In newer Python versions, I can call a function this way:

func = some_function
func(*params)

I think the "apply" function is what you want:

apply(object[, args[, kwargs]]) -> value

Call a callable object with positional arguments taken from the tuple
args, and keyword arguments taken from the optional dictionary kwargs.
Note that classes are callable, as are instances with a __call__()
method.

Deprecated since release 2.3. Instead, use the extended call syntax:
function(*args, **keywords).
 
F

Fredrik Lundh

David said:
For some reasons, I've to use Python 1.5.2 and am looking for a workaround:

In newer Python versions, I can call a function this way:

func = some_function
func(*params)

Then, the list/tuple named params will automatically be "expanded" and
n=len(params) arguments will be submitted.

Python 1.5.2 doesn't support this kind of function call.

use

apply(func, params)

or

result = apply(func, params)

more info:
Help on built-in function apply in module __builtin__:

apply(...)
apply(object[, args[, kwargs]]) -> value

Call a callable object with positional arguments taken from the tuple args,
and keyword arguments taken from the optional dictionary kwargs.
Note that classes are callable, as are instances with a __call__() method.

Deprecated since release 2.3. Instead, use the extended call syntax:
function(*args, **keywords).

</F>
 
D

David Duerrenmatt

Great, this is exactly what I was looking for.

Thanks all of you for your immediate answer!




Nick said:
David said:
Hi there

For some reasons, I've to use Python 1.5.2 and am looking for a workaround:

In newer Python versions, I can call a function this way:

func = some_function
func(*params)


I think the "apply" function is what you want:

apply(object[, args[, kwargs]]) -> value

Call a callable object with positional arguments taken from the tuple
args, and keyword arguments taken from the optional dictionary kwargs.
Note that classes are callable, as are instances with a __call__()
method.

Deprecated since release 2.3. Instead, use the extended call syntax:
function(*args, **keywords).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top