Home Technology Hacking The Python For Loop

Hacking The Python For Loop

0

[ad_1]

Within the early days of C, you’d often see somebody — in all probability a former Pascal programmer — write one thing like this:

#outline BEGIN {
#outline END }

This could often provoke complaints about abusing the preprocessor and usually being anti-C. Absolutely no trendy language would allow such issues, proper? Maybe not. Contemplate [Tushar Sadhwani] who wished to create a basic C-style for loop within Python. He did it, and the journey is probably extra fascinating than the outcome.

First, you possibly can’t simply transport straight C for loops into Python. There must be some concession to Python syntax. The preliminary try was intelligent however not intelligent sufficient. Nevertheless, the disassembly of the Python code was telling. The second try, nevertheless, was notably fascinating.

That try used an odd function to look at the interpreter’s tree construction for the code after which modify it. That is form of like a really painful C preprocessor however extra highly effective. That model works though it’s fairly convoluted.

Mockingly, [Tushar] then arrange a 3rd try after seeing code that tries to interchange Python indentation with braces utilizing a codec. In Python-speak, a codec enables you to convert totally different textual content encodings. Nevertheless, you are able to do different issues than textual content encoding conversion. That is closest in spirit to the C preprocessor technique. You may wade by means of the supply code forward of processing and make whichever adjustments you see match.

Is any of this actually helpful? In all probability not as it’s. However you by no means know while you may must do one thing unique and considered one of these strategies might save the day. You in all probability couldn’t get away with a few of this on MicroPython, in fact. Your mileage could range relying on the place you discover your Python operating — like the Internet.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here