Commits

Jonathan Fine committed 6228f0f

$ hg rm source/objects-are-dicts.rst

  • Participants
  • Parent commits 347e107
  • Branches europython-2011

Comments (0)

Files changed (2)

 
    easiest-quiz-answers
    know-in-python
-   objects-are-dicts
 
 
 

source/objects-are-dicts.rst

-Objects are dictionaries (draft)
-================================
-
-.. code-block:: python
-
-   # class to represent JavaScript behavour, based on dict
-   class Jso(dict):
-       # Obj. & Obj[] are the same thing in JavaScript 
-       def __getitem__(self, key):
-           return dict.get(self, key)
-       __getattribute__ = __getitem__
-   
-       __setattr__ = __setitem__
-
-       __delattr__ = __delitem__
-
-
-.. code-block:: python
-
-   >>> # create js object
-   ... j = Jso()
-   >>> j
-   {}
-   >>> # set by attribute
-   >>> j.x = 3
-   >>> # get by item
-   >>> j['x']
-   3
-   >>> # get by attribute
-   >>> j.x
-   3
-   >>> j
-   {'x': 3}
-   >>> 
-   >>> # set by item
-   >>> j['x'] = 3
-   >>> j['x']
-   3
-   >>> j.x
-   3
-   >>> # missing items return undefined
-   >>> j['y']
-   undefined
-   >>> # so do missing attributes
-   >>> j.y
-   undefined
-   >>> 
-   >>> # delete by attribute
-   >>> del j.x
-   >>> j.x
-   undefined
-   >>> j['x']
-   undefined
-   >>> 
-   >>> # put the value back in
-   >>> j.x = 3
-   >>> # delete by item
-   >>> del j['x']
-   >>> j.x
-   undefined
-   >>> j['x']
-   undefined
-
-
-