Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Dynamic function execution (http://www.velocityreviews.com/forums/t396722-dynamic-function-execution.html)

Andy Wu 11-25-2006 03:43 PM

Dynamic function execution
 
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


Fredrik Lundh 11-25-2006 03:47 PM

Re: Dynamic function execution
 
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>


Irmen de Jong 11-25-2006 03:50 PM

Re: Dynamic function execution
 
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

Cameron Laird 11-25-2006 11:58 PM

Re: Dynamic function execution
 
In article <mailman.718.1164469686.32031.python-list@python.org>,
Fredrik Lundh <fredrik@pythonware.com> wrote:
>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})

?

John Machin 11-26-2006 01:51 AM

Re: Dynamic function execution
 
Cameron Laird wrote:
> In article <mailman.718.1164469686.32031.python-list@python.org>,
> Fredrik Lundh <fredrik@pythonware.com> wrote:
> >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


Fredrik Lundh 11-26-2006 11:37 AM

Re: Dynamic function execution
 
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>



All times are GMT. The time now is 11:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.