By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Dynamic function execution

P: n/a
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

Nov 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Andy Wu wrote:
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>

Nov 25 '06 #2

P: n/a
Andy Wu wrote:
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
Nov 25 '06 #3

P: n/a
In article <ma**************************************@python.o rg>,
Fredrik Lundh <fr*****@pythonware.comwrote:
>Andy Wu wrote:
>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>
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})

?
Nov 25 '06 #4

P: n/a
Cameron Laird wrote:
In article <ma**************************************@python.o rg>,
Fredrik Lundh <fr*****@pythonware.comwrote:
Andy Wu wrote:
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>

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

Nov 26 '06 #5

P: n/a
Cameron Laird wrote:
? 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>

Nov 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.