One of my favorite features of Python 3 is f-strings (Formatted String Literals).
Each of these print
statements emits the same output. But which of them is the easiest to write and read?
company_name = 'Beeswax'
location = 'New York City'
print('The company {} is based in {}. {} is a great place to work.'.format(company_name, location, location))
print('The company {company_name} is based in {location}. {location} is a great place to work.'.format(company_name=company_name, location=location))
print(f'The company {company_name} is based in {location}. {location} is a great place to work.')
Even if that’s all they did, I’d love f-strings. But they’re much more powerful than that. (For example, you can dereference objects inline: f"Number of connections: {aerospike_client.connection_count}"
) In fact, you can evaluate arbitrary expressions including function calls within the f-string braces, and all variables in scope can be passed as arguments. See the references below for the details.
https://docs.python.org/3.7/reference/lexical_analysis.html#f-strings
I discovered this subtle change when one of my unit tests mysteriously failed once I…
Exception chaining solves two problems in Python 2. 1. Swallowed Exceptions If, while handling exception…
Here's the recording of my talk "15 minutes w/ Beeswax: Safe Password Storage - How…
My presentation from the NYC Python Meetup: Python at Scale; Concurrency at Beeswax. Concurrent Python…
My BazelCon 2019 lightning talk, Python Dependencies The Right Way*, has been posted. Please excuse…
I wanted to combine the simplicity of Python's built-in WSGI server with the benefits of…
This website uses cookies.