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:
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.
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
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.
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"]
?
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
Thanks to both of you.
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.
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"]
?
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
Thanks to both of you.
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.
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"]
?