classColorPoint(Point):def__init__(self,x,y,color):super().__init__(x,y)self.color=colordef__str__(self):return"{} in color {}".format(super().__str__(),self.color)
ES2017
classColorPointextendsPoint{constructor(x,y,color){super(x,y)this.color=color}toString(){return`${super.toString()} in ${this.color}`}}
:( Kinda gutted that JS extends syntax looked a lot cleaner.
I have to fight to not use braces if I switch between Python & JS, even though they waste space and the language doesn't enforce (eslint is a tool, it's not language enforced like python).
I find it unlikely that JS's poor organization will ever reach a level where they could be considered Pythonic, even though they do make (often sloppy) copies of features from (among other things) Python.
To add to the Getter & Setter examples: You can use this in Python instead of self. It is just a convention, that people refer to the current instance as self. Also, instead of sqrt in both languages, you can write **0.5. With both modifications, that codes look even more similar.
About the classes example: They might look similar, but they are not. __init__ is not a constructor. __init__ is an initializer. The constructor in Python is called __new__. Also, usually you override __repr__ to get a representation of the object instead of __str__.
Python expert | Serial FOSS maintainer | Author of https://github.com/marketplace/actions/alls-green | Creator of https://octomachinery.dev | Ansible Core Dev | Maintainer of aiohttp, CherryPy
Location
Mikulov, CZ π¨πΏπͺπΊ β Brovary/Kyiv, UA πΊπ¦π
Education
Does it matter?
Work
Principal Software Engineer @ Ansible Core Engineering team at Red Hat
Latest comments (49)
It looks more like C#. if you consider Typescript instead of JavaScript is uncanny similar
:( Kinda gutted that JS extends syntax looked a lot cleaner.
I have to fight to not use braces if I switch between Python & JS, even though they waste space and the language doesn't enforce (eslint is a tool, it's not language enforced like python).
Here's my tweet response twitter.com/LewisCowles1/status/10...
It takes more than a few specific capabilities to be "Pythonic".
PEP-20 for example is pretty core to Python: python.org/dev/peps/pep-0020/
I find it unlikely that JS's poor organization will ever reach a level where they could be considered Pythonic, even though they do make (often sloppy) copies of features from (among other things) Python.
Wow didn't know JavaScript became coolπ
Great that's what I just needed ;)
Just waiting for ES202? == Python, you might as well...
I was worried that the variable declaration of the sample code is
var
though using ES2017.Very interesting comparison, thanks for the nice clear examples.. :)
To add to the Getter & Setter examples: You can use
this
in Python instead ofself
. It is just a convention, that people refer to the current instance asself
. Also, instead ofsqrt
in both languages, you can write**0.5
. With both modifications, that codes look even more similar.About the classes example: They might look similar, but they are not.
__init__
is not a constructor.__init__
is an initializer. The constructor in Python is called__new__
. Also, usually you override__repr__
to get a representation of the object instead of__str__
.Yeah, I've noticed this some time ago.
Don't forget that you also can omit semicolons in JS as well :)