why sqrt is not a built-in function?

Michael F. Stemper mstemper at gmail.com
Fri Jan 15 16:49:52 EST 2021


On 15/01/2021 15.26, Stefan Ram wrote:
> "Michael F. Stemper" <mstemper at gmail.com> writes:
>> On 15/01/2021 14.01, Stefan Ram wrote:
>>> __import__( "math" ).sqrt( 4 )
>> I had no idea that syntax existed, and find it completely at odds
>> with The Zen of Python. I'm torn between forgetting that I ever saw
>> it and using it for some evilly-obfuscated code.
> 
>    When one collects snippets of Python code that are intended
>    to be inserted into programs, a snippet usually would have
>    to consist of two parts: One part to be inserted at the top
>    of the program, into the imports section, and then the actual
>    snippet.

This statement led inevitably to:

 >>> sqrt = __import__("math").sqrt
 >>> sqrt(4)
2.0
 >>>

I don't know why; it just did.

>    "__import__" allows to write snippets that can be inserted
>    as they are without the need to do insertions at two different
>    places. (Possibly with a tool to later normalize the insertions
>    to the usual style.)

That's much too mature. Es ist Feierabend!

-- 
Michael F. Stemper
2 Chronicles 19:7


More information about the Python-list mailing list