Skip to content

Commit 25d3f83

Browse files
committed
Add __len__ to dictionary views.
1 parent 31fd293 commit 25d3f83

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

tests/snippets/dict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def dict_eq(d1, d2):
6868
with assertRaises(TypeError):
6969
next(data.keys())
7070

71+
assert len(data.keys()) == 2
72+
7173
x = {}
7274
x[1] = 1
7375
assert x[1] == 1

vm/src/obj/objdict.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ macro_rules! dict_iterator {
265265
fn iter(&self, _vm: &VirtualMachine) -> $iter_name {
266266
$iter_name::new(self.dict.clone())
267267
}
268+
269+
#[pymethod(name = "__len__")]
270+
fn len(&self, vm: &VirtualMachine) -> usize {
271+
self.dict.clone().len(vm)
272+
}
268273
}
269274

270275
impl PyValue for $name {

0 commit comments

Comments
 (0)