Convert python to JS?

I don't know JS, but I'm learning python, so I was wondering, is there a way to convert python to JS?

Probably. Ask your friends at DuckDuckGo.

Or stack overflow like a man
How fo you ask duck duck go anyway it's a search engine

You search for "convert Python to JS" (or maybe you spell out Javascript).

I highly recommend learning a bit of JavaScript at the least. It is also a fairly easy language to learn, like Python. It is not as extensive though as Py has a couple of features JS doesnt, but it will be performant in the long-run. Especially if you are trying to write JS with Snap! API, as I really don't expect anything written in python to be able to work with the Snap! API too well, or at least well enough to be considerably better than just using JS in the first place.

Really? It has a lot of libraries, but is there something in the language itself that JS is missing? JS has a real lambda, unlike Python's broken one.

P.S. Of course they both have terrible syntaxes. I hate the confusing punctuation in JS, but I'm terrified of the no-punctuation of Python.

1 Like

Well in my own personal opinion, python has a lot of very cool things like for generators, named parameters, keyword arguments, proper equivalence and value checking, and other things that are very useful and interesting. It also has a better / more well defined set of objects and datatypes with simpler and easier to set up classes, as well as things like operator overloading which just doesn't exist in JavaScript.
I still love Python a lot more than JS, but that doesn't mean I hate it.

I. just thought that @jens could add a python function block, but that's just a suggestion.

Okay, I guess those make sense. Maybe someday we'll add keyword inputs, although the notation would be a little tricky. Don't take this as a promise!

Oh, no, since browsers all support JS, Snap! is written in JS, which means it was pretty easy to add the JS Function block using JS's eval() function. But none of that is true of Python, so we'd need to write or borrow a Python interpreter in JS, which would probably double the size of Snap!. Not gonna happen, sorry. But if you found a Python interpreter written in JS, you could import the whole thing in a JS Function block. (Look at how the infinite precision integer library imports external JS code.)

function* myGenerator() { yield 0; yield 1; yield 2; }

5 + 3 == 8; "5" + "3" == "53";

(I don't know Python)

ok, I didn't know that.

Both of those are completely wrong.
For generators (not iterator functions):

v1 = (1,2,3)
v2 = (4,5,6)
print(tuple(a * b for a, b in zip(v1, v2))) # vector multiplication
print(list(2**num for num in range(11))) # powers of 2
print(
  list(
    list(
      1 if num == x else 0 for num in range(5) 
    ) for x in range(5)
  )
)
# creating an identity matrix

Yes the syntax is flexible, but only within brackets, parenthesis, curly braces, etc. as each complete statement must be indented properly. It is not complete without matching closers.

mylist = [1,2,3,4]
print(1, 2, 3, *(a+3 for a in mylist), 8, 9) 
# unpack results to ship to function parameters

And there is literally no way to overload operators like this

class Vector3:
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z
    def __str__(self):
        return f"<{self.x}, {self.y}, {self.z}>"
    __repr__ = __str__
    def __add__(self, other):
        return Vector3(self.x+other.x, self.y+other.y, self.z+other.z)
print(Vector3(1,2,3) + Vector3(4,5,6))

I literally had to write add methods and do v1.add(v2) in the other scripts.
also python does have lambdas similarly:

pow2 = lambda x: x**2
pow = lambda x, y: x**y
print(*(pow2(num) for num in range(11)))

don't see what you mean by broken as it is perfectly fine to send these into a function, unless you are trying to create multi-line lambdas which are very popular for things like

myJsEvent.on("xyz", (params) => {

});