Is it possible to create anonymous objects in Python?
I like Tetha's solution, but it's unnecessarily complex.
Here's something simpler:
>>> class MicroMock(object):
... def __init__(self, **kwargs):
... self.__dict__.update(kwargs)
...
>>> def print_foo(x):
... print x.foo
...
>>> print_foo(MicroMock(foo=3))
3
I found this: http://www.hydrogen18.com/blog/python-anonymous-objects.html, and in my limited testing it seems like it works:
>>> obj = type('',(object,),{"foo": 1})()
>>> obj.foo
1