We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

kangell on 04/08/08


Tagged

class shorthand dictionary


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

pulczynski
kevcow


Dictionary Class


Published in: Python 


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

  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 

You need to login to post a comment.