Can one output something other than 'nan' for not a number values?

Grant Edwards grant.b.edwards at gmail.com
Mon Feb 19 11:58:31 EST 2024


On 2024-02-19, Chris Green via Python-list <python-list at python.org> wrote:

> It's using f'{...}' at the moment.

Here's a demonstration of how to hook custom code into the f-string
formatting engine. It's brilliantly depraved.

https://stackoverflow.com/questions/55876683/hook-into-the-builtin-python-f-string-format-machinery

>From the above:

    You can, but only if you write evil code that probably should
    never end up in production software. So let's get started!
    
    I'm not going to integrate it into your library, but I will show
    you how to hook into the behavior of f-strings. This is roughly
    how it'll work:
    
     1. Write a function that manipulates the bytecode instructions of
        code objects to replace FORMAT_VALUE instructions with calls
        to a hook function;
    
     2. Customize the import mechanism to make sure that the bytecode
        of every module and package (except standard library modules
        and site-packages) is modified with that function.

Final code is here:

https://github.com/mivdnber/formathack



More information about the Python-list mailing list