Dynamic function execution

A

Andy Wu

Hi guys,

There's a function I want to use which looks like this:

def func(seconds = None, minutes = None, hours = None):
...

In my program I can get a string object('seconds', 'minutes', 'hours')
to specify which parameter to use, the problem is I don't know how to
call the function.

Say I have a string 'minutes' and a integer 30, now I need to call the
func this way: func(minutes = 30), how do I do this?

I'm sure this is a simple question, but I can't google it out since I
don't know how to describe it in a short term.

Thanks,

Andy Wu
 
F

Fredrik Lundh

Andy said:
def func(seconds = None, minutes = None, hours = None):
...

In my program I can get a string object('seconds', 'minutes', 'hours')
to specify which parameter to use, the problem is I don't know how to
call the function.

Say I have a string 'minutes' and a integer 30, now I need to call the
func this way: func(minutes = 30), how do I do this?

func(**{"minutes": 30})

</F>
 
I

Irmen de Jong

Andy said:
Say I have a string 'minutes' and a integer 30, now I need to call the
func this way: func(minutes = 30), how do I do this?

d={"minutes": 30}
func(**d)

This is "extended call syntax". You can read more about this when
you look up the (deprecated) "apply" function in the manual.

--Irmen
 
C

Cameron Laird

func(**{"minutes": 30})

</F>

Now I'm confused: what's the advantage of

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

func(**{"minutes": 30})

over

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

func(minutes = 30)

? Or am I missing the point that a better example of what
Mr. Wu really wants is

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

dimension = "minutes"
func(**{dimension: 30})

?
 
J

John Machin

Cameron said:
Now I'm confused: what's the advantage of

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

func(**{"minutes": 30})

over

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

func(minutes = 30)

? Or am I missing the point that a better example of what
Mr. Wu really wants is

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

dimension = "minutes"
func(**{dimension: 30})

?

Hi Cameron,

You're on the right track. A better example would have the last two
lines replaced by:

# Simulate obtaining data
argument_name = "minutes"
argument_value = 30
# Then ...
func(**{argument_name: argument_value})

:)

Cheers,
John
 
F

Fredrik Lundh

Cameron said:
? Or am I missing the point that a better example of what
Mr. Wu really wants is

def func(seconds = None, minutes = None, hours = None):
print seconds
print minutes
print hours

dimension = "minutes"
func(**{dimension: 30})

I assumed that the OP was looking for a mechanism that allowed him to
use strings for parameter names, not that he wasn't able to replace a
literal with a variable once he knew what mechanism to use...

</F>
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top