6 ms·
Can you provide some more info / links regarding the “# coding:” feature? I wasn’t able to find anything.
by develatio 2y ago
Can you provide some more info / links regarding the “# coding:” feature? I wasn’t able to find anything.
- deleted 2y ago[deleted]
- klibertp 2y agoIf you place `# coding: utf-8` on the first line of a Python script, it'll try to interpret the raw bytes contained later in the file by passing them through a relevant codec[1]. Since the codec receives the raw source and transforms it before any interpretation happens, you can (here's the point I'm starting to guess) supply a codec that parses the code, transforms the AST, and dumps it back to the source for execution. It would be similar to how "parse transforms" work in Erlang, though there you're handed AST and not bytes. [1] https://docs.python.org/3/library/codecs.html https://docs.python.org/3/library/codecs.html
- zahlman 2y ago>how "parse transforms" work in Erlang, though there you're handed AST and not bytes. Oh, I didn't know about this. Was thinking about doing something similar in my own language, so I'll have to check that out.
- JackC 2y agoThe idea is you can register a custom file encoding, analogous to built-in file encodings like utf-8, and use it to transform the source file before python loads it. An example is pyxl, kind of like jsx, where you put `# coding: pyxl` at the top and then it will transform bare `<html>` in the python file into calls to a template builder: https://github.com/gvanrossum/pyxl3 https://github.com/gvanrossum/pyxl3 Incidentally this turns out to be super hard to search for without asking an LLM, since "python coding" is so overloaded, and using the feature this way is intentionally undocumented because it's not really what it's for, and not something I think most python users really want to encourage. So, forbidden python knowledge!
- kevindamm 2y agoIt's specified in PEP 263 [0] which pulls up easier on search if you quote it and include the typical "special comment symbol" -*- around it. [0]: https://peps.python.org/pep-0263/ https://peps.python.org/pep-0263/
- BiteCode_dev 2y agoCodecs (https://docs.python.org/3/library/codecs.html https://docs.python.org/3/library/codecs.html) can change the content of the file on the fly. You can abuse that to create new syntax, although it is evidently not the original intent. Let's say you hate significative spaces, here is a (very fragile) PoC for your pain: https://0bin.net/paste/42AQCTIC#dLEscW0rWQbE70cdnVCCiY72VuJwnSoLfJ0FlEGl82m https://0bin.net/paste/42AQCTIC#dLEscW0rWQbE70cdnVCCiY72VuJw... Import that into a *.pth file in your venv, and you can then do: # coding: braces_indent def main() { print("Hello, World!") if True { print("This is indented using braces.") } } You also can use import hooks (python ideas does that), bytecode manipulations (pytest does that) or use the ctypes module (forbiddenfruit does that). Still, I'm very happy it stays limited to super specific niches. Big power, big responsibilities, and all that.