I wanted to create a throwaway "struct" object to keep various status flags. My first approach was this (javascript style)
>>> status = object()
>>> status.foo = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'foo'
Definitely not what I expected, because this works:
>>> class Anon: pass
...
>>> b=Anon()
>>> b.foo = 4
I guess this is because object() does not have a __dict__
. I don't want to use a dictionary, and assuming I don't want to create the Anon object, is there another solution ?
The most concise way to make "a generic object to which you can assign/fetch attributes" is probably:
b = lambda:0
As most other answers point out, there are many other ways, but it's hard to beat this one for conciseness (lambda:0
is exactly the same number of characters as object()
...;-).