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.

2
  • 51
    This has been my approach as well, but I feel like it's dangerous precisely because a dictionary can accept anything for a key. There won't be an error if I set myStruct["ffield"] when I meant to set myStruct["field"]. The issue might (or might not) become apparent when I'm using or re-using myStruct["field"] later. I like PabloG's approach. Commented Feb 13, 2014 at 0:30
  • Same issue exists with PabloG's. Try adding the following code to his: pt3.w = 1 print pt3.w In a language with dicts, it is better to use them, especially for objects being serialized, since you can automatically use import json to save them and other serialization libraries as long as you don't have weird stuff inside of your dict. Dicts are the solution to keeping data and logic separate and are better than structs for people who don't want to write custom serialize and unserialize functions and don't want to use non-portable serializers like pickle. Commented Aug 9, 2018 at 17:16