Advertisement
ewalstad

Classtionary

Sep 1st, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. class Classtionary(dict):
  2.     """A Class, er, dictionary, um, attributes-as-keys thingy.
  3.  
  4.    >>> c = Classtionary(foo="bar")
  5.    >>> d = dict(foo="bar")
  6.    >>> c, d
  7.    ({'foo': 'bar'}, {'foo': 'bar'})
  8.    >>> c.fizz = "bang"
  9.    >>> d.fizz = "bang"
  10.    Traceback (most recent call last):
  11.      File "<input>", line 1, in <module>
  12.    AttributeError: 'dict' object has no attribute 'fizz'
  13.    >>> d['fizz'] = "bang"
  14.    >>> c['fizz']
  15.    'bang'
  16.    >>> c.fizz
  17.    'bang'
  18.    >>> d['fizz']
  19.    'bang'
  20.    >>> d.fizz
  21.    Traceback (most recent call last):
  22.      File "<input>", line 1, in <module>
  23.    AttributeError: 'dict' object has no attribute 'fizz'
  24.    """
  25.  
  26.     def __init__(self, **kwargs):
  27.         self.__dict__ = self
  28.         self.update(kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement