TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.86k stars 215 forks source link

object() constructor #840

Closed astavely closed 1 year ago

astavely commented 1 year ago

In my Python program I have:

ANY = object()

Transcrypt translates this to:

export var ANY = object ();

But the script won't execute. My browsers (Firefox 109.0.1, Safari 15.6.1) apparently don't recognize "object" as a JavaScript function. Something missing from the Transcript-generated runtime perhaps?

astavely commented 1 year ago

I should have said: that's perfectly good Python, and the Python program has worked fine for years.

JennaSys commented 1 year ago

Not sure I understand what your desired end result is. Are you trying to create a bare JavaScript object? If so, you can try using window.Object() which would be more explicit and might have a better chance of being transpiled correctly. You may also have to use the Transcrypt __new__() function in this case as well.

Issue #665 may provide some more clues, and also check the docs on Creating bare JavaScript objects for another possible option.

astavely commented 1 year ago

I'm creating an atom, as in LISP or Erlang. It 's just a marker. It's an object that compares equal to itself and unequal to any other Python object using "is" or "==". That's all it's for.

The proposed solutions are not more explicit, they are obfuscation. I'm not doing anything with windows. And I'm writing Python, not Javascript.

If I have to know about special cases that Transcrypt won't translate correctly, and write workarounds in which I have to think about the JavaScript translation, I won't use Transcrypt.

JennaSys commented 1 year ago

Sorry, I assumed you were translating Python to JS for use in a browser, window is a browser object that the JS Object() belongs to in that case and has nothing to do with the Windows OS.

astavely commented 1 year ago

Yes, I want to run the Python program in a browser window as if it were a terminal window.

But the Python program will still be used in other environments, so I don't want to put Transcrypt-specific code into it.

Thanks,

- Allan

On Mon, Feb 27, 2023 at 10:35 AM John S @.***> wrote:

Sorry, I assumed you were translating Python to JS for use in a browser, window is a browser object that the JS Object() belongs to in that case and has nothing to do with the Windows OS.

— Reply to this email directly, view it on GitHub https://github.com/QQuick/Transcrypt/issues/840#issuecomment-1446755136, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAU6EBN36W47SHPXMSCPZMTWZTQXPANCNFSM6AAAAAAU74XN7I . You are receiving this because you authored the thread.Message ID: @.***>

JennaSys commented 1 year ago

Considering the differences in runtime environments, sometimes having "Transcrypt-specific code" is unavoidable depending on what you are trying to accomplish. This is one reason why Transcrypt has pragma notations (compiler directives). But, even with these differences, you can definitely create Python code that will work in both environments. It does sometimes require coding workarounds to solve impedance mismatches between Python and JavaScript though.

Specifically the # __pragma__ ('ecom') directive for code that only needs to be used on the JS side and the # __pragma__ ('skip') directive for code that only needs to be used on the Python side. I personally use the skip/noskip directive all the time to keep the Python linter happy when referencing JS libraries in Python code. The Transcrypt docs are actually pretty good IMO, but there is a lot there to digest and involves a subsequent learning curve in order to get the most out of Transcrypt.

TBH, if you are looking to stay 100% in Python and never have a need to make any calls to JS libraries in your application, you might want to look at using PyScript (WASM), as it might be a better fit depending on where you need to deploy it, and if you can tolerate the initial load time. There's also libraries like Brython and Skulpt that might be potential alternate options. You just have to figure out which one is best for your situation and priorities. This post by Anvil is getting a bit dated, but it helped me out a lot when I first decided that Transcrypt was the best solution for me.

I'm not trying to push you away from using Transcrypt either. It's really versatile and I absolutely love it despite whatever imperfections and limitations it may currently have. But you sometimes have to dig into it a little to achieve a specific result, and I recognize that it isn't always the best solution for every situation.

astavely commented 1 year ago

Thanks! I took a look at PyScript and I thought it was exactly what I wanted.

But it doesn't do input() nicely. It pops up a text entry box and grays out the rest of the window. I can type input lines, but I don't see the program's responses to them until the program terminates! I don't see a dialog as I would if I were running the program in a terminal window.

From the PyScript documentation and what I can find with online searches, I don't think I can get PyScript to behave differently. (No, I'm not asking for help with PyScript.) So I'm abandoning it.

I may come back to Transcrypt some day and deal with the the workarounds I need. But for now I'm stepping away from my project for a while.

Anyway, many thanks for the help!

- Allan

On Mon, Feb 27, 2023 at 1:23 PM John S @.***> wrote:

Considering the differences in runtime environments, sometimes having "Transcrypt-specific code" is unavoidable depending on what you are trying to accomplish. This is one reason why Transcrypt has pragma notations (compiler directives). But, even with these differences, you can definitely create Python code that will work in both environments. It does sometimes require coding workarounds to solve impedance mismatches between Python and JavaScript though.

Specifically the # pragma ('ecom') https://www.transcrypt.org/docs/html/special_facilities.html#skipping-transcrypt-code-fragments-when-running-with-cpython-pragma-ecom-and-pragma-noecom directive for code that only needs to be used on the JS side and the # pragma ('skip') https://www.transcrypt.org/docs/html/special_facilities.html#skipping-fragments-while-generating-code-pragma-skip-and-pragma-noskip directive for code that only needs to be used on the Python side. I personally use the skip/noskip directive all the time to keep the Python linter happy when referencing JS libraries in Python code. The Transcrypt docs are actually pretty good IMO, but there is a lot there to digest and involves a subsequent learning curve in order to get the most out of Transcrypt.

TBH, if you are looking to stay 100% in Python and never have a need to make any calls to JS libraries in your application, you might want to look at using PyScript https://pyscript.net (WASM), as it might be a better fit depending on where you need to deploy it, and if you can tolerate the initial load time. There's also libraries like Brython and Skulpt that might be potential alternate options. You just have to figure out which one is best for your situation and priorities. This post by Anvil https://yasoob.me/2019/05/22/running-python-in-the-browser/ is getting a bit dated, but it helped me out a lot when I first decided that Transcrypt was the best solution for me.

I'm not trying to push you away from using Transcrypt either. It's really versatile and I absolutely love it despite whatever imperfections and limitations it may currently have. But you sometimes have to dig into it a little to achieve a specific result, and I recognize that it isn't always the best solution for every situation.

— Reply to this email directly, view it on GitHub https://github.com/QQuick/Transcrypt/issues/840#issuecomment-1447030468, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAU6EBLZFPNPWKT2TSEGJBLWZUEK7ANCNFSM6AAAAAAU74XN7I . You are receiving this because you authored the thread.Message ID: @.***>