FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding the 'add' method to the set class by kellrott · Pull Request #212 · go-python/gpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 11 additions & 0 deletions py/set.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func NewSetFromItems(items []Object) *Set {
return s
}

func init() {
SetType.Dict["add"] = MustNewMethod("add", func(self Object, args Tuple) (Object, error) {
setSelf := self.(*Set)
if len(args) != 1 {
return nil, ExceptionNewf(TypeError, "append() takes exactly one argument (%d given)", len(args))
}
setSelf.Add(args[0])
return NoneType{}, nil
}, 0, "add(value)")
}

// Add an item to the set
func (s *Set) Add(item Object) {
s.items[item] = SetValue{}
Expand Down
14 changes: 14 additions & 0 deletions py/tests/set.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from libtest import assertRaises

doc="__and__"
a = {1, 2, 3}
b = {2, 3, 4, 5}
Expand Down Expand Up @@ -81,6 +83,18 @@
assert 4 in c
assert 5 in c

doc="add"
a = set()
a.add(1)
a.add(2)
a.add(3)
assert len(a) == 3
assert 1 in a
assert 2 in a
assert 3 in a
assert 4 not in a
assertRaises(TypeError, lambda: a.add())

doc="__eq__, __ne__"
a = set([1,2,3])
assert a.__eq__(3) != True
Expand Down

Back | FazBrowse Home | New Git URL