Is there a data type in Python similar to structs in C++? I like the struct feature myStruct.someName. I know that classes have this, but I don’t want to write a class everytime I need a “container” for some data.
Is there a data type in Python similar to structs in C++? I like
Share
Why not? Classes are fine for that.
If you want to save some memory, you might also want to use
__slots__so the objects don’t have a__dict__. See http://docs.python.org/reference/datamodel.html#slots for details and Usage of __slots__? for some useful information.For example, a class holding only two values (
aandb) could looks like this:If you actually want a dict but with
obj.itemaccess instead ofobj['item'], you could subclass dict and implement__getattr__and__setattr__to behave like__getitem__and__setitem__.