views:

250

answers:

6

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?

+1  A: 

In Python, whitespace is part of the language. In Javascript, braces define code blocks and spaces are ignored. Furthermore, Python has bindings for the Java API, .net, and other cool fancy libraries. Javascript is pretty limited in the library department when compared to Python, but it has some neat windowing libraries and such.

Stefan Kendall
Javascript has java bindings and .net bindings too. Just depends on what interpreter you're using. You can even call directly into JAVA and make swing apps if you're javascripting in firefox. Don't even need any special privilages unless you wanna do something weird like file i/o.
Breton
:P We're almost always talking about "IE6" when JavaScript is concerned.
Stefan Kendall
Really? who is "we" exactly? What a strange thing to say. In any case, if you wanna do it in IE6 you can use chrome frame, or Jscript.NET + silverlight
Breton
We, as in the majority of JavaScript users who have to support a wildly differing userbase and some users are on locked-down systems where chrome frame and silverlight just aren't possible.Anyone with business concerns almost always writes for the greatest common denominator, and with JavaScript, that goes pretty low. Such a GCD doesn't generally happen in Python.
Stefan Kendall
+2  A: 

Being a JavaScript developer and done some Python stuff (thanks to Google App Engine) I would say that the two major differences between JavaScript and Python would be

  • Formatting. JavaScript doesn't care about the looks of your code (think of all the code minimizers and what the resulting looks like)

  • Unicode support. JavaScript is all the way unicode, GAE's Python 2.5 not so much (having Latin 1 as the default character set). So having the need to support non-latin characters can be a real PITA if your'e not sure what you are doing.

Andris
Have you seen Python's `unicode` type yet?
Roger Pate
Sure I have but the thing with python 2.5 unicode is that you need to be sure what you are doing - you need to know the basics. Hopping from another language just to do one thing quickly may end with disaster. For example it took me hours with this linehttp://code.google.com/p/turbinecms/source/browse/trunk/main.py#584to get rid of the "'ascii' codec can't encode character %c3" errors
Andris
+10  A: 
  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

meder
For #8, actually Mozilla JavaScript supports _every_ one of those features.
Eli Grey
Yeah I'm aware of that but I was rapidly making edits and just decided to say `some` incase I accidently added a Python-specific feature... I'll update.
meder
4. is obsolete in Py2.6 (`nonlocal` keyword).
Alex Martelli
Python has native unicode strings and incrementing (just spelled "+= 1" instead of "++"). You can use duck-typing in JS as well.
Roger Pate
+2  A: 

Typing: Javascript and Python are both dynamically typed, whereas javascript is weakly, python strongly typed.

The MYYN
+3  A: 

In python, "self" is explicitly passed to a member function, and is not a special keyword or anything. In javascript, "this" is dynamically scoped. you can fiddle with the scope of a member function by calling apply() on it.

Jimmy
+2  A: 

I'll add a few I haven't seen mentioned yet:

  • JavaScript supports object-literal notation. Python doesn't exactly work the same way, but Python dictionaries are similar to JavaScript associative arrays.
  • JavaScript objects/arrays support that cool feature where you don't need to quote (single-word) strings when creating new objects:

    var foo = { bar: "baz" };

  • Accessing associative array keys in JavaScript can be done using dot notation, in addition to brace notation. That is, these are the same:

    foo.bar; //returns "baz"

    foo["bar"]; // returns "baz"

  • Python's anonymous function (lambda) syntax is not as flexible as JavaScript's anonymous functions.

  • Python has, like, a standard library and stuff. (And yes, I know about Rhino et al., but the libraries they give you are not standard. There's no standardized way to read a file in JavaScript... that I know of.)
  • You can run JavaScript in a browser. Python... not so much. ;)
Jeff