3 ms·
Looks like the FAQ covers this question: https://brython.info/static_doc/en/faq.html https://brython.info/static_doc/en/faq.html Quote: Q: why use the operat
by wenc 6y ago
Looks like the FAQ covers this question:
https://brython.info/static_doc/en/faq.html 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
- corytheboyd 6y agoNot at all a Python user, but I don’t get how this is a valid argument at all: > 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
- dragonwriter 6y ago> What’s wrong with an API like appendChild etc? 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.
- bruxis 6y ago+1, would have much rather seen a function-based interface for DOM manipulation than this monstrosity.
- linkdd 6y agoI'll try to explain why I strongly disagree with the FAQ: > 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/ https://www.python.org/dev/peps/pep-0292/ ) tries to replace % as an operator, because it should not be used for string formatting.
- divbzero 6y agoI agree with GP on this. 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.stderr