Skip to content

Commit ffcb977

Browse files
committed
Add __new__ function and property of slice
Issue #98
1 parent 8c361a8 commit ffcb977

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-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,

builtin/testfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,2,3,

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

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