Stefano Borini Stefano Borini - 1 year ago 84
Python Question

struct objects in python

I wanted to create a throwaway "struct" object to keep various status flags. My first approach was this (javascript style)

>>> status = object()
>>> = 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()
>>> = 4

I guess this is because object() does not have a
. I don't want to use a dictionary, and assuming I don't want to create the Anon object, is there another solution ?

Answer Source

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()...;-).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download