diff --git a/py/set.go b/py/set.go index cc27a464..f38b32ff 100644 --- a/py/set.go +++ b/py/set.go @@ -111,6 +111,20 @@ func (s *Set) M__iter__() (Object, error) { return NewIterator(items), nil } +func (s *Set) M__and__(other Object) (Object, error) { + ret := NewSet() + b, ok := other.(*Set) + if !ok { + return nil, ExceptionNewf(TypeError, "unsupported operand type(s) for &: '%s' and '%s'", s.Type().Name, other.Type().Name) + } + for i := range b.items { + if _, ok := s.items[i]; ok { + ret.items[i] = SetValue{} + } + } + return ret, nil +} + // Check interface is satisfied var _ I__len__ = (*Set)(nil) var _ I__bool__ = (*Set)(nil) diff --git a/py/tests/set.py b/py/tests/set.py new file mode 100644 index 00000000..e1500ed1 --- /dev/null +++ b/py/tests/set.py @@ -0,0 +1,16 @@ +# Copyright 2019 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +doc="__and__" +a = {1, 2, 3} +b = {2, 3, 4, 5} +c = a.__and__(b) +assert 2 in c +assert 3 in c + +d = a & b +assert 2 in d +assert 3 in d + +doc="finished" \ No newline at end of file