diff --git a/builtin/builtin.go b/builtin/builtin.go index bb4158c4..3c32d789 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -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, diff --git a/py/slice.go b/py/slice.go index 53b21493..827ad718 100644 --- a/py/slice.go +++ b/py/slice.go @@ -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 { @@ -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 diff --git a/py/tests/slice.py b/py/tests/slice.py new file mode 100644 index 00000000..8fb84a84 --- /dev/null +++ b/py/tests/slice.py @@ -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" \ No newline at end of file
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: