[Python-ideas] Towards harmony with JavaScript?

Chris Angelico rosuav at gmail.com
Fri Aug 11 11:15:46 EDT 2017


On Sat, Aug 12, 2017 at 12:57 AM, Jason H <jhihn at gmx.com> wrote:
> Before I done my firesuit, I'd like to say that I much prefer python and I rail on JS whenever I can. However these days it is quite common to be doing work in both Python and Javascript. Harmonizing the two would help JS developers pick up the language as well as people like me that are stuck working in JS as well.
>
> TIOBE has Python at 5 and JS at 8 https://www.tiobe.com/tiobe-index/
> Redmonk: 1 and 1, respectively http://redmonk.com/sogrady/2017/06/08/language-rankings-6-17/
> PYPL: 2 and 5 respectively http://pypl.github.io/PYPL.html
>
> While JS is strongly for web (Node.JS, Browsers) and Python has a weak showing (Tornado, Flask), Python is very popular on everything else on the backend where JS isn't and isn't likely to be.  The I'm making point is not to choose a 'winner', but to make the observation that: given that the tight clustering of the two languages there will be considerable overlap. People like me are asked to do both quite frequently. So I'd like a little more harmony to aid in my day-to-day. I have just as many python files as JS files open in my editor at this moment.
>

Python has a number of strong web frameworks - Django is probably the
best known.

> There are several annoyances that if removed, would go a long way.
> 1. Object literals: JS: {a:1} vs Python: {'a':1}
>    Making my fingers dance on ' or " is not a good use of keystrokes, and it decreases readability. However a counter argument here is what about when the a is a variable? JS allows o[a] as a way to assigned to a property that is a variable. Python of course offers functions that do this, but for simple objects, this would very much be appreciated.
>    The point here is this is
>

Disagreed. Python is both more consistent and more flexible than JS
here. More flexible in that dict keys can be any hashable type, where
JS object properties are always strings; and more consistent in that a
value is always represented the same way. Consider literals and
variables as dict keys in Python:

# Literal
d = {'a': 1}
print(d['a'])
d['a'] = 2
# Variable
key = 'a'
d = {key: 1}
print(d[key])
d[key] = 2

Contrast JS:

// Literal
d = {a: 1}
console.log(d.a)
d.a = 2
// Variable
key = 'a'
d = {[key]: 1}
console.log(d[key])
d[key] = 2

In Python, a literal string is always in quotes, and an unquoted
symbol is always a name lookup. In JS, you can use the shorthand of
dot notation for literals that are valid symbols, but to use a
variable, you need to switch syntax. (To be fair, this is simply
adding a shorthand that Python doesn't have; you could use square
brackets and string literals in JS too. But people don't do that, so a
programmer has to know to read it using dot notation primarily.)

Coupled with the increased flexibility in what you can have in a dict
key, Python's requirement to quote keys is a small price to pay for
consistency.

> 2. Join: JS: [].join(s) vs Python: s.join([])
>    I've read the justification for putting join on a string, and it makes sense. But I think we should put it on the list too.

This might be safe to add; but it needs to be well worth adding, since
it's just a different spelling for the exact same thing. -0.

> 3. Allow C/C++/JS style comments: JS:[ //, /* ] vs Python #
>    This one is pretty self-explanatory.

If you'd asked for this a few years ago, maybe, but since // is a
division operator, that part of it won't fly. Possibly /* comments */
could be added though.

That's about all that I'd support adding, though.

ChrisA


More information about the Python-ideas mailing list