Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 5
    Would a empty class just do the same? Commented Jul 20, 2011 at 18:01
  • 52
    Note if you are new to python: tuples are read-only once created, unlike C structs Commented Oct 30, 2011 at 18:28
  • 3
    @KurtLiu No, it would probably say TypeError: this constructor takes no arguments Commented May 23, 2015 at 5:15
  • This uses an object, with, internally, a dict __dict__ (well, like all objects, except if you use __slots__). So why not use a dict directly? mystruct = {'field1': value1, 'field2': value2}. TL;DR: here you're creating an object just for the purpose of using its internal dict object.__dict__, so it would be less complex to simply use a dict from the beginning. Commented Dec 5, 2020 at 22:14
  • ...especially since you can just do a = dict(foo=123, bar=456) to make that dict if you like the function call syntax with keywords better than the regular dict syntax, and also str()/repr() are somewhat more useful than just giving the object id. Commented Feb 17, 2021 at 10:29