Implementing C-style for loops in Python for fun.
Project description
cursed-for
Implementing C-style for loops in Python:
for (i = 0; i < 10; i += 3):
print(i)
Installation
pip install cursed-for
Usage
Add the following # coding
comment at the top of the file in which you wish to
curse:
# coding: cursed_for
Then write the cursed for-loops as needed. Check this file for an example.
But why?
This was implemented because of a cursed idea I had one night, which I put on twitter:
And although this worked, I really didn't like the look of it. But using the methods I used, I was confined to Python's syntax, which really doesn't support the kind of things I'd have liked to do. But then I stumbled upon a couple packages which gave me exactly the tools I needed to commit this atrocity.
I wrote a blog on the approaches I went through to implement this.
The rough iterations that happened during the development are documented in the approach folder. It contains both the AST manipulation method, and the "truly cursed" method.
The "old way" usage
Note that this only really works in a REPL. To start it, run cursedfor.py in the terminal. It's a single file, you can get it by just downloading the one file if you want to.
>>> with _for(i := 0, i < 10, i += 2):
... print(i)
0
2
4
6
8
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for cursed_for-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ede1cb212e00e4e5678f4c51d9f919ee4eaca9903e83c4691129ca0d7dbb2a8 |
|
MD5 | 75137eaf8c9eae078f3c3ad465e2af0f |
|
BLAKE2b-256 | 780fb92e8b3b6e7f4d783658c1522c69d648caa05a088b4447c850a1739185e9 |