Define __iter__
and/or __contains__
for pybamm.ParameterValues
#4438
Labels
__iter__
and/or __contains__
for pybamm.ParameterValues
#4438
Description
The
pybamm.ParameterValues
is dict-like, and through its__getitem__
and__setitem__
methods allows dict-like key access. However, it does not support thein
keyword, which for a dict will treat the keys as an iterable. This means that natural code like:will not work (generally raises a
KeyError
). It is currently necessary to instead write explicitly:Defining the missing method
__iter__
(and__contains__
if preferred) would allowin
to work in logical test and for-loop contexts.Motivation
Code is cleaner if
pybamm.ParameterValues
looks like a souped-up dict always, not just sometimes. Key-value accessors are expected to iterate over their keys and to allow logical test on the presence/absence of keys. PyBaMM-containing code looks 'asymmetric' compared to code working with actualdict
objects due to the requirement to call intopybamm.ParameterValues.keys()
specifically.Was there any historic reason for the decision to exclude these methods?
Possible Implementation
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: