> So now we can write web apps in one of most inappropriate languages for frontend development.
Implying that javascript was ever appropriate.
> White space significant, good luck minifying it.
Just gzip it. Minifying is highly overrated to begin with.
> No anonymous functions, yes you need to have name for that callback, even though it's simply disabling a button.
Lambdas are supported, what do you mean?
> Webpack does a great job of handling assets by simply importing/require them in the code. Python import system, can't support that kind of importing.
Can we please stop doing the 4chan-esque snarky >implying ? It's not discussion in good faith.
> Minifying is highly overrated to begin with
Compression is not magic and minifying has real world performance gains in JS projects at least - I don't see how Python would be different here.
> Lambdas are supported
If you want to write your event handlers - which in UI programming is a lot of your code - in a point-free style, then I think you'll be letting yourself in for a lot of hassle.
> Webpack... What does this even mean?
I don't know Python too well, but I assume this is about the ability of Webpack to overload import semantics with non-code dependencies (e.g. importing stylesheets, icons, bits of JSON). Presumably Brython does not support this.
You seem quite bullish about the strengths of this project. Exactly what problems do you feel it solves? Assuming that we already have tooling like Webpack and are able to write in either ES2020 or TypeScript, what do you think we gain from writing our UI code in Python?
>Compression is not magic and minifying has real world performance gains in JS projects at least - I don't see how Python would be different here.
How much are the gains with minifying (factoring out compression) anyways? I'm sure they're somewhat faster, but enough to justify the step/obfuscation (assuming obfuscation isn't a goal)
If a function is so long it requires multiple statements, is it really so bad to give it a name and a separate line of code from its use? Personally I'd find that clearer, even if the language didn't enforce it.
Edit: Just in case anyone isn't aware, in Python, nested named functions have precisely the same capture rules as lambdas, so it's never a problem to replace a Python lambda with a normal function (except personal preference about code style). This is particularly in contrast to JavaScript where arrow functions are so popular partly because of their different relationship with `this` to other functions. And normal nested functions defined with `def foo():` have a benefit: they have their name attached to the object, so a traceback involving it will be clearer, which isn't true for `foo = lambda ...` or `bar(fn=lambda...)`.
In practice? Yeah. It sucks. In Javascript, I'd simply add curly braces and be done with it.
Code is meant to be clear. And often times with Python lambdas, there's no reason to give the function a name. It's a callback. What would you name it? "Callback"? You're adding extra complexity for no gain.
But, whatever. It is what it is. Javascript has its own pile of limitations. I was just saying that Python isn't a flawless butterfly.
Yes just call it "callback" if necessary. In the situations I'm imagining, that would still make the code clearer than using one statement to cram in both the definition of the multi-statement function and the use of it in another function.
But maybe we're imagining different situations, and maybe that's the real reason we don't share the same point of view.
100% this. When I write Javascript or Scala, I do this anyway just because it’s a better way to structure code.
Nested anonymous functions that contain complex bodies and rely on scope closure of args or local variables inside the complex bodies of exterior anonymous functions needs to stop being a thing, in any language, anywhere. It’s a massive antipattern.
Same for using functional programming constructs like map or filter or reduce with complex multi-statement nested anonymous functions. “Inlining” your anonymous function when it’s not just a simple statement is simply a terrible idea across the board.
It’s like living in crazy town to see that style defended as reasonable or treated like it’s valuable for a use case, etc.
Functional programming constructs helps you write simple code faster, and clarify the algorithm.
It is easier to rearrange, to test and to reason about, but it is harder to read.
Once your fast-written code works, you obviously need to optimize it where it's needed. And if you map/filter/reduce is your bottleneck, just get rid of it obviously.
But I highly doubt it will be your bottleneck.
Nested anonymous function that rely on scope closure is THE way to truly encapsulate your data, example: https://pastebin.com/JrAP6yGP
IMHO, the factory pattern is a clearer/easier pattern than Javascript's classes/prototypes.
I've been writing Python professionally for a while now, and the one thing I always want to reach for are multi-line anonymous functions. However, I do appreciate how Python prevents some of the callback hell one can encounter in JavaScript codebases.
There is a difference between abusing anonymous functions, and situations where anonymous functions might be called for, where it just so happens that those functions require more than one line of code.
>If a function is so long it requires multiple statements, is it really so bad to give it a name and a separate line of code from its use? Personally I'd find that clearer, even if the language didn't enforce it.
Sometimes this is the case. But let's take for example filtering an array. Do you really want to give names for the callback that will filter? Also I find it more readable if the logic doing the filtering is right there where the filter call is, and not somewhere above.
It sort of is a pain point because you need to move the code out of wherever the lambda was into its own block elsewhere. Breaks up the flow and is awkward. I'd love to see some kind of block lambda syntax in Python. The one drawback of whitespace syntax...
"Whitespace syntax" is not the problem. It is that Guido thinks that the anonymous callback style is harder to read and therefore something to avoid most of the time. I tend to agree.
It's the problem inasmuch as the way you'd spell an anonymous block is non-obvious considering that it would have to work as an argument to a function or element of a literal and in both of those cases indentation is freeform.
The only thing with 'just gzip it' is that actually minification+gzip does seem to give slightly better results than just gzipping it. This is partly going to be because there are some things that gzip doesn't know it can remove that a minifier does - e.g. comments. Presumably this difference is exacerbated if your codebase uses long/descriptive names and many long comments. Not that this is a particular problem for Python on the web of course because minifiers do exist for it.
Lambdas are not functions. You can't have statements in them, only expressions. So no, they don't cut it as anonymous functions.
>> Webpack does a great job of handling assets by simply importing/require them in the code. Python import system, can't support that kind of importing.
>What does this even mean?
It means that in JS you can do "require('./image.png')" and webpack will automatically handle the image for you. Python doesn't have any way to import anything that is not .py file.
This is some pretty intense negativity. What's wrong with doing something just for the hell of it? People will choose naturally whichever may be the most efficient methods.
There is no reason to shit on someone's work just because you think it was a bad idea. It's already done, and it can coexist in the universe with you.
> There is no reason to shit on someone's work just because you think it was a bad idea. It's already done, and it can coexist in the universe with you.
If zaro is wrong (and people pounce on him for being technically wrong), Brython will indirectly be vindicated. If he's right, it's the perfect opportunity for break down just how right he is; e.x, if future client-side languages will be shaped by their suitability for minification or if content compression obviates this.
I think even for new projects, this is good. Nobody arrogates themselves to the belief their project will be as influential as the most well known projects started just for fun (n.b "Just For Fun, Torvalds (2002)") and no critical commenter should pretend they are with replies that hide hostility behind concern for our future... but by the time news of it's development has reached us here, the sliver of chance that Brython will join their ranks is no longer microscopic. I'm reminded of early CoffeeScript (and even Javascript itself), which could have avoided numerous pitfalls with early constructive(!) criticism from cantankerous commentators.
No matter what, the armchair quarterbacks will be taken down a notch if they're wrong and are worth your time otherwise. Those early complaints help decide if your future is filled with a junkyard of half-baked software or manages to harness our collective wisdom (with a healthy dose of individual discretion) to refine itself into something elegant and powerful.
It's not that hard to see Brython doing just that either; you don't need to be Nostradamus to foretell a prophecy of an easy-to-use, "just works" implementation of Python3 in the browser profoundly altering the current landscape, shaping our future toolbox and even what software is thought possible.
He could have presented less snarky, but the comment is appropriate and helpful. The comments are the low effort way to get a feel for the technical viability of the tech featured in the main post.
You know, this idea that removing whitespace is the best way to reduce code size for delivery can be debunked by any student that takes a compiler class. That technique + gzip is a shortcut that we have taken as a defacto “right way” of delivering web code (of course taking out the whole conversation of WebAssembly). Perhaps something good can come up out of this - just like people pissed all over the idea of js on the server when node.js came out, perhaps this has the potential of doing the reverse on the client side. Stay open minded.
I'm not sure if any of the points you raise would be a deal breaker when set against the potential advantages for some people.
For example: "I have code that already exists in Python that I want to reuse in a browser" or "I am fluent and very productive in Python and I just need to get shit done".
I don't think anyone expects this to replace javascripts for normal public-facing web development. Do they?
> White space significant, good luck minifying it.
While I cant vouche for this implementation if you want to minify Python you compile it to bytecode and run that. Works wonders for embeded deployments of Python.
Noone asks you to use Brython. People familiar with Python might prefer writing their frontend code in Python just because they are already familiar with it.
The following argument isn't necessary for folks who do want Python in frontend but still, not all of your points hold true, Python can handle multiple commands in a single line and it does have anonymous functions: lambda.
I would differentiate between marketing speak (designed to replace Javascript) and the practical implications of Brython (designed for folks who want to use Python in frontend)
If I were a JS developer who only wanted to write JS, but I needed to do a database query, should I look for a database that lets me write queries in JavaScript (or a language basically interchangeable with JS)? Or should I just learn SQL?
Sometimes as a programmer you need to learn new tools for new domains, and that's not necessarily a bad thing.
Too often I've seen companies stuck with an old framework just because their developers are used to it. That's how you get a microservice architecture in Java where each containers takes 300MB of space, and requires 1GB of memory.
I'm frankly amazed that the people who brought us node.js haven't also created a JS query language, furthering the spread of the triple-equals to new and greater heights.
Believe it or not most server-side developers find javascript ecosystem an awful experience so I doubt they care about Webpack or any of the JS "innovations". The speed is not really an issue given that most of the client apps just call DOM/Web API functions.
Yea, there always seems to be a disconnect in these conversations between people who aren't yet used to Javascript's grotesqueness and those who are desensitized to it. Javascript was so horrible for so long, and remained horrible every time I tried it because "seriously, it's good now", that there's a fairly significant threshold to get me interested in using it when any other alternative is available.
Naturally, if I was making a decision for a company I worked at, I would do my due diligence and likely end up using JS for any of a number of reasons that don't have to do with how unpleasant and unproductive working in the language is. But for personal projects, I'd happily use something like this, short of discovering a show-stopping problem with it.
The thread OP comment just seems like classic defensiveness about JS; a project like this is never going to be literally perfect for corporate production use on day one or but it doesn't need to be to be useful. Even on the off chance that it were to grow into something widely useful, at that point you'd see a lot of powerful actors polishing off the rough edges. This is exactly what happened to Javascript: the only reason it's defensible at all is because of the millions of dollars poured into it and its ecosystem by companies like Google, Mozilla, etc
This is exactly what BeeWare's Batavia does: compile to bytecode server-side, then run a remarkably small interpreter in the browser. This is a substantial win: parsing is a CPU hog, and Brython's architecture does it every time the page loads.
That said, in order to support `eval` and friends, you still need a full-fat compiler in the browser. So the best route is probably to use a full-service Python->JS compiler, but compile most of the code ahead of time. That's where we're heading with Anvil (we use https://skulpt.org for the client side, but it's the same principle).
My colleague wrote up a pretty wide-ranging comparison of browser-based Python interpreters. It describes how (and why) Brython, Batavia, Skulpt, Transcrypt, and Pyodide differ, and the tradeoffs they're chasing:
In twenty years of professional Python development, I've never used eval once. I'm not saying nobody ever needs it, but it's a niche feature that the vast majority of programs can do without, and is probably not worth the tradeoff.
Even there it's not strictly necessary. You could accomplish the same thing with a variadic function that does its own argument parsing, it'd just be slower and not play as well with reflection. So you could port it to a Python implementation without eval().
It's necessary if you want to offer an interactive Python shell in the browser, e.g. for websites that teach programming or otherwise use programming as a means of user interaction.
You probably have used a Python REPL though? Either on commandline or in iPython shell or similar. This relies on eval type functionality somewhere in the chain.
Would it be hard to define an alternate representation of Python code that uses curly braces instead of white space to use over the wire for delivery of front-end Python (with curly braces that occur in the original Python code escaped)?
This code, for example:
def func(foo, bar):
if foo < bar:
foo += bar
return foo * 3
That could then be subject to the usual minimization approaches.
Going back from curly representation to significant white space is easy and fast, although you could probably build your tokenizer so that it never even has to bother. That could make the client side handling of this essentially free.
Both CPython's parser and Python language grammar works in terms of indents/dedents being represented by brace-like tokens, so this is trivially possible transformation.
> White space significant, good luck minifying it.
Brython supports precompilation to JS, which is obviously minifiable.
> on top of that you are running Python interpreter in the JS VM.
No, you aren't. Brython is not a Python interpreter, but a compiler to JS. There are optimization issues from the need to maintain Python semantics interfering with optimizing the JS, but that's not the same as running a Python interpreter.
Node shows that this is false. You live in a 2002 bubble if you still think anyone would choose any language over Javascript any time. Python to me only ranks above PHP and I wouldn't use it at all if it weren't for its math/ML subcommunities.
first of all, I don't think "most inappropriate languages for frontend development" is a thing, it is just a new way to do web dev that is different to what you used to be doing with. If you don't think it is an approach that you prefer/can accept, you can just move on continue with whatever you like. You should learn from the people who built Brython, they don't like the way how the current web is being built - hence they continue doing what they believe its right, BUT wihtout putting shit on other's people's work.
There are people who think images and JS are far too much for the Web (you can often read comments like that on HN) and want pure text only Web. Should we learn from them too?
I don't mean to put shit on any body's work. I am just noting several obvious flaws to the idea. Now it might sound like putting shit, I don't know, but I am not native speaker so sometimes I guess I am using the wrong tone/working.
The real question for me is why it's such a big deal? In the end its simply an opinion.
White space significant, good luck minifying it.
No anonymous functions, yes you need to have name for that callback, even though it's simply disabling a button.
Webpack does a great job of handling assets by simply importing/require them in the code. Python import system, can't support that kind of importing.
And of course there is the performance. Python is generally slower than JS, and then on top of that you are running Python interpreter in the JS VM.