|
|
Log in / Subscribe / Register

Positional-only parameters for Python

Positional-only parameters for Python

Posted Apr 10, 2019 13:16 UTC (Wed) by FLHerne (guest, #105373)
In reply to: Positional-only parameters for Python by rgb
Parent article: Positional-only parameters for Python

That's already valid syntax - `_` is an acceptable name in Python, so it's identical to `def fun(name, *args)`.

>>> def fun(name, *_):
>>> print(_)
...
>>> fun("A", "B", "C")
('B', 'C')


to post comments

Positional-only parameters for Python

Posted Apr 10, 2019 16:35 UTC (Wed) by dbaker (guest, #89236) [Link] (3 responses)

not only that, _ is the conventional name of any variables that you're going to ignore. Which is really handy for a lot of things;

iterating over containers of containers:

for a, b, _ in [(1, 'a', []), (2, 'b', [])]: ...

for functions that need to fulfill an interface:

def f1(f: str, *, opt: bool = False):
def f2(f: str, **_):

myval = [f(myval, opt=True) for f in [f1, f2]]

with the python3 partial container explosion syntax:

a, b, *_ = (1, 2, 3, 4)

and a bunch of other cases.

Positional-only parameters for Python

Posted Apr 10, 2019 17:57 UTC (Wed) by juliank (guest, #45896) [Link] (2 responses)

Using _ for things to ignore is fine until you want to introduce gettext as _ :D

Positional-only parameters for Python

Posted Apr 11, 2019 9:39 UTC (Thu) by mgedmin (guest, #34497) [Link]

And for extra fun, _ has a special meaning in the REPL (it contains the value of the last expression).

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> print("hi")
hi
>>> _
4

Positional-only parameters for Python

Posted Apr 12, 2019 20:41 UTC (Fri) by dbaker (guest, #89236) [Link]

Which is just so weird to me :)


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds