Python lets you do interesting things in f-strings
you can put an f-string inside of an f-string
>>> a = {"jkl": 123}
>>> b = {"0x7b": "beep"}
>>> f"{b[f"{hex(a["jkl"])}"]}"
'beep'and you’re not just limited to that. your string can actually take user input and import all on it’s own!
>>> f"{"".join(__import__("importlib").import_module("string").ascii_lowercase[int(i)] for i in input(">").split(' '))}"
>1 4 4 15
'beep'
we all know the classic construct
if __name__ == "__main__":
main()but that’s boring and unoriginal. next time you write a script, switch it up!
f"{__name__ == '__main__' and main()}"everyone reading will be very impressed by your clever thinking and enjoy a fun little puzzle!
brie@parmesan ~/c/cheese-paper (main)> cat scripts/crimes.py
def main():
print("beep")
f"{__name__ == '__main__' and main()}"
brie@parmesan ~/c/cheese-paper (main)> python scripts/crimes.py
beep
you can also use this in bash. it’s really helpful when you want a simple one liner but still keeping nice output formatting!
$ python -c 'print(f"{__import__("importlib").import_module("os").get_terminal_size()}")'
os.terminal_size(columns=148, lines=22)
@charlotte absolutely, I bet you can do all sorts of interesting things with that