[Python-ideas] Treat underscores specially in argument lists

Andrew Barnert abarnert at yahoo.com
Tue Feb 17 10:38:35 CET 2015


On Feb 16, 2015, at 23:58, "Stephen J. Turnbull" <stephen at xemacs.org> wrote:

> Andrew Barnert writes:
> 
>> So anyway, what if you needed the index, but not the name or mode? 
>> You can't use *_ to get around that; you have to come up with two
>> dummy names. That's a bit annoying.
> 
> If there's one such function, I'd say "suck it up".  If there are two
> or more, hit it with a two-by-four and it will submit:
> 
> def twobyfour (func):
>    def wrapper(real1, dummy1, real2, dummy2):
>        return func(real1, real2)
>    return wrapper
> 
>>>> Why are you ignoring *any* arguments?
> 
> I wouldn't express the idea that way.  Obviously we're ignoring
> arguments because there's a caller that supplies them.
> 
> Rather, I'd say "in my experience ignoring arguments means a badly
> designed API that should be refactored."  That's true in callbacks as
> well as in any other function.  Granted, sometimes you can't refactor,
> but I think more often than not ignoring arguments is a code smell and
> you can.

Sure, the tkinter var tracing callback interface is not very Pythonic--but that's because it's a thin wrapper around a Tcl interface. And keeping it thin means that when tkinter doesn't document everything, you can go real the Tcl/Tk docs, so I don't think you'd want to change that. 

(In fact, years ago, I actually did write a higher-level validation framework for Tkinter apps to deal with the haphazard variety or bindable events, validate functions, and tracing functions that different widgets provide in a more consistent and Pythonic way. I found it to be a bit easier to read, but a whole lot harder to debug, so I stopped using it.)

And the same goes for C callback functions, ObjC blocks, JS DOM callbacks, PyCOM event handlers, etc. Or even large legacy Python frameworks. You pick the best framework for the job, and if it has parts of its interface that aren't to your liking, sometimes you have to deal with that.


More information about the Python-ideas mailing list