Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How different are the semantics between Python and JavaScript?

Both these languages seem extremely similar to me. Although Python supports actual classes instead of being prototype-based, in Python classes are not all that different from functions that generate objects containing values and functions, just as you'd do in JavaScript. On the other hand, JavaScript only supports floating-point numbers and strings as built-in data types.

These seem like fairly shallow differences to me, so these things aside, what are some more important differences between them?

like image 521
Steve Avatar asked Nov 23 '09 22:11

Steve


People also ask

How different is Python and JavaScript?

Python makes it simple to read and maintain code. Because of its flexibility, Javascript does not provide easy code readability or maintainability. To run Python code, you'll almost always need an interpreter. The ability to run Javascript code is built-in to most web browsers.

Is JavaScript syntax similar to Python?

Python's "object-based" subset is roughly equivalent to JavaScript. Like JavaScript (and unlike Java), Python supports a programming style that uses simple functions and variables without engaging in class definitions.

What is Python semantics?

Python uses dynamic semantics, meaning that its variables are dynamic objects. Essentially, it's just another aspect of Python being a high-level language. In the list example above, a low-level language like C requires you to statically define the type of a variable.

Are Python and JavaScript used together?

In fact, the two languages complement each other. Javascript is used as a client-side scripting language, whereas Python is mostly used as a server-side scripting language.


1 Answers

  1. Classical inheritance in Python, Prototypal inheritance in ECMAScript
  2. ECMAScript is a braces and semicolons language while Python is white-space and indent/block based
  3. No var keyword in Python, implicit globals in ECMAScript, both are lexically scoped
  4. Closures in Python 2.5 and lower ( re: Alex Martelli's comment ) are somewhat "limited" because the bindings are read-only, you can't access private variables like you could in ECMAScript
  5. There's no undefined in Python, exceptions are thrown
  6. Immutable list arrays in Python ( tuples )
  7. No switch statement in Python but instead you're encouraged to use a dictionary in that manner, sometimes its convenient assigning properties to lambdas and executing them
  8. ECMAScript 3 does not have a yield statement, nor let expressions/statements, nor array comprehensions - however these are included in Mozilla's JS which is non-standard
  9. raise vs throw, except vs catch ( Python, JS )
  10. Native Unicode strings in ECMAScript
  11. keyword operators such as and, is, and not are used in Python
  12. Python doesn't support counters such as i++
  13. Python's for loop is "smart" so you don't need to use a counter for enumerating through lists, nor do you run into prototypal properties inherited from Object.prototype
  14. You don't have to use the new operator in Python to create objects
  15. Python is duck-typed

I stole a good bit of info from http://hg.toolness.com/python-for-js-programmers/raw-file/tip/PythonForJsProgrammers.html

like image 53
meder omuraliev Avatar answered Sep 23 '22 20:09

meder omuraliev