Re: Automatic binding of **kwargs to variables



"lbolognini@xxxxxxxxx" <lbolognini@xxxxxxxxx> writes:
> 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


You're contorting things horribly, and - not unexpected when you do
that kind of thing - running code that has no effect. To wit, the
second loop goes through every key in kwargs. The first thing it does
is test to see if the key you just selected from kwargs is in
kwargs. This test is always true.

IIUC, the reason you're going through these contortions is that the
argument list is long and repeated in a number of different places. So
you're going to declare expected_form1_kwargs in one place and use it
repeatedly. Presumably, you can also bundle the set/check code up in a
function that you pass kwargs to as well.

Have you thought about generating the more readable form from code you
write? One of the most powerful tools available to programmers is code
that writes code. This seems to be the perfect case for such a
tool. You could, for instance, run your code through m4 to generate
the Python that actually gets run. A trivial script that looks for
"^def " and then replaced **kwargs with your list could serve as a
custom preprocessor. To be particulary perverse, you could try using
a Cheetah template to generate your code - I've not tried generating
Python with it, but it creates Makefiles quite nicely.

<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
.



Relevant Pages

  • Re: A __getattr__ for class methods?
    ... or am I trying to mirror a functionality that Python simply does ... I'm not sure that the way you tackled this is the good approach: while it's quite flexible as far as the search criteria go, it'll require less than obvious code to match keywords and values, will lead to somewhat verbose syntax, and the syntax itself is unforgiving, brittle and not really pythonic. ... As you probably know, Python has a good support for keyword args, allowing you to fill your arguments out of order. ... But **kwargs goes beyond the regular explicit keyword arguments: when specified, **kwargs is a dict populated with the implicit keyword arguments, the ones you haven't specified in the argument tuple of your method. ...
    (comp.lang.python)
  • Re: overloading *something
    ... I should be able to write a dictionary-like ... > interface in python and **kwargs should in turn be able to use it. ... > additional tests would need to be made to make **kwargs more generalized. ...
    (comp.lang.python)
  • Re: breaking up is hard to do
    ... Pump variables into dictionary and pass it as **kwargs argument ... to functions/classes. ... > I've a 2,000 line and growing Python script that I'd like to break up ... > camper with Python. ...
    (comp.lang.python)
  • Re: different order of parameters...
    ... E.g. compare it with Python: ... print a, b, args, kwargs ...
    (comp.lang.ruby)
  • Re: Extending Python Syntax with @
    ... due to wxWidgets defining every function as (*args, **kwargs) ... and indirecting through a layer of Swig. ... the "relaxed" Python you describe and the standard Python. ...
    (comp.lang.python)