473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Nov 25 '06 #1
5 1029
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ford Desperado | last post by:
there is an application which issues a lot of simple dynamic queries against an Oracle database. If CURSOR_SHARING is set to FORCE, Oracle treats dynamic queries as static ones, for instance...
14
by: Ina Schmitz | last post by:
Hello, I would like to get the logical and physical reads for every sql statement executed. Thatfore, I used the command "db2 get snapshot for dynamic sql on <mydatabase>". There, I could see...
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.