Skip to content

Commit 4b996c8

Browse files
HyeockJinKimcorona10
authored andcommitted
Add __new__ function and property of slice (#99)
* Add __new__ function and property of slice Issue #98 * Add tests for slice
1 parent 9c36f4c commit 4b996c8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

builtin/builtin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func init() {
8888
"range": py.RangeType,
8989
// "reversed": py.ReversedType,
9090
"set": py.SetType,
91-
// "slice": py.SliceType,
91+
"slice": py.SliceType,
9292
"staticmethod": py.StaticMethodType,
9393
"str": py.StringType,
9494
// "super": py.SuperType,

py/slice.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ type Slice struct {
1313
Step Object
1414
}
1515

16-
var SliceType = NewType("slice", `slice(stop) -> slice object
16+
var SliceType = NewTypeX("slice", `slice(stop) -> slice object
1717
"slice(stop)
1818
slice(start, stop[, step])
1919
20-
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`)
20+
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`, SliceNew, nil)
2121

2222
// Type of this object
2323
func (o *Slice) Type() *Type {
@@ -151,4 +151,25 @@ func (r *Slice) GetIndices(length int) (start, stop, step, slicelength int, err
151151
return
152152
}
153153

154+
func init() {
155+
SliceType.Dict["start"] = &Property{
156+
Fget: func(self Object) (Object, error) {
157+
selfSlice := self.(*Slice)
158+
return selfSlice.Start, nil
159+
},
160+
}
161+
SliceType.Dict["stop"] = &Property{
162+
Fget: func(self Object) (Object, error) {
163+
selfSlice := self.(*Slice)
164+
return selfSlice.Stop, nil
165+
},
166+
}
167+
SliceType.Dict["step"] = &Property{
168+
Fget: func(self Object) (Object, error) {
169+
selfSlice := self.(*Slice)
170+
return selfSlice.Step, nil
171+
},
172+
}
173+
}
174+
154175
// Check interface is satisfied

py/tests/slice.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2019 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="slice"
6+
a = slice(10)
7+
assert a.start == None
8+
assert a.stop == 10
9+
assert a.step == None
10+
11+
a = slice(0, 10, 1)
12+
assert a.start == 0
13+
assert a.stop == 10
14+
assert a.step == 1
15+
16+
doc="finished"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy