Automatic binding of **kwargs to variables

Peter Otten __peter__ at web.de
Sat Oct 29 14:18:09 EDT 2005


Steve Holden wrote:

>> Why don't you just change the method signature to foo(self, x, y, z,
>> whatever, **kwargs)?

> Probably because values are then required for those arguments. Plus it's
> a lot of work to specify "a very long list", and the list will also need
> maintaining.
 
Note that I kept the **kwargs argument. He would only have to specify
variables that are actually used in foo(), and these need maintaining
anyway. As to optional arguments, you can provide default values. Checking
for those is easier and safer than using local variables that may not be
bound or accidentally refer to a global.

# e. g.
missing = object() # if None is a valid state of whatever
def foo(self, whatever=missing, **kwargs):
    if whatever is not missing:
        # use whatever

> I must, however, agree with Mike's advice: it's unwise to try and
> pollute a function's namespace with arbitrary variables. Some kind of
> bunch-like object would seem to be the most satisfactory way to go.  

Using a bunch doesn't remove the necessity of an existence-test either (for
optional attributes).

Peter





More information about the Python-list mailing list