Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Dynamic function execution

Reply
Thread Tools

Dynamic function execution

 
 
Andy Wu
Guest
Posts: n/a
 
      11-25-2006
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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-25-2006
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>

 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      11-25-2006
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
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      11-25-2006
In article <(E-Mail Removed)>,
Fredrik Lundh <(E-Mail Removed)> 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})

?
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      11-26-2006
Cameron Laird wrote:
> In article <(E-Mail Removed)>,
> Fredrik Lundh <(E-Mail Removed)> 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

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-26-2006
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
Dynamic tracing of C code execution Nikhil C Programming 5 05-16-2006 06:21 AM
"Open-end" Java-session / dynamic compilation and execution Jesper Sahner Java 1 02-01-2005 04:52 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Dynamic Execution of Function/Proc Kishor ASP .Net 9 09-27-2003 05:53 AM



Advertisments