Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Code Reviews A class to access dicts using attribute syntax

I've used something similar in the past (supporting attribute access as equivalent to dict item access), but I would then always derive the class from dict (or UserDict). This can be useful - but m...

posted 7mo ago by mudskipper‭  ·  edited 7mo ago by mudskipper‭

Answer
#2: Post edited by user avatar mudskipper‭ · 2024-05-27T17:11:55Z (7 months ago)
  • I've used something similar in the past (supporting attribute access as equivalent to dict item access), but I would then always make the class a dict. This can be useful - but mainly inside your own code, never as library code - if you have sections of the code that otherwise would need a lot of quoting plus square brackets.
  • It's tricky code, and I'd generally advise not to use it however:
  • - If a dictionary is passed in as argument to the class init, then after constructing the object, you have two, entirely separate references to a mutable dict in your code. This easily allows bugs to crawl in. It seems better to derive the class from dict or UserDict to prevent this.
  • - "DictProxy" seems a somewhat misleading or confusing name since it's not really a dict proxy: it doesn't add behavior to a class that otherwise behaves like a dict. It's also a bit confusing because the stdlib has various abc.mapping classes (and types.MappingProxy). I think this is yet another reason why it may be better to derive the class from collections.UserDict (or do sth similar).
  • - If the class is merely intended as view on the underlying dict data, I would make sure that instances of it are immutable (derive the class from Mapping or MappingView for instance).
  • - There seems to be an implicit assumption that the underlying dict will not have keys that are not valid Python attribute identifiers. This assumption could also lead to trouble (since certain keys will not be representable as attributes).
  • I've used something similar in the past (supporting attribute access as equivalent to dict item access), but I would then always derive the class from dict (or UserDict). This can be useful - but mainly inside your own code, never as library code - if you have sections of the code that otherwise would need a lot of quoting plus square brackets.
  • It's tricky code, and I'd generally advise not to use it however:
  • - If a dictionary is passed in as argument to the class init, then after constructing the object, you have two, entirely separate references to a mutable dict in your code. This easily allows bugs to crawl in. It seems better to derive the class from dict or UserDict to prevent this.
  • - "DictProxy" seems a somewhat misleading or confusing name since it's not really a dict proxy: it doesn't add behavior to a class that otherwise behaves like a dict. It's also a bit confusing because the stdlib has various abc.mapping classes (and types.MappingProxy). I think this is yet another reason why it may be better to derive the class from collections.UserDict (or do sth similar).
  • - If the class is merely intended as view on the underlying dict data, I would make sure that instances of it are immutable (derive the class from Mapping or MappingView for instance).
  • - There seems to be an implicit assumption that the underlying dict will not have keys that are not valid Python attribute identifiers. This assumption could also lead to trouble (since certain keys will not be representable as attributes).
#1: Initial revision by user avatar mudskipper‭ · 2024-05-27T17:11:13Z (7 months ago)
I've used something similar in the past (supporting attribute access as equivalent to dict item access), but I would then always make the class a dict. This can be useful - but mainly inside your own code, never as library code - if you have sections of the code that otherwise would need a lot of quoting plus square brackets. 

It's tricky code, and I'd generally advise not to use it however:
- If a dictionary is passed in as argument to the class init, then after constructing the object, you have two, entirely separate references to a mutable dict in your code. This easily allows bugs to crawl in. It seems better to derive the class from dict or UserDict to prevent this.
- "DictProxy" seems a somewhat misleading or confusing name since it's not really a dict proxy: it doesn't add behavior to a class that otherwise behaves like a dict. It's also a bit confusing because the stdlib has various abc.mapping classes (and types.MappingProxy). I think this is yet another reason why it may be better to derive the class from collections.UserDict (or do sth similar).
- If the class is merely intended as view on the underlying dict data, I would make sure that instances of it are immutable (derive the class from Mapping or MappingView for instance).
- There seems to be an implicit assumption that the underlying dict will not have keys that are not valid Python attribute identifiers. This assumption could also lead to trouble (since certain keys will not be representable as attributes).