faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

Order of keyword arguments

Apr 5th, 2002 12:19
Michael Chermside, Nathan Wallace, Hans Nowak, Snippet 321, Emile van Sebille


"""
Packages: miscellaneous.introspection
"""
"""
> Hi,
> Can one access the order in which keyword arguments were passed to a
> function? I want to call a function with the keyword syntax: f(
> foo='theFooString', bar='theBarString' ) and use the fact that foo was
> listed first so I can do something like:
>
> def f( **kw ):
>
> Process keyword arguments remembering the order in which the were used
> to call the function.
> Print first keyword
> Print second keyword
> .
> .
> .
Perhaps you can adapt one of these ideas.
HTH
"""
#------start of code fragment
import traceback
def ordered1(opts ,**kws):
    print opts
    kws = eval('{'+opts+'}')
    print kws
def ordered2(opts ,**kws):
    print opts
    def convert_to_dict(**kws):
        return kws
    kws = eval('convert_to_dict(' + opts+ ')')
    print kws
def ordered3(**kws):
    tbs = traceback.extract_stack()
    for line in tbs:
        if line[3][:8] == 'ordered3':
            call_order = line[3][9:]
    print call_order
    print kws
ordered1 (opts="'c':1, 'b':2, 'a':1")
ordered2 (opts="c=1, b=2, a=1")
ordered3 (c=1, b=2, a=1)
#-------end of code fragment