{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# How to defer evaluation of f-strings\n", "\n", "It seems that one solution is to use lambdas, which are explored below.\n", "\n", "What other solutions are there?" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "---\n", "\n", "Imagine that one wants to format a string\n", "selecting the format from several f-strings.\n", "Unfortunately, the obvious straightforward way\n", "of having the f-strings as values in a dictionary,\n", "does not work as desired,\n", "because the f-strings are evaluated when creating the dictionary.\n", "This unhappy way follows." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "year, month, day = 'hello', -1, 0\n", "date_formats = {\n", " 'iso': f'{year}-{month:02d}-{day:02d}',\n", " 'us': f'{month}/{day}/{year}',\n", " 'other': f'{day} {month} {year}',\n", "}" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Notice below that\n", "the current values of year, month, and day\n", "are ignored when evaluating date_formats['iso']." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2017 3 27\n", "hello--1-0\n" ] } ], "source": [ "year, month, day = 2017, 3, 27\n", "print(year, month, day)\n", "print(date_formats['iso'])" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "A solution is to use lambdas in the dictionary, and call them later,\n", "as shown below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "iso:\n", "2017-03-27\n", "2017-04-24\n", "2017-05-22\n", "us:\n", "3/27/2017\n", "4/24/2017\n", "5/22/2017\n", "other:\n", "27.3.2017\n", "24.4.2017\n", "22.5.2017\n" ] } ], "source": [ "year, month, day = 'hello', -1, 0\n", "# year, month, and day do not have to be defined when creating dictionary.\n", "del year # Test that with one of them.\n", "date_formats = {\n", " 'iso': (lambda: f'{year}-{month:02d}-{day:02d}'),\n", " 'us': (lambda: f'{month}/{day}/{year}'),\n", " 'other': (lambda: f'{day}.{month}.{year}'),\n", "}\n", "dates = (\n", " (2017, 3, 27),\n", " (2017, 4, 24),\n", " (2017, 5, 22),\n", ")\n", "\n", "for format_name, format in date_formats.items():\n", " print(f'{format_name}:')\n", " for year, month, day in dates:\n", " print(format())" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "This also answers the question about\n", "[what good use there is](http://nbviewer.jupyter.org/github/james-prior/cohpy/blob/master/20170130-cohpy-fstrings.ipynb)\n", "for\n", "[Zak K (aka y2kbugger)](https://github.com/y2kbugger)'s\n", "crazy whacko f-string lambdas.\n", "Thanks [Zak](https://github.com/y2kbugger)!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }