[Tutor] quick function variable

Cameron Simpson cs at cskk.id.au
Tue Feb 8 19:39:02 EST 2022


On 09Feb2022 00:02, nathan tech <nathan-tech at hotmail.com> wrote:
>Consider the following:
>
>    def func(name="bob", lastname="bobland", chicken="soup"):
>        print("Hello")
>
>if I wanted to call this function and only change the variable name I 
>could do:
>
>    func(name="Jimmy")
>
>How could I do this in variables?
>
>EG:
>variable_change="name"
>value="Bob"
>
>func(variable_change=value)

Keyword arguments can be unpacked from a mapping such as a dict.

    kw = {'name': 'Bob'}
    func(**kw)

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list