top | item 42655563

Show HN: Python with do..end in place of strict indentation

12 points| humility | 1 year ago |github.com

70 comments

order

driggs|1 year ago

Anyone 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.

mkl|1 year ago

Agreed. I find such complaints very strange, as most people already follow Python-like indentation rules quite strictly in most other languages, but need to add delimiters as well. Indentation is the clearest indication of code structure, so when it's not significant it still looks significant, leading to bugs like:

  if(condition)
      do_thing1();
      do_thing2(); //not in the if!
  do_thing3();

dariusj18|1 year ago

My problem with indentation as significant is when I paste a chunk of code inside some chunk that is indented differently. Some may say that a good text editor will deal with that for you, but it's rarely been the case for me. The most is when the code is longer than my screen height and I have to scroll down and figure out where the pasted chunk ends and indentation it all who knows how many times without scrolling back up to the top of the chunk.

wwfn|1 year ago

Literate programming tools that weave/tangle code and documentation don't play well with significant whitespace. Not a problem for me, but I think it's on a valid line of complaints.

dgfitz|1 year ago

I agree. I think it’s only an issue because we as a collective didn’t sort out a tabs-vs-spaces debate and new people to the language struggle with that bit.

jokoon|1 year ago

Personally I started making a language that uses it

everyone|1 year ago

I dont think it's superficial. Having characters that are invisible affect your code is weird and bad.

Which is a shame, cus otherwise Python is good imo.. I feel like its creator just had a weird whitespace fetish or something.

Sohcahtoa82|1 year ago

When 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.

ahartmetz|1 year ago

Yeah. It avoids the problem of indentation disagreeing with code structure and reduces visual clutter and... that's it.

animal_spirits|1 year 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|1 year ago

Yeah 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

gus_massa|1 year ago

Sorry, 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?

zahlman|1 year 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.

humility|1 year 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?

airstrike|1 year ago

I 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.

sanderjd|1 year ago

I 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?

kstrauser|1 year ago

I 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.

maleldil|1 year ago

Regarding your Claude copying flow, there's a Python-compatible alternative. In nvim, you can use treesitter selections to select a block, and treesitter is aware of the indentation.

scotty79|1 year ago

Seeing code blocks as braced or meaningfully indented should be a switch in your IDE.

sanderjd|1 year ago

Yeah 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 :)

kazinator|1 year ago

Please 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"?

1oooqooq|1 year ago

i imagine the thought process went "oh, indentation is much inferior to braces. i know, let me use the second worst option to braces."

relyks|1 year ago

Make Python look like Ruby

actionfromafar|1 year ago

Too 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.

scotty79|1 year ago

It is and it's .dopy