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

dn PythonList at DancesWithMice.info
Mon Feb 19 15:11:31 EST 2024


On 20/02/24 05:58, Grant Edwards via Python-list wrote:
> 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

Some of this (Expression components inside f-strings) newly available in 
v3.12 (PEP-701) - which can be used in production...

-- 
Regards,
=dn


More information about the Python-list mailing list