19 ms·
Show HN: Python with do..end in place of strict indentation
- gus_massa 2y agoSorry, I have to ask... Does it support also braces? :) IIUC the source is writen as a long heresting and then executed. Does it have the same speed than normal Python? Does it suppont numpy, numba and other similar packages that use jit? Can a function inside the modified code call a function outside? (It may be helpful for porting conde one function at a time.) Is it possible to do something similar with a decorator instead of a herestring?
- humility 2y ago> Does it also support braces? No :) > the source is written as a long herestring and then executed that's correct but only for single file modules. you can actually have a python like structure comprising entirely of dopy files and it will be compiled in place or in temp dir and executed as a normal python package with distributed source > Does it have the same speed than normal Python It's just a transpiler so the code is actually executed by the python interpreter itself. So same speed as python with an extra build step > Does it support numpy, numba, other packages that use jit? Not now > Can a function inside the modified... Certainly > Is it possible to do something similar with a decorator instead of a herestring? Could you explain?
- zahlman 2y ago(This sort of thing has been done before, and of course it will never make its way into the official Python distribution; but it's always fun to see.) >Is it possible to do something similar with a decorator instead of a herestring? No, this is a fundamental change to Python syntax. To apply a decorator to existing Python code, the decorated code has to compile first, creating a Python object (generally either a function object or "type" i.e. class object) which is passed to the decorator (which is really just a higher-order function with special syntactic support). But of course, you could write the code to preprocess in a separate file with a different extension, and then have actual Python code which reads and preprocesses it and then evals the result. (The internals could also be written differently, in terms of AST manipulations using the "compiler services" part of the standard library. But this way is probably easier.) On Linux, you could also have a top-level script which does those steps for a specified input filename, and then specify that script in the shebang line for the Dopy code file.
- 1oooqooq 2y agoi imagine the thought process went "oh, indentation is much inferior to braces. i know, let me use the second worst option to braces."
- relyks 2y agoMake Python look like Ruby
- scotty79 2y agoIt is and it's .dopy
- actionfromafar 2y agoToo late for those trains, but imagine a world where GIMP would have copied the UI of Photoshop and Python the syntax of Ruby. A happy world.
- driggs 2y agoAnyone who can't see past Python's indentation-based syntax is a person who doesn't understand Python. (Both literally and figuratively!) When "significant whitespace" is at the top of someone's complaints about Python, I'm immediately done hearing about their superficial criticism of the language.
- ahartmetz 2y agoYeah. It avoids the problem of indentation disagreeing with code structure and reduces visual clutter and... that's it.
- tacticus 2y agoyou can solve that with a formatter. and then you'd get consistent outputs that work reliably as you move code.
- Sohcahtoa82 2y agoWhen I hear someone complain about the whitespace thing, it sends a message to me that they do not format their code well and just go willy-nilly on their indentation. If you're properly indenting code, then it always works as intended. Proper indentation doesn't come from a desire to make the code work, it comes from a desire to make it readable. It just happens that readable means correct in Python's case. It literally takes me zero thought to get indentation right. If you can't tell when to start or end indentation level, then I'd question if you know when you need to use an open/close brace in other languages. I mean, yeah, sometimes copy/pasting code doesn't work precisely as intended, but all you have to do is select the code, hit Tab or Shift-Tab, and any sane editor will add/subtract an indentation level to the code.
- HexDecOctBin 2y agoI just translated all my build scripts written in Python to straight C due to indentation issues. Not because I don't format my code properly, but because any time I refactored my code, I ended up with dozens of subtle indentation related bugs that took hours to find and fix. If your language requires an IDE, then you have become Java.
- airstrike 2y agoI used to code in Python for everything in the late aughts and loved meaningful whitespace. Whenever I used a language with braces like JavaScript, for instance, I felt like I was still indenting code meaningfully but now I had the extra hurdle of also caring about the braces. It felt unnecessary and stone-agey. But back then I was just using vim with a nicely configured .vimrc. Linters weren't really a thing, or at least not neatly integrated into my editing experience. Nowadays I write Rust in vscode and I love braces. Rustfmt just formats my code every time anyway, so I don't have to care about indentation and braces are placed where they should be. I spend zero time caring about code formatting, outside of configuring rustfmt.toml once per project or the occasional #[rustfmt::skip] for codeblocks which I really want to format in a specific way outside of the linter's configured rules. I also have the benefit of the compiler screaming at me if I forget a brace, pointing to the exact error including telling me when indentation is suggesting a brace is missing! :O Moreover, I'm much more proficient in vim, and the single hotkey % is reason enough to want to favor braces. Coupled with how often I paste code into Claude, V$%,y has become muscle memory (visually select this whole line, go to the end of the line, then expand the selection to the line matching the closing delimiter under the cursor, then yank everything into the system clipboard with <leader>y)... On occasion, I go back to Python for some side project and the whole experience feels... weird. If braces before LSPs and linters was the stone age, then Python feels like the iron age and I'm living in the future with a compiler that is incredibly talkative and helpful. I'm never going back.
- kstrauser 2y agoI feel the same way with my editor running Ruff on save. I still have to get the indentation right (which the editor handles for me), but that's about it. Once I hit cmd-s, everything subtly shifts to where it's supposed to be.
- sanderjd 2y agoI feel like your conclusion is backwards based on your argument... > I also have the benefit of the compiler screaming at me if I forget a brace, pointing to the exact error including telling me when indentation is suggesting a brace is missing! What the compiler is telling you here is that the braces are superfluous, because the indentation is already describing the structure correctly. So why bother? My take is that in the (amazing!) new world with near universal usage of standardized linters, this whole debate is stale, it just doesn't matter either way. Everything is indented properly, whether or not it is necessary for correctness. So whether there is a bit of additional bracing or not, who cares?
- scotty79 2y agoSeeing code blocks as braced or meaningfully indented should be a switch in your IDE.
- sanderjd 2y agoYeah I once saw a great talk at a functional programming conference by a Scala compiler developer, making the point that programming language semantics should be specified at the AST level, with syntax entirely up to the user's whim. I consider this to be both totally sensible and completely impractical :)
- scotty79 2y agoI tried using Idea for Scala 3 where indents are meaningful. I prefer size 4 tabs for indents. There were four knobs for adjusting indents and until I adjusted all of them the IDE was messing up my indents in subtle ways. If leading editors have trouble abstracting indentation from its textual representation I have zero hopes for them being able abstract even such simple element of AST as a block.
- sanderjd 2y agoAgreed. I think it would require a lot of work on tooling to make this idea work, for fairly little gain. I still think it's an elegant idea though.
- scotty79 2y agoI think we might get it implemented some day but it's gonna be approached from the side of visual programming. Those systems just need to relax a bit and allow some structure inside their little rectangles. Then the internal structure will be able to easily have multiple views and editors.
- animal_spirits 2y ago> I built this to learn more about python/interpreter language syntax in general, but not interested in pursuing this further. Good job trying something new out! There is always room for experimentation, if any reason to try to learn something new.
- joshdavham 2y agoYeah I think it’s a cool idea but also a little funny to see that the project is archived after clicking on it for the first time haha
- kazinator 2y agoPlease make the corner/base cases more prominent. For instance, the question obviously arises: can we have this: def function(x) do end without writing "pass"?