[Python-ideas] Treat underscores specially in argument lists
Andrew Barnert
abarnert at yahoo.com
Tue Feb 17 05:24:52 CET 2015
On Feb 16, 2015, at 18:45, Ryan Gonzalez <rymg19 at gmail.com> wrote:
> It's usually in callbacks where you only care about a few arguments.
A perfect example is using Tk variable tracing in tkinter (for validation or post-change triggering on widgets). The signature of the trace callback is callback(name, index, mode). You almost never need the index, you rarely need the mode, so you write:
def callback(self, name, _, mode):
In fact, you usually don't need the mode either (in fact, you often don't even need the name), but you can't write this:
def callback(self, name, _, _):
So instead you usually write something like:
def callback(self, name, *_):
Personally, I find *_ ugly, so I use *dummy instead. But whatever.
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.
The question is, how often do you really need to ignore multiple parameters, but not all of them (or all of them from this point on)? I can't remember any callback API that I've used where that came up, but I can imagine someone else has one:
> On Mon, Feb 16, 2015 at 8:41 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Mon, Feb 16, 2015 at 07:23:30PM -0600, Ryan Gonzalez wrote:
>> > Often, underscores are used in function argument lists:
>> >
>> > def f(_): pass
>> >
>> > However, sometimes you want to ignore more than one argument, in which case
>> > this doesn't work:
>>
>> Why are you ignoring *any* arguments?
>>
>>
>> --
>> Steve
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> --
> Ryan
> If anybody ever asks me why I prefer C++ to C, my answer will be simple: "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was nul-terminated."
> Personal reality distortion fields are immune to contradictory evidence. - srean
> Check out my website: http://kirbyfan64.github.io/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150216/06d20323/attachment.html>
More information about the Python-ideas
mailing list