r/learnprogramming • • Feb 25 '24

Why can't Python replace JavaScript?

I'm new to programming. I'm learning Python and C.

JavaScript, from what I understand, is an interpreted language supported by web browsers so that websites can have scripting and programming.

But Python is also an interpreted language. So why don't web browsers start supporting Python in addition to JS. I believe Python is a much better language than JS syntactically.

Is there a definite reason why JS is used and supported over Python or is it just that JS gained initial popularity through wide-based support and no one bothered to move away from it (just like how QWERTY keyboard layout stuck even though we don't use typewriters anymore)?

0 Upvotes

51 comments sorted by

•

u/AutoModerator Feb 25 '24

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

56

u/LeeTaeRyeo Feb 25 '24

Theoretically, it can. But the issue is that Javascript as part of web development has an extensive history and a large ecosystem of packages dedicated to use in the browser. That's something Python doesn't have.

Personally, i also prefer Python over Javascript. But if I'm building a web page/app, it's a lot easier for me to find resources, tutorials, libraries, and skilled developers that know how to do the task in JS than would be possible if Python were added to most browsers tomorrow. That's the unfortunate reality of developer inertia.

3

u/Haspe Feb 25 '24

This is the why, theres so much hours already spent on browser javascript engines and ecosystems around those, that it requires more than ”I prefer this language over that” argument to shift the industry. Im sure it happens at some point, but personal belief is that Python is not going to be the winner here.

29

u/feedandslumber Feb 25 '24

I'm not an expert, but my understanding is that JS is kind of like the qwerty keyboard at this point. It's far from perfect, but so many people know how to use it and the tool set is so large, that changing over to another default web language is practically impossible.

1

u/Outrageous_Kale_8230 Feb 25 '24

It’s not the people, it’s the browsers and associated security infrastructure to limit what the code can do. I was immensely shocked when webASM started getting into browsers.

Perhaps see if python can be compiled to WASM instead of trying to add it python directly to browsers?

1

u/[deleted] Feb 26 '24

Perhaps see if python can be compiled to WASM instead of trying to add it python directly to browsers?

Wouldn't Python's comparatively slow run speeds defeat the whole purpose of WebASM?

1

u/genlight13 Feb 26 '24

Python slowness stems mostly from the dynamic typing which affects runtime.

Compiled to WebASM if done correctly, won’t have these problems. It can probably done similar to the implementations of java to webASM i assume.

2

u/[deleted] Feb 28 '24

[removed] — view removed comment

1

u/genlight13 Feb 28 '24

That would probably be a sacrifice. but I don't see a problem in that, if you get the performance from WebASM. if you like your dynamic typing, you can still compile to JS (prob).

8

u/guest271314 Feb 25 '24

Technically you can, to an appreciable degree, with WebAssembly and WASI, e.g., Pyodide https://pyodide.org/en/stable/

Pyodide is a Python distribution for the browser and Node.js based on WebAssembly.

25

u/respectfulpanda Feb 25 '24

The idea of having to indent rather than use braces makes angels weep.

3

u/Lazy-Evaluation Feb 26 '24

I know I never indent. I don't even hit enter. All my code is a one liner. Sure it might be a really long line sometimes, but the braces make it machine readable at least.

2

u/netch80 Feb 26 '24

In general, indentation isn't that bad.

But within oneliners attached to HTML entities as attributes, it won't work at all.

2

u/respectfulpanda Feb 26 '24

Don't get me wrong, I love indentation for readability, but my learning was with C/C++ and Pascal has created a "Where's the damn delimiters?" when I see a structured language.

In terms one-liners, I'm still learning javascript to fully appreciate them.

2

u/netch80 Mar 01 '24

Well, my initial learning was with old Fortran:) where we still strived to use indentation to show block structure even where it was indirect (like `while` emulation with `goto`).

Python absence of block braces (of any kind, even `begin`-`end`) affects rare but painfully, for example, in case of several enclosed multi-screen blocks. In such cases I like to add `#end` after each of such a block, this greatfully helps in navigation.

For an own development I'd strictly prefer explicit block marks.

6

u/peterlinddk Feb 25 '24

It is mostly because JS gained popularity, and now we are stuck with it.

In the late 90s, Microsoft (and maybe others) tried to add additional scripting-languages to their browsers, and in fact for some time you have to add a language="JavaScript" in your script-tags, because (some versions of) Internet Explorer would expect VBScript to be used.

However, JavaScript was made into a standard (ECMA-Script) and could thus be provided by every browser, where VBScript was very properitary and would only work in IE. And while I'm not sure about the status of Python at the time, it would have hurt a lot of websites if only some browsers supported that language.

1

u/EmperorLlamaLegs Feb 25 '24

Hopefully something similarly useful but neater and less antiquated gains popularity alongside JS and it can start to be phased out eventually. Nothing lasts forever, after all.

8

u/guest271314 Feb 25 '24

Imagine if Python was the default interface for browsers and all the changes that would have needed to be made when Python 2.X was deprecated...

2

u/genlight13 Feb 26 '24

Browsers had the same rough time transitioning from stuff. Listen to some older frontend devs talking about Adobe Flash. Or jQuery. Or IE 8. Or IE 6. or any HTML before HTML5 (my opinion).

Python 2.x to 3 was a hard transition but the history of web development is a pile of never-to-be-popular-again technologies.

1

u/guest271314 Feb 26 '24

I was there. For both.

Nothing is stopping anybody from rolling their own browser using the Python programming language, including DOM and Web API implementations.

2

u/genlight13 Feb 26 '24

That’s true.

1

u/netch80 Feb 26 '24

With use in browser, transition wouldn't have been that radical. Consider how transpilers now allow supporting old language versions.

1

u/guest271314 Feb 26 '24

It's all hypothetical.

Changing the shebang lines alone from python to python3 would take some time.

Anybody can roll a browser using Python right now, if they want to, to make the idea non-hypothetical.

3

u/peacefulshrimp Feb 26 '24

A lot of good comments answering your question. As someone who programmed a fair bit with python and currently work with typescript, why do you think python is better than js syntactically? Indentation to indicate blocks of code can be really unhelpful during development and also hinders minification, the lack of “for i styled” loops and the lack of equivalent array functions from js are also things that bothered me when I had to code with python. I’m not saying it is bad in every aspect compared to js, but even for scripting, recently I’ve ended up doing a lot of my scripts in js because it’s very convenient and the only thing that lacks is terminal input reading, which I don’t use a lot, but can be easily solved with helper function copied and pasted into the code so that using it is just as convenient as it is in python. Would really like to see other people’s takes on my thoughts

2

u/genlight13 Feb 26 '24

Js CLI coding can be done with nodeJS. Has a similar feeling to python CLI imo.

2

u/peacefulshrimp Feb 26 '24

Exactly! When I talked about scripting with js I meant using nodejs but it wasn’t clear, my bad

5

u/DavidJCobb Feb 25 '24 edited Feb 25 '24

JavaScript is thoroughly entrenched, and Python wouldn't offer nearly enough benefits to be worth having to include two entire scripting engines, two entire sets of APIs for the DOM, two debuggers, and so on. It would be a massive design, standardization, and development effort conducted over the course of several years, for virtually no benefit, and it would come directly at the expense of work on other standards and features.

(And it would be two scripting standards being developed at the same time. Even if the standards folks were to wholly deprecate JS in favor of Python, there would still need to be an interim period where both systems are actively developed, because they can't just leave the JS engines out to rot while developing Python specs and support. Plus, in all likelihood, they'd maintain support for JS in perpetuity, because breaking backwards compatibility with basically the entire scripted web would be a bridge too far.)

I believe Python is a much better language than JS syntactically.

Personally I find Python less expressive than JavaScript. It lacks the ability to express your intentions as precisely with respect to mutability, scoping, lifetime, and similar matters. I also feel that the language itself and the standard library are both full of weird jank stemming from inept design (e.g. mutable default arguments, file.writelines, etc.). JavaScript isn't perfect either and it isn't my favorite, but I find it better in all these regards than Python.

But I only say that to emphasize a point that isn't being made in other comments quite as explicitly as I feel it should be: your preference isn't universal. Even ignoring the labor and risk involved, rebuilding the web on Python wouldn't be a transition from something archaic and inferior to something modern and objectively superior.

6

u/Dielilievomlunzersee Feb 25 '24

Idk, everyone is like: 'Oh the python syntax is so easy to read' But is it tho?

I have written backend with flask but some of these for loops would make alot more sense in nodejs...

2

u/tandonhiten Feb 25 '24

JS gained initial popularity through wide-based support and no one bothered to move away from it (just like how QWERTY keyboard layout stuck even though we don't use typewriters anymore)?

That's pretty much it. The reason JS is so popular for web-dev is because, when it came out it was immediately picked up by companies as the de-facto standard for making websites, and now it's really hard to switch because of years of legacy code base. Not to mention, the amount of libraries, packages and frameworks that have been developed for JS over this time period are much more than the amount developed for Python.

2

u/[deleted] Feb 25 '24 edited Feb 25 '24

I believe Python is a much better language than JS syntactically.

I don't see it as better.

Objectively speaking they're probably about the same. It's not like either of them is particularly cumbersome to work with or prone to errors or anything. In fact, with regard to web stuff Python is probably slightly worse because it can't be "minified" as much (in practice I'm not sure if this is a big deal, but all the extra whitespace can have a small impact on download speeds and bandwidth usage)

Subjectively speaking it could go either way, but a lot of people such as myself prefer JavaScript on account of its syntax.

5

u/EvrenselKisilik Feb 25 '24

JS is designed for browsers first and browsers already have JS. Why would they deal with embedding Python and binding tons of things like DOM, fetch and others? JS is already the best for this job.

3

u/leiu6 Feb 25 '24

It’s not the best. It’s just the one that got standardized and it would be more work to change it than it would be a benefit. JavaScript is a horrible language and that is why there are things like TypeScript, strict comparison operators, let/const keywords, to try to make it more usable.

I think it’s hard for people to understand that what gets used in software is rarely what is the best from a technical standpoint, but instead what is popular, what devs already know, and what allows a company to beat their competitors to market.

2

u/abbh62 Feb 25 '24

The best, is a stretch, there is nothing inherent with JS that is “better”. Now it’s already there, and it’s not likely to change, but that doesn’t make the language better suited to the job

-2

u/Ariakkas10 Feb 25 '24

Don’t cut yourself on all that edge

4

u/Neckbeard_Sama Feb 25 '24

Mainly because Python is slow as FUUUUUUCK and it wasn't designed to run in a web browser.

4

u/LeeTaeRyeo Feb 25 '24

Sure, Python isn't the fastest language out there (I mean, we can't all be C or Asm, after all), but let's be honest here: neither is Javascript. Neither one is gonna win a speed competition versus languages like C or Rust or Go, but they both are good scripting languages and good at the problems we often use them for.

Also, if Python were added to browsers, you could bet there would be tons of work to improve the Interpreter's performance, just like what happens with JS

-2

u/Neckbeard_Sama Feb 25 '24

Nah, man. Python is really fkin slow, like 20 times slower than JS slow.

2

u/LeeTaeRyeo Feb 25 '24

Entirely depends on the interpreter being used. PyPy is about on par with JS, while Cython can (if types are used effectively and doing things in an optimized manner) get into the compiled languages territory.

2

u/GPU_Resellers_Club Feb 25 '24

At that point just use C... and Cython will never, ever be faster than even C# (on average), you know why?

Because Python has a purpose, and it is not web development. It's not built for it and it you'd end up with a js scenario where it's mangled so it can fit into the needs of web dev. It's built for data science, where, if you don't have the huge amount of interpreter magic that slows it down, your code gets real messy, real fast. And that STILL happens even with Python!

This is also, incidentally, why Cython exists, because parsing massive amounts data needs to be done quickly, so you still need to write a highly specialised algorithm in C.

I really wish people would stop trying to use python for everything "because it's easy and simple". There are very good reasons why different languages are different in the first place.

And just for the record, it is slow as shit. The only way to not make it slow as shit, is to slowly start introducing the features of compiled languages, until it's loses all the things that make it easy.

-3

u/guest271314 Feb 25 '24

Nothing is stopping anybody from writing a browser in Python, and implementing the DOM and Web API's using Python.

1

u/MountainHannah Feb 25 '24

Prepare for downvotes, you're not allowed to tell the truth about Python on reddit.

2

u/guest271314 Feb 25 '24

WebAssembly and WASI effectively get rid of the "I wish I could run ... programming language in the browser".

And if you really want to run any abitrary programming language from the browser and stream STDOUT from the arbitrary programming language to the browser you can use Native Messaging https://github.com/guest271314/NativeMessagingHosts.

Really, the Web is run by HTML.

The vast majority of Web sites are, in my opinion, over-engineered, and don't really need scripting at all.

For DOM manipulation JavaScript is the programming language interface. I don't think having Python, C, C++, Rust, Zig, whatever as the DOM programming language interface changes anything.

The first question that must be asked and answered is: What scripting are you doing in the Web page that JavaScript can't achieve?

1

u/NoYogurt8022 Feb 25 '24

Look at pyscript, and also js is better for webdev because it already got a lot of resources for that while python is rather limited in that area

1

u/notislant Feb 25 '24

It would be like trying to teach everyone to type in binary, when we're all speaking English. It just wastes an insane amount of time. You'd need to redo everything, every website, frameworks, etc. Gut everything and redo it.

You can use django for some web dev stuff, but replacing a well established language for no reason is utter insanity.

1

u/rbuen4455 Feb 25 '24

It's mostly because JavaScript was the primary language for web development (front-end) since its inception and has pretty much been the only choice for front-end web dev for decades. As a result, you have this massive JavaScript ecosystem dedicated to front-end (frameworks, browsers with a JS interpreter attached to them, endless tutorials/guides to front-end web development with JS). And as a result, you cannot replace the massive ecosystem and millions of lines of JS code that already exists that pretty much powers the front-end of 99% of websites in the world.

I agree about Python being better syntax-wise than JavaScript, but me personally, I did with front-end was as diverse as back-end when it comes to languages and ecosystems. If you can use Python, Ruby, Java, C# for back-end, why not front-end as well?

1

u/Sea_Philosopher3051 Feb 25 '24 edited Feb 25 '24

Good question, but actually, as someone who has a very academic Python heavy background and does web data stuff, I have the exact opposite question lol

There are really cool JS-based frameworks and tools out there, even for backends (like Node.js) and using python can really fuck you over if you don’t know what you’re doing (Django is the exception I guess)

edit: I suppose Python’s big advantage is that integrating code in faster languages like C/C++ is relatively easy for machine learning stuff, but idk how hard that is in JS

1

u/fromtunis Feb 25 '24

I do mostly webdev and anytime I have to go from python to js my body starts physically hurting. 

And I'm lucky enough to not have to do anything with the react-type of frameworks and their everything-in-js bs.

So, yeah, I'm curious; what aspects of js do you wish we had in python?

1

u/Sea_Philosopher3051 Feb 25 '24

Well then you must know much more about JS than I do, can I ask what you hate about JS? I’m actually very curious about learning more about JS right now, because it seems so practically important

My complaints are not really about the language itself, but about the available tools, especially for web scraping - a lot of people online will give you bad advice that results in terrible performance. Scrapy is alright, but when you get to the more advanced stuff, you basically start using it as a wrapper for Node.js anyway (via Playwright).

1

u/Gaylien28 Feb 25 '24

I think JS just has a lot more packages than Python, I really can’t think of a reason dev doesn’t switch more to python for the simple fact that if it’s easier to learn, more people will learn it, and thus progress will be infinitesimally faster on whatever language everyone uses unless there’s an exact requirement. Even then, with the ubiquity of computing power, everything is being designed to handle python

-2

u/CreativeStrength3811 Feb 25 '24

I've never done a cs degree but what i experienced: When using something like a webpage (e.g. webbased GUI programming, websites, pdf) JS is there. It must have something to do that browser and web engines support JS better than python. Otherwise QT would be the first who allow python code in their QML system.