Dictionary Class


/ Published in: Python
Save to your folder(s)

This bit of code allows you to turn code that looks like this:

x = {'foo':1, 'bar':2 }
x['foo'] = 3

Into this:
x = dictclass( {'foo':1, 'bar':2 } )
x.foo = 3


Copy this code and paste it in your HTML
  1. class dictclass:
  2. """Takes a dictionary and uses it as its __dict__ member, effectively
  3. making the dictionary supply the object's attributes."""
  4.  
  5. def __init__( self, d ):
  6. self.__dict__ = d

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.