Automatic binding of **kwargs to variables

lbolognini at gmail.com lbolognini at gmail.com
Sun Oct 30 00:45:42 EDT 2005


Peter Hansen wrote:
> Do you mean this instead?
>
>       elif name in expected_form1_kwargs and name not in kwargs:
>
> What you wrote doesn't do what you think it does...  it actually tests
> for whether True or False is a key in kwargs, depending on whether "name
> in expected_form1_kwargs" returns True or False.

Hi Peter,

you're right... the code needed also other fixes and I didn't find a
way to do it other than this:

def foo(**kwargs):
    expected_form1_kwargs = ["arg1", "arg2"]

    for name in expected_form1_kwargs:
        if name not in kwargs:
            kwargs[name]=None

    for name in kwargs:
        if name in kwargs and name not in expected_form1_kwargs:
            raise ValueError, "Unrecognized keyword: " + name

    print kwargs
    
            
if __name__ == "__main__":
    foo(arg1=8)


Lorenzo




More information about the Python-list mailing list