Live data from Hacker News

Asterisks in Python

treyhunner.com

11–20 of 110 posts

Re: Asterisks in Python

#11
post #3

Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)

That is curious, it looks like it might just be a type-o in the documentation. Since it is written in C and not python, it is not automatically generated. But I don't know enough C to say for sure, maybe someone else can.

https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...

Re: Asterisks in Python

#12
post #3

Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)

It looks like it's a semi-internal(?) API notation for position-only fields.

That is, you cannot say sorted(iterable=range(10)) because 'iterable' is a position-only parameter.

Some pointers. https://www.python.org/dev/peps/pep-0436/

> / Establishes that all the proceeding arguments are positional-only. For now, Argument Clinic does not support functions with both positional-only and non-positional-only arguments. ... (The semantics of / follow a syntax for positional-only parameters in Python once proposed by Guido. [5] )

https://www.python.org/dev/peps/pep-0457/ ("PEP 457 -- Syntax For Positional-Only Parameters" - draft)

> All parameters before the / are positional-only. If / is not specified in a function signature, that function does not accept any positional-only parameters.

This is only for documenting API signatures. There is no way to implement directly in Python code. Eg, that PEP goes on to say:

> This PEP does not propose we implement positional-only parameters in Python. The goal of this PEP is simply to define the syntax, so that:

> - Documentation can clearly, unambiguously, and consistently express exactly how the arguments for a function will be interpreted.

> - The syntax is reserved for future use, in case the community decides someday to add positional-only parameters to the language.

See also https://bugs.python.org/issue21314 , "Add support for partial keyword arguments in extension functions"

Re: Asterisks in Python

#13
Unrelated to the article: the 'x' on the newsletter popup doesn't work on mobile. Makes the article very difficult to read. If the author is reading this, you should fix that!

Re: Asterisks in Python

#14
post #3

Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)

Python has a bit of a wart in that arguments can normally be passed as keyword or positional. Python3 also added the ability to pass keyword only args (the ones after the `* `), That is

    def f(a, b):
        return a+b
can be called as `f(1, 2)` or `f(b=2, a=1)`, although I'd look at you funny if you did the second.

Certain functions implemented in C also have positional only args, so the function of signature

    weird(p_only, /, pos, kw=None, * , kw_only=None)
can be called in the following ways (1 is p_only, 2, is pos, 3 is kw, 4 is kw_only)

    weird(1, 2, 3)
    weird(1, 2, 3, kw_only=4)
    weird(1, pos=2, kw=3)
    weird(1, pos=2)
    weird(1, 2, kw_only=4)
    ...
but not

   weird(p_only=1, 2)
   weird(1,2,3,4)
   ...

Re: Asterisks in Python

#15
post #13

Unrelated to the article: the 'x' on the newsletter popup doesn't work on mobile. Makes the article very difficult to read. If the author is reading this, you should fix that!

Thanks for reporting this issue!

Re: Asterisks in Python

#16
post #10

This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!

I don't really think so. It's expanding a "language-level" iterable (list, etc) into a "syntactic-level" comma-separated list of those values. does the same but turns dictionaries into syntactic `name=value, ...` constructs. It's a great little feature with loads of handy uses!

Re: Asterisks in Python

#17
post #10

This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!

From the Zen of Python:

> (...) Although practicality beats purity.

https://www.python.org/dev/peps/pep-0020/

Re: Asterisks in Python

#18
post #10

This is one of those areas where I find Python a little contradictory. About 50% of the time it's "explicit is better than implicit" and "there should be one and only one good way" and then the other half of the time it's "here's this cool feature for doing something you could do a different way that looks like hieroglyphics and nobody understands but you should totally use it because it's awesome!

It wouldn't be HN if the top comment weren't critical of asterisks in Python.

Re: Asterisks in Python

#19

my favorite cheat is using locals() with string formatting something like this: def example_cheater(color, flavor, age): template = 'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream' return template.format(**locals()) Obviously a contrived example, and it can be argued that using locals() is not very pythonic, but I think it makes the code look much nicer.

My favorite unpacking snippet is rotating a matrix

>>> matrix = [(1,2,3),(4,5,6),(7,8,9)]

>>> list(zip(*matrix))

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Not as readable as a real maths library, but pretty cool and educational.

Think I came across it in Python in a nut shell, by Alex Martelli

Edit: formatting

Re: Asterisks in Python

#20
post #3

Whats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)

That is curious, it looks like it might just be a type-o in the documentation. Since it is written in C and not python, it is not automatically generated. But I don't know enough C to say for sure, maybe someone else can. https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...

Nope. It represents positional-only arguments. You can have positional-only arguments for C function but not Python functions. Just a weird Python wart.
Post reply on HN