5 ms·
my favorite cheat is using locals() with string formatting something like this: def example_cheater(color, flavor, age): template = 'I am {age} ye
by dec0dedab0de 8y ago
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.
- 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.
- sys_64738 8y agoDoes this trip up pylint?
- nercht12 8y agoTaken straight out of Ruby, I see. Very convenient feature (and one of my favorites about Ruby), though it does contradict the do-it-one-way mentality.
- bhrgunatha 8y agoAccording to Rosetta Code Algol 68 had string interpolation. [1] It'd be interesting to see if there are any older languages that supported it too. https://rosettacode.org/wiki/String_interpolation_(included)#ALGOL_68 https://rosettacode.org/wiki/String_interpolation_(included)...
- setr 8y ago>though it does contradict the do-it-one-way mentality. not really: use f-strings when you're passing in variables as-is (or nearly no work), and format() when you need to do work on them before stringifying them; f-strings are naturally (and obviously) more difficult to read when the variables are big, as it obscures the actual text they're being fit into, and where. The only natural area for preference to apply is whether to do the work before the format() call, name the variables, and change it to an f-string.. or stick with a multi-line format() I'm not sure theres any real situation where the choice isn't obvious. Maybe if you're doing something like f"list1: {sorted(a)}\nlist2{sorted(b)}", where the work is rather small, but even then f"list1:{}\nlist2:{}".format(sorted(a), sorted(b)) is just as nice, or rather unsatisfying, as the f-string.
- masklinn 8y agof-strings are also unusable for i18n.
- nercht12 8y ago> not really: Yes it does. You could construct the string using a for-loop. Thank you for proving my point and then down-voting me.
- setr 8y ago...if you’re going down that route, you could claim that any feature not necessary for turing completeness conflicts with the one way to do it strategy, since you could alternatively build up whatever feature from scratch, thus offering additional solutions But practically, there remains (close to) one obvious way to do things, with the choice boiling down to whether or not to name your outputs before string construction (and the choice of f-string vs format naturally falling out). But thats always been a choice. On a side note: I don’t use comment voting systems, for up or down votes, on pretty much any site including HN. Even if I did, I don’t see why you’d care
- chestervonwinch 8y agoNeat: >>> pi = 3.14159; two = 2 >>> f"{two:02d} pi is {2*pi:.2f}" '02 pi is 6.28'
- alex- 8y agoMy 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
- deleted 8y ago[deleted]
- pfranz 8y agoMy frustration with locals() and globals() is the same with "from foo import *" It's really difficult to move that code around or clean up variables because their use is obscured. The worst is when your template variable is defined somewhere else.