What is in **kwargs?

When I declare a function with **kwargs, it is because I don’t know in advance what arguments I’ll need in any given situation.
But at that point there is the lenghty chore of sifting all possible arguments. Does anybody know of a less stupid way than a sequence of
if “name_variable” in kwargs:

variable = kwargs[“name_variable”]

?

If there are particular values you know you want to deal with, then
simply specify them explicitly in the definition:

def my_func(arg1, arg2, arg3=None, arg4=0, arg5='', **kwargs):

You typically need kwargs for passing through to another function. As
long as you have defaults for the arguments, then you don’t need to
specify them in the function call.

–Ned.

raffaello wrote:

···

http://nedbatchelder.com

When I declare a function with **kwargs, it is because
I don’t know in advance what arguments I’ll need in any given situation.

But at that point there is the lenghty chore of sifting all possible
arguments. Does anybody know of a less stupid way than a sequence of
*if “name_variable” in kwargs:
*

variable = kwargs[“name_variable”]

?



---

_______________________________________________
wxpython-users mailing list

wxpython-users@lists.wxwidgets.orghttp://lists.wxwidgets.org/mailman/listinfo/wxpython-users

-- Ned Batchelder,

http://nedbatchelder.com

There's also...
variable = kwargs.get('name', default)

- Josiah

···

On Wed, Aug 27, 2008 at 2:34 PM, Ned Batchelder <ned@nedbatchelder.com> wrote:

If there are particular values you know you want to deal with, then simply
specify them explicitly in the definition:

    def my_func(arg1, arg2, arg3=None, arg4=0, arg5='', **kwargs):

You typically need kwargs for passing through to another function. As long
as you have defaults for the arguments, then you don't need to specify them
in the function call.

--Ned.
http://nedbatchelder.com

raffaello wrote:

When I declare a function with **kwargs, it is because I don't know in
advance what arguments I'll need in any given situation.
But at that point there is the lenghty chore of sifting all possible
arguments. Does anybody know of a less stupid way than a sequence of
if "name_variable" in kwargs:
variable = kwargs["name_variable"]
?

________________________________
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Ned Batchelder, http://nedbatchelder.com

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

The proper combination of default arguments and the function "get"
(quicker and shorter than my "ifs") will solve the problem.
I hoped in the possibility of a loop (that would anyway need the
specification of its argument) just because it looks more elegant.
Well, my code will dress casual :slight_smile:
Thanks to both of you.

···

2008/8/28, Josiah Carlson <josiah.carlson@gmail.com>:

There's also...
variable = kwargs.get('name', default)

- Josiah

On Wed, Aug 27, 2008 at 2:34 PM, Ned Batchelder <ned@nedbatchelder.com> > wrote:

If there are particular values you know you want to deal with, then simply
specify them explicitly in the definition:

    def my_func(arg1, arg2, arg3=None, arg4=0, arg5='', **kwargs):

You typically need kwargs for passing through to another function. As
long
as you have defaults for the arguments, then you don't need to specify
them
in the function call.

--Ned.
http://nedbatchelder.com

raffaello wrote:

When I declare a function with **kwargs, it is because I don't know in
advance what arguments I'll need in any given situation.
But at that point there is the lenghty chore of sifting all possible
arguments. Does anybody know of a less stupid way than a sequence of
if "name_variable" in kwargs:
variable = kwargs["name_variable"]
?

________________________________
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Ned Batchelder, http://nedbatchelder.com

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

With the way Python's scoping works, you can't change the local
variables like...

for name in ('var1', 'var2', ...):
    locals()[name] = kargs.get(name, None)

... despite the fact that locals() is a dictionary. It's due to the
fact that locals are actually stored in flat arrays, and updating the
arrays after the dictionary *copy* has been modified is 1) slow, 2)
not seen to be useful enough to be worth it.

If you have an object that defines your configuration; like...
class configObj:
    pass

You can use...

config = configObj()
config.__dict__.update((name, kargs.get(name, None)) for name in
('var1', 'var2', ...))

Then you can refer to all of your options as config.var1, etc. This
will generally reduce the number of lines of your configuration
handling, but it also adds an extra layer of attribute checking, and
doesn't have the convenience of each value having a different default
(unless you create a secondary table of defaults, etc.).

- Josiah

···

On Wed, Aug 27, 2008 at 10:34 PM, raffaello <barbarossa.platz@gmail.com> wrote:

The proper combination of default arguments and the function "get"
(quicker and shorter than my "ifs") will solve the problem.
I hoped in the possibility of a loop (that would anyway need the
specification of its argument) just because it looks more elegant.
Well, my code will dress casual :slight_smile:
Thanks to both of you.

2008/8/28, Josiah Carlson <josiah.carlson@gmail.com>:

There's also...
variable = kwargs.get('name', default)

- Josiah

On Wed, Aug 27, 2008 at 2:34 PM, Ned Batchelder <ned@nedbatchelder.com> >> wrote:

If there are particular values you know you want to deal with, then simply
specify them explicitly in the definition:

    def my_func(arg1, arg2, arg3=None, arg4=0, arg5='', **kwargs):

You typically need kwargs for passing through to another function. As
long
as you have defaults for the arguments, then you don't need to specify
them
in the function call.

--Ned.
http://nedbatchelder.com

raffaello wrote:

When I declare a function with **kwargs, it is because I don't know in
advance what arguments I'll need in any given situation.
But at that point there is the lenghty chore of sifting all possible
arguments. Does anybody know of a less stupid way than a sequence of
if "name_variable" in kwargs:
variable = kwargs["name_variable"]
?

________________________________
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Ned Batchelder, http://nedbatchelder.com

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users