
On 18.02.2021 12:00, Steven D'Aprano wrote:
On Wed, Feb 17, 2021 at 11:28:05AM -0800, Brendan Barnwell wrote:
When I see people suggest SimpleNamespace, it's usually just to use it as a dict which is accessed with attribute syntax instead of item syntax.
If its a dict, it must have dict methods. That leads to conflict: data fields and methods collide.
A Bunch (see my previous post) or SimpleNamespace doesn't have that problem. A Bunch has no methods (apart from dunders) and so there is no way for them to collide with field names.
Things like attrdict, and the Javascript "convenience" short-cut that allows dict key:item pairs to be accessed through attribute syntax, are an attractive nuisance because of that collision problem.
I use a Namespace class for such things, with methods using an underscore as prefix to avoid clashes with attributes. It supports the attribute protocol, the dictionary protocol and also comes with some extras, such as optional defaults for unknown attributes, or formatting support. Oh, and acquisition is also supported, for those who still know how this was used in Zope to do attribute access across hierarchies. You can find this in the egenix-mx-base package under mx.Misc.Namespace. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Feb 18 2021)
Python Projects, Coaching and Support ... https://www.egenix.com/ Python Product Development ... https://consulting.egenix.com/
::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/