https://brython.info/static_doc/en/faq.html
Quote:
Q: why use the operator <= to build the tree of DOM elements? This is not pythonic!
A: Python has no built-in structure to manipulate trees, ie to add "child" or "sibling" nodes to a tree node. For these operations, functions can be used; the syntax proposed by Brython is to use operators: this is easier to type (no parenthesis) and more readable
To add a sibling node, the operator + is used.
To add a child, the operator <= was chosen for these reasons:
* it has the shape of a left arrow; note that Python function annotations use a new operator -> that was chosen for its arrow shape
* it looks like an augmented assignment because of the equal sign
* it can't be confused with "lesser or equal" because a line with document <= elt would be a no-op if it was "lesser or equal", which is always used in a condition or as the return value of a function
* we are so used to interpret the 2 signs < and = as "lesser or equal" that we forget that they are a convention for programming languages, to replace the real sign ≤
* in Python, <= is used as an operator for sets with a different meaning than "lesser or equal"
* the sign < is often used in computer science to mean something else than "lesser than": in Python and many other languages, << means left shift; in HTML tags are enclosed with < and >
* Python uses the same operator % for very different operations: modulo and string formatting
> this is easier to type (no parenthesis) and more readable
When 99% of the open-source codebase uses "<=" as lesser-than-equal, no it is not more readable to change that.
> It has the shape of a left arrow
Then, why not `child >> tree` ? or `tree << child`
> it looks like an augmented assignment because of the equal sign
No, it looks like a comparison. This is totally subjective.
> it can't be confused with "lesser or equal" because [...] would be a no-op
Wrong, the value of _ variable contains the result of the last expression. What value can i expect here?
> we are so used to interpret the 2 signs < and = as "lesser or equal" that we forget that they are a convention for programming languages
I agree, and trying to be subversive won't change that convention.
> in Python, <= is used as an operator for sets with a different meaning
The implementation is different, but the math are the same. The "<", "<=", "==", "!=", ">=", ">" operators are comparison operators.
"setA < setB" has the same meaning as "numberA < numberB", aka: a comparison between two object in an ordered space.
> the sign < is often used in computer science to mean something else than "lesser than", [...], << means left shift; in HTML tags are enclosed with < and >
You are talking about a character not an operator.
> Python uses the same operator % for very different operations: modulo and string formatting
This PEP ( https://www.python.org/dev/peps/pep-0292/ ) tries to replace % as an operator, because it should not be used for string formatting.
> Python has no built-in structure to manipulate trees
What’s wrong with an API like appendChild etc? I know you’re just quoting the FAQ but if anyone has an answer I am legitimately curious. I have worked with Ruby for many years and loath cute DSLs but I always thought it was just the Ruby community that went overboard on that shit haha
Verbosity vs. fluency, given the application domain, I expect is the concern. I'd rather have fluent DSL separate from the basic language runtime, though, so that adopting it is a separate decision from using the runtime.
Alternatives to <= that could be more Pythonic:
document.append(node)
node >> document
which parallel # list append
list().append(item)
# print to stream in Python 2
print "example" >> sys.stderrApache Beam is one of the worst offenders to me. The recommended method is to connect your functional transforms when building your pipeline as if it were a series Bash commands piped together (overloading bitwise OR). Who thought “oh, you know what language and API is synonymous with ‘write a large complicated amount of business logic in me’, Bash.”
The bitwise operation dunder methods have no definition outside of integers. So they’re basically freebies to do what you want with.
Honestly I think the Apache Beam API makes a lot of sense.
I just think the Java API's version...
pipeline = pipeline
.apply("FirstTransform, MyFirstParDo())
.apply("SecondTransform, MySecondParDo())
is much clearer than the Python version... pipeline = (
pipeline
| "FirstTransform" >> MyFirstParDo()
| "SecondTransform" >> MySecondParDo()
)Like, for example, if you have tasks A, B, C, and D you can do...
A >> (B, C) >> D
but not... (B, C) >> A >> D
you have to write... D << A << (B, C)It may not be "pythonic", but there's an established tradition of operator methods and there's no requirement that python code conform to any particular tradition.
https://stackoverflow.com/questions/52389105/how-operator-de...
When it’s used that way, you’ve lost that the operation is semantically analogous to __le__. I think the assumption it has no side effects goes, too.
Edit: not saying I like it, though.
Unless you implement __ge__, `'Hello!' >= document` will also call __le__, which may or may not be what you want. And `'Hello!' >= document <= 10` is equivalent to `'Hello!' >= document and document <= 10`, which can lead to confusing surprises.
(Brython does implement all of those pitfalls faithfully)
So, this is just another implementation with its tradeoffs, just like when using Jython to have Python (without its ecosystem) and the JVM. Here you have Python with another semantic to learn.
>>> document <= 'Hello!' # write in the DOM
Since when the __le__ method has any side effects?
So to sum it up:
- it runs inside the Javascript VM
- you change the syntax by breaking the semantics
- the import mechanisms is completely different
Is it still Python then ?
NB: Still, it's a fun project and hope you had fun making it.