annotate source/jso.py @ 162:77e87f1b5811 core-javascript

$ hg merge core-javascript.closure-exercises # done, at least for now
author jf3243
date Wed, 09 Nov 2011 17:00:38 +0000
parents 4d70388df03e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
1 # class to represent JavaScript behavour, based on dict
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
2 class Jso(dict):
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
3 # Obj. & Obj[] are the same thing in JavaScript
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
4 def __getitem__(self, key):
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
5 return dict.get(self, key)
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
6 __getattribute__ = __getitem__
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
7
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
8 __setattr__ = dict.__setitem__
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
9
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
10 __delattr__ = dict.__delitem__
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
11
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
12 j = Jso()
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
13 j.a = 'a'
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
14 j['b'] = 'b'
4d70388df03e objects-are-dicts simulated in python class with interactive example
Tom V <tom@viner.tv>
parents:
diff changeset
15 print j