From f0bd5c6189907428091b71397decfbceea8e204b Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 24 Jan 2025 17:24:49 -0800 Subject: [PATCH] `MultiFab.__len__()` We already have `.size` and we can iterate the `FabArray`, so we should support `len(mf)`, too. --- src/Base/MultiFab.cpp | 2 ++ tests/test_multifab.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Base/MultiFab.cpp b/src/Base/MultiFab.cpp index b86d26ff..f3cb93aa 100644 --- a/src/Base/MultiFab.cpp +++ b/src/Base/MultiFab.cpp @@ -143,6 +143,8 @@ void init_MultiFab(py::module &m) "Return number of variables (aka components) associated with each point.") .def_property_readonly("size", &FabArrayBase::size, "Return the number of FABs in the FabArray.") + .def("__len__", &FabArrayBase::size, + "Return the number of FABs in the FabArray.") .def_property_readonly("n_grow_vect", &FabArrayBase::nGrowVect, "Return the grow factor (per direction) that defines the region of definition.") diff --git a/tests/test_multifab.py b/tests/test_multifab.py index 2abbee53..eb2db0ca 100644 --- a/tests/test_multifab.py +++ b/tests/test_multifab.py @@ -269,6 +269,8 @@ def test_mfab_ops(boxarr, distmap, nghost): def test_mfab_mfiter(mfab): + assert len(mfab) == 8 + assert iter(mfab).is_valid assert iter(mfab).length == 8