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
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...
Answer
#2: Post edited
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
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).