Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Mash equivalent in Python?

in Ruby, there's this awesome library called a Mash which is a Hash but through clever use of missing_method can convert:

object['property']

to

object.property

This is really useful for mocks. Anyone know of a similar kind of thing in Python?

like image 276
Julian H Avatar asked Dec 17 '22 21:12

Julian H


1 Answers

Is it absolutely necessary that you base this on a dict? Python objects can dynamically acquire attributes with very little extra plumbing:

>>> class C(object): pass
...
>>> z = C()
>>> z.blah = "xyzzy"
>>> dir(z)
['__class__', '__delattr__', '__dict__', ... '__weakref__', 'blah']
like image 64
PaulMcG Avatar answered Jan 03 '23 01:01

PaulMcG