11 ms·
Asterisks in Python
- th0ma5 8y agoI've been programming with Python for over a decade. I mostly understand, but I do try to avoid when possible for maximum clarity. Expanding function variables is fine and clear enough, but multiple levels deep in a comprehension and it can get pretty thick to try and keep it all straight. This article is nice that it covers all the patterns I've seen.
- soVeryTired 8y agoHonest question: why would anyone make a list comprehension that's multiple levels deep? Isn't the purpose of comprehensions to provide quick-and-dirty inline for loops, where a full for loop is too verbose?
- jetrink 8y agoIt's useful for working with permutations. E.g, [f(a, b) for a in some_list for b in some_list] or even [f(a, b) for a in some_list for b in some_list if a is not b]
- kbp 8y agoitertools has functions that make that sort of nesting unnecessary (and some would say make the code clearer), eg: import itertools as it [f(a, b) for a, b in it.product(some_list, some_list) if a is not b]
- deleted 8y ago[deleted]
- kbp 8y agoOh, or more directly: [f(a, b) for a, b in it.permutations(some_list, r=2)] And then you could also use itertools.starmap instead of using a comprehension at all, if you wanted.
- SatvikBeri 8y agoComprehensions have some other benefits too, for example, they're much less likely to be accidentally stateful. My general experience is that for complex logic, a comprehension is harder to write initially, but much less likely to have bugs when I get it right. That said, I usually won't use deeply nested comprehensions, but sometimes it actually is the clearest way to parse something (e.g. extracting a field from nested JSON.)
- shmolyneaux 8y agoWhile supported, list comprehensions that are multiple levels deep isn't considered good style. There is an example of this in Google's python style guide: https://github.com/google/styleguide/blob/gh-pages/pyguide.md#27-comprehensions--generator-expressions https://github.com/google/styleguide/blob/gh-pages/pyguide.m...
- bjourne 8y agoPeople who come from math backgrounds often becomes infatuated with complicated list comprehensions when they realize that they can be used as set-builder notation. {(x,y) for x in range(10) for y in range(10) if y == 2*x}
- mike00632 8y agoThat looks exactly like my notes from math courses and I'm overjoyed that it's meaningful in Python.
- analog31 8y agoI use stuff like that. But my rule of thumb is that I try and make it readable by breaking it up into multiple lines and using indentation. If I can't make it readable, then I will consider another way of writing it. This is mainly for self preservation, since I'm likely to be the person who has to read it later.
- klodolph 8y agoFor me it’s very rare and used where I would write something like concatMap in Haskell. citiesByState = { 'WA': ['Seattle', 'Spokane', 'Olympia'], 'OR': ['Portland', 'Salem'], 'CA': ['San Francisco', 'Los Angeles', 'San Diego'], } cityToState = {city: state for state, cities in citiesByState.items() for city in cities} I would generally avoid multiple levels in a comprehension but there are some very simple two-level cases like this that I use occasionally. I feel that the code is easy to read, at least.
- evincarofautumn 8y agoIn Haskell, these direct equivalents aren’t bad at all: cityToState = Map.fromList [ (city, state) | (state, cities) <- Map.toList citiesByState , city <- cities ] cityToState = Map.fromList $ do (state, cities) <- Map.toList citiesByState city <- cities pure (city, state) For multiple “nested loops”, I generally prefer do-notation over both list comprehensions and combinators such as concatMap, unless the structure is simple enough that the combinator version is much shorter.
- klodolph 8y agoAnd for those following who don’t know Haskell, both of these are basically sugar for concatMap. (>>=) = flip concatMap
- quietbritishjim 8y agoMultiple for loops in list comprehensions (if that's what's meant by "multi-level") are pretty straightforward once you realise the simple rule that translates them into loops: write everything to the right of the expression in the same order with nesting. For example this: l = [f(x, y) for x in X for y in Y if x > y] is the same as this: l = [] for x in X: for y in Y: if x > y: l.append(f(x,y)) If a list comprehension doesn't fit on one line I try to highlight this to any future reader by using one line per thing that would get nested: l = [ f(x, y) for x in X for y in Y if x > y ] Needless to say, there are still list comprehensions that are complex enough that they ought to be broken out into loops. But being nested isn't enough by itself.
- dorfsmay 8y agoIt used to be for performance reasons, the way python runs comprehension is faster than plain loops, so the more you could cram in a single comprehension, the faster it ran. These days though, you can split it in generator expressions and make it run at the end inside a comprehension, and you get best of both worlds: splitting helps readability, running it all at once in a single comprehension at the end keeps it performing.
- the_clarence 8y agoI never understand why people feel the need to find shortcuts. It’s like reading smthg[i++] in C. It’s not clear and it could be clearer if written over two lines instead but yet everyone does it.
- dmurray 8y agoIf "everyone does it", then you should treat it as clear and just part of writing the language idiomatically. Pick your battles for when even the cognoscenti favour the more verbose approach.
- the_clarence 8y agoIt doesn't help people not too familiar with the language.
- taneq 8y agoIt's perfectly clear and unambiguous. For some reason everyone loves it when functional languages are 'pithy' but hates it when C-like languages are equally terse.
- the_clarence 8y agoHow? Smthg[i++] and smthg[++i] have two different behaviors. If you tell me that this is clear I probably don't want to read your code.
- taneq 8y agoThey're two different things. Are you going to complain that a/b and b/a give different results?
- the_clarence 8y agoYou're comparing mathematics term which have been established since before your grandfather was born with a peculiarity of the C language that confuse more people than it helps. People willing to Obtain a small amount of LOC for less clarity deserves neither.
- tln 8y agoWhats with the / in the help for sorted? Help on built-in function sorted in module builtins: sorted(iterable, /, *, key=None, reverse=False)
- dec0dedab0de 8y agoThat 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/9dfa0fe587eae3626ffc973680c6a17f35de3864/Python/bltinmodule.c#L2203 https://github.com/python/cpython/blob/9dfa0fe587eae3626ffc9...
- jnwatson 8y agoNope. It represents positional-only arguments. You can have positional-only arguments for C function but not Python functions. Just a weird Python wart.
- eesmith 8y agoIt 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/ 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/ 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 https://bugs.python.org/issue21314 , "Add support for partial keyword arguments in extension functions"
- dec0dedab0de 8y agomy 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.
- kirkules 8y agoCalling calling a Python standard library function "not pythonic" is a great example of when the term "pythonic" is beyond usefulness.
- avbor 8y agoIn python 3.6 and up, that becomes def example_cheater(color, flavor, age): return f'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream' Even nicer!
- luhn 8y agoFor reference, this feature is Formatted String Literals ("f-strings"). https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals https://docs.python.org/3/reference/lexical_analysis.html#fo...
- shaklee3 8y agoOf course I didn't read your comment until several Google searches later.
- nazgul17 8y agoDidn't know this. Thanks for sharing!
- dec0dedab0de 8y agoOMG! that is so exciting, Thank you for telling me! I have only been using 3.x as my primary language for a few months now, I can't wait to try this out. I feel like a huge dork right now, because that made my day.
- zmmmmm 8y agoThis 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!
- andrepd 8y agoI 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!
- cuchoi 8y agoFrom the Zen of Python: > (...) Although practicality beats purity. https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- jakevn 8y agoEssentially, then, expressiveness (language complexity for the sake of flexibility), beats ease of learning or "one right way to do things".
- anothergoogler 8y agoIt wouldn't be HN if the top comment weren't critical of asterisks in Python.
- pavlov 8y agoThis is one of the things that gets me about Python. It makes this big noise about being a super-friendly form of executable pseudocode, but then you open any code example and the first thing you see is two asterisks and the mysterious word "kwargs" (a Swedish dessert perhaps?). I wouldn't mind except for all the haughty pretense about Python being a language that doesn't do this sort of thing. Dear Python, get a grip, you're just another ASCII-abusing interpreted monstrosity like Perl, you just decided to make programmer life suck with whitespace instead.
- bpchaps 8y agoUnrelated 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!
- th 8y agoThanks for reporting this issue!
- cpsempek 8y ago> print(*more_numbers, sep=', ') This alone makes me appreciate print as a function in py3.
- sooheon 8y agoNow how much nicer would it be if even more things were functions, rather than special syntax like here? This can be done with an `apply`.
- nemetroid 8y agoI think the star syntax is a lot nicer than `apply`. Python actually had an `apply` builtin, but it was deprecated in Python 2.3.
- sooheon 8y agoApply, and first class / higher order functions in general are good in languages where they compose well. Python's * won't compose well.
- int_19h 8y agoHow wouldn't * not compose as well as apply?
- syrrim 8y agodef apply(f, a): return f(*a)
- quietbritishjim 8y agoTo add a small footnote to the existing replies to this comment: A related, but slightly different, function in the `functools` module (part of the Python standard library) is the `partial` function. It is like apply() but returns a function instead of calling the function and return its return value: f1 = partial(f, 1, 2) f2 = partial(f1, 3, 4) f2(5, 6) # equivalent to f(1, 2, 3, 4, 5, 6) It also supports kwargs.
- keyle 8y agoSide note... Why does everything needs to be bold on that page? It made reading it very difficult.
- kroltan 8y agoIt's... not? It looks like this: https://i.imgur.com/AfEnDf0.png https://i.imgur.com/AfEnDf0.png And this is what it would look like if was bold. I fiddled in the devtools to make this: https://i.imgur.com/zQQR1gA.png https://i.imgur.com/zQQR1gA.png
- th 8y agoI'm curious about what browser you're using. Side note: I typically bold many sentences in my articles, but this is the first article in a while where I actually bolded nearly no words at all. I wonder what my other articles look like in your browser.
- runarberg 8y agoSo this is like the rest and spread operator in javascript (`...`). An interesting observation is that while python has separate operators for lists ( * ) and dictionaries ( * * )—javascript has only `...`. > You need to be careful when using multiple times though. Functions in Python can’t have the same keyword argument specified multiple times, so the keys in each dictionary used with must be distinct or an exception will be raised. This is not an issue in javascript, the unpacked object with repeated keys takes the value of the last one. {...{a: 1}, ...{a: 2}} // => {a: 2} edit: formatting
- reificator 8y agoLooks like you lost some characters to HN's pseudo-markdown.
- the_clarence 8y agoSame with Golang: A := []int{1,2} B := append(A, A...) fmt.Println(B) > [1, 2, 1, 2]
- ereyes01 8y agoGolang has no equivalent to double-* since it doesn't do keyword arguments.
- masklinn 8y agoNeither does javascript, "keyword expansion" only occurs for object literals (which is why it's unambiguous: objects can't be created from sequences and don't implement the iteration protocol, so expansion within an object context can easily be special-cased).
- jacobolus 8y agoYes, the Python feature is much older. From what I can tell, Python inspired Ruby, Ruby inspired CoffeeScript, and CoffeeScript inspired JavaScript to adopt this feature.
- 8y ago
- reachtarunhere 8y agoThe single * way to specify keyword only arguments is a total monstrosity.
- masklinn 8y agoIt's a logical and consistent outgrowth of the existence of *args, and having (finally) allowed parameters after that (which would be keyword-only). I'm sure you could make up other ways to do it, but not that they'd be any better.