Live data from Hacker News

Asterisks in Python

treyhunner.com

81–90 of 110 posts

Re: Asterisks in Python

#81

Earlier quoted context omitted.

Honest 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?

For 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…

In Haskell, these direct equivalents aren’t bad at all:

    cityToState = Map.fromList
      [ (city, state)
      | (state, cities) 
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.

Re: Asterisks in Python

#82
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!

How would you approach decorating /any/ function without args, kwargs or something very similar?

  def decorator(f):
      def new_f(*args, **kwargs):
          do_something()
          return f(*args, **kwargs)
      return new_f

Re: Asterisks in Python

#83
post #79
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!

True, Python allows a lot of crazy hacks like monkeypatching, dynamically creating classes and so on. It is there if you need it. It is not like in Java where those things are simply impossible. The Python culture on the other hand discourages obscure hacks and unreadable cleverness, and the language is generally designed to encourage the straightforward and readable solution. I don't really see a contradiction in th…

GP's quotes are paraphrased from PEP 20 and I think _are_ put of Python culture, at least as I've experienced it.

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

Re: Asterisks in Python

#84
post #66
post #28

Earlier quoted context omitted.

This 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 gri…

Aside: Swedish does have the word 'kvarg', which is a product made out of sour milk. Some people eat it for breakfast but personally I can't stand it.

This is legitimately my favourite comment of the day.

Re: Asterisks in Python

#85
post #79

Earlier quoted context omitted.

True, Python allows a lot of crazy hacks like monkeypatching, dynamically creating classes and so on. It is there if you need it. It is not like in Java where those things are simply impossible. The Python culture on the other hand discourages obscure hacks and unreadable cleverness, and the language is generally designed to encourage the straightforward and readable solution. I don't really see a contradiction in th…

GP's quotes are paraphrased from PEP 20 and I think _are_ put of Python culture, at least as I've experienced it. https://www.python.org/dev/peps/pep-0020/

I'm afraid you've misunderstood olavk's point. 'The "quote" you made up' certainly refers to

> "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!"

not to

> "explicit is better than implicit" and "there should be one and only one good way"

Re: Asterisks in Python

#86
post #28

Earlier quoted context omitted.

This 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 gri…

At least the kwargs word is googleable. So it is relatively easy to find documentation. Unlike list comprehensions which are really hard to google until you know the name.

"for loop inside list python" seems to find a stackoverflow thread explaining them. It took me quite a while to bother trying that when I was first learning python though.

Re: Asterisks in Python

#87
post #85

Earlier quoted context omitted.

GP's quotes are paraphrased from PEP 20 and I think _are_ put of Python culture, at least as I've experienced it. https://www.python.org/dev/peps/pep-0020/

I'm afraid you've misunderstood olavk's point. 'The "quote" you made up' certainly refers to > "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!" not to > "explicit is better than implicit" and "there should be one and only one good way"

Doh, I think you're right!

Re: Asterisks in Python

#88

Earlier quoted context omitted.

For 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…

In Haskell, these direct equivalents aren’t bad at all: cityToState = Map.fromList [ (city, state) | (state, cities) 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.

And for those following who don’t know Haskell, both of these are basically sugar for concatMap.

(>>=) = flip concatMap

Re: Asterisks in Python

#89
post #79
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!

True, Python allows a lot of crazy hacks like monkeypatching, dynamically creating classes and so on. It is there if you need it. It is not like in Java where those things are simply impossible. The Python culture on the other hand discourages obscure hacks and unreadable cleverness, and the language is generally designed to encourage the straightforward and readable solution. I don't really see a contradiction in th…

> The Python culture on the other hand discourages obscure hacks and unreadable cleverness

I guess Pandas, Matplotlib, SQLAlchemy, etc, etc didn’t get the memo.

Re: Asterisks in Python

#90
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!

> This is one of those areas where I find Python a little contradictory. It's not contradictory. You should never use args and kwargs unless you need them. Usually they're only needed in relatively rare situations, like A) when you're building a dictionary dynamically without knowing what the key names will be in advance B) when building a library where you want to allow people to subclass a parent class, but also re…

They absolutely do solve a problem. But I can't help but wonder if it would have been possible to find a way to let people solve that problem in a more explicit manner. The specific solution the Python team chose smells to me like one that was optimized for the convenience of the language's implementors more so than its users.
Post reply on HN