isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Executing Python code in Pyth? #264

Closed cpaca closed 2 years ago

cpaca commented 2 years ago

As title says, would it be possible to add something that executes Python code into Pyth?

The main reason I ask for this is that there will - inevitably - always be something inside Python that can't be done in Pyth. Whether this is because a coding challenge allows import statements for specific types of libraries (which, obviously won't exist in Pyth, only Python) or something else, Python will generally be more powerful than Pyth.

Plus, Python has zip(), which I couldn't find a Pyth equivalent for. Python also has functions with any number of variables, which I could find hints of being in Pyth, but has no examples (at least in the documentation page)

That said, getting Pyth to define as many pre-defined constants as possible (before using those constants inside exec()) would also save many characters, given the nature of Pyth.

Couldn't find any other issues that explicitly request this. Closest would be #156 asking for Pyth exec() (instead of Python exec()) and #30 , where @isaacg1 suggests having .x do exec(), but in the end (... not in that thread, but somewhere...) .x becomes try/catch.

cpaca commented 2 years ago

Actually, I see that .x got used, but .X and .Y are available. Maybe .X can be made into a Pyth exec() (to fulfill #156) and then .Y can be made into a Python exec()?

Or, better yet, just make .X take two values: An int and a string. If the int is 0, it's a pyth exec(), but if the int is any other value, it's a Python exec()

cpaca commented 2 years ago

Well I made .X into exec().

Here it is.

Not going to make a PR for it though, because I can't actually check if the fork worked or not. I run Python on TIO (and the helper website you've given) but not on my own computer, so I wouldn't know how to set it up.

cpaca commented 2 years ago

I'm an idiot. This functionality is already doable, since the eval() is a thing, though not directly.

isaacg1 commented 2 years ago

I think what you're looking for is $, which allows Python literals to be embedded into Pyth. For dynamic Python execution, eval is your best bet, yes.