Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add __new__ function and property of slice #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func init() {
"range": py.RangeType,
// "reversed": py.ReversedType,
"set": py.SetType,
// "slice": py.SliceType,
"slice": py.SliceType,
"staticmethod": py.StaticMethodType,
"str": py.StringType,
// "super": py.SuperType,
Expand Down
25 changes: 23 additions & 2 deletions py/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type Slice struct {
Step Object
}

var SliceType = NewType("slice", `slice(stop) -> slice object
var SliceType = NewTypeX("slice", `slice(stop) -> slice object
"slice(stop)
slice(start, stop[, step])

Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`)
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).`, SliceNew, nil)

// Type of this object
func (o *Slice) Type() *Type {
Expand Down Expand Up @@ -151,4 +151,25 @@ func (r *Slice) GetIndices(length int) (start, stop, step, slicelength int, err
return
}

func init() {
SliceType.Dict["start"] = &Property{
Fget: func(self Object) (Object, error) {
selfSlice := self.(*Slice)
return selfSlice.Start, nil
},
}
SliceType.Dict["stop"] = &Property{
Fget: func(self Object) (Object, error) {
selfSlice := self.(*Slice)
return selfSlice.Stop, nil
},
}
SliceType.Dict["step"] = &Property{
Fget: func(self Object) (Object, error) {
selfSlice := self.(*Slice)
return selfSlice.Step, nil
},
}
}

// Check interface is satisfied
16 changes: 16 additions & 0 deletions py/tests/slice.py
Original file line number Diff line number Diff line change
@@ -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="slice"
a = slice(10)
assert a.start == None
assert a.stop == 10
assert a.step == None

a = slice(0, 10, 1)
assert a.start == 0
assert a.stop == 10
assert a.step == 1

doc="finished"