We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6f8e06a commit 8e99b32Copy full SHA for 8e99b32
2 files changed
py/set.go
@@ -46,6 +46,17 @@ func NewSetFromItems(items []Object) *Set {
46
return s
47
}
48
49
+func init() {
50
+ SetType.Dict["add"] = MustNewMethod("add", func(self Object, args Tuple) (Object, error) {
51
+ setSelf := self.(*Set)
52
+ if len(args) != 1 {
53
+ return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
54
+ }
55
+ setSelf.Add(args[0])
56
+ return NoneType{}, nil
57
+ }, 0, "add(value)")
58
+}
59
+
60
// Add an item to the set
61
func (s *Set) Add(item Object) {
62
s.items[item] = SetValue{}
py/tests/set.py
@@ -2,6 +2,8 @@
2
# Use of this source code is governed by a BSD-style
3
# license that can be found in the LICENSE file.
4
5
+from libtest import assertRaises
6
7
doc="__and__"
8
a = {1, 2, 3}
9
b = {2, 3, 4, 5}
@@ -81,6 +83,18 @@
81
83
assert 4 in c
82
84
assert 5 in c
85
86
+doc="add"
87
+a = set()
88
+a.add(1)
89
+a.add(2)
90
+a.add(3)
91
+assert len(a) == 3
92
+assert 1 in a
93
+assert 2 in a
94
+assert 3 in a
95
+assert 4 not in a
96
+assertRaises(TypeError, lambda: a.add())
97
98
doc="__eq__, __ne__"
99
a = set([1,2,3])
100
assert a.__eq__(3) != True
0 commit comments