Live data from Hacker News

Asterisks in Python

treyhunner.com

1–10 of 110 posts

Re: Asterisks in Python

#2
I'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.

Re: Asterisks in Python

#3
Whats with the / in the help for sorted?

    Help on built-in function sorted in module builtins:

    sorted(iterable, /, *, key=None, reverse=False)

Re: Asterisks in Python

#4
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.

Re: Asterisks in Python

#5

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.

Calling calling a Python standard library function "not pythonic" is a great example of when the term "pythonic" is beyond usefulness.

Re: Asterisks in Python

#6

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.

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

Re: Asterisks in Python

#7
post #6

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.

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

For reference, this feature is Formatted String Literals ("f-strings"). https://docs.python.org/3/reference/lexical_analysis.html#fo...

Re: Asterisks in Python

#8
post #6

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.

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

Didn't know this. Thanks for sharing!

Re: Asterisks in Python

#9
post #6

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.

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

OMG! 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.

Re: Asterisks in Python

#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!
Post reply on HN