Jython Phone Interview Advice

Jeremy Bowers jerf at jerf.org
Tue Mar 15 21:05:02 EST 2005


On Tue, 15 Mar 2005 03:21:19 -0800, George Jempty wrote:
> I'm noticing that Javascript's array/"hash" literal syntax is EXACTLY the
> same as that for Python lists/dictionaries.

No it isn't, quite.

Two differences of note, one literally syntax and one technically not but
you probably still want to know about it. 

First, Javascript objects can only use strings for keys, anything used as
a key will be converted to a string. Try this in your browser and you'll
see what I mean... the "instance" of the "class" I define (let's not get
into prototyping issues here :-) ) has its string value used as the key,
not the object:

javascript:function a(){}; a.prototype.toString = function () {return
'q';}; b = new a(); c = {}; c[b] = 1; alert(c['q'])

(All one line, if your browser objects to the newline.)

The other is the syntax point: The strings you use in {} expressions to
denote keys are used literally, they are not resolved. Thus, in the above
I *had* to write

c = {};
c[b] = 1;

Because had I written 

c = {b: 1}

I would have ended up with an object where c['b'] == 1; Javascript does
not resolve the "expression", 'cause it isn't one. 

(That said, certain reserved words like "class" and such do have to be
quoted, which means the safe bet is to quote them all, which leads to
Javascript objects that look identical to Python dicts. But 

{1+2: "moo"}

will end up different in each language.}

<steve_irwin>Danger danger danger!</steve_irwin>




More information about the Python-list mailing list