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

Skip to content

Commit c4de1c8

Browse files
committed
Add __eq__, __ne__ of range
__eq__ compare length, start, step of range
1 parent 0ff757f commit c4de1c8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

py/range.go

+55
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,58 @@ func computeRangeLength(start, stop, step Int) Int {
196196
var _ I__getitem__ = (*Range)(nil)
197197
var _ I__iter__ = (*Range)(nil)
198198
var _ I_iterator = (*RangeIterator)(nil)
199+
200+
201+
func (a *Range) M__eq__(other Object) (Object, error) {
202+
b, ok := other.(*Range)
203+
if !ok {
204+
return NotImplemented, nil
205+
}
206+
207+
if a.Length != b.Length {
208+
return False, nil
209+
}
210+
211+
if a.Length == 0 {
212+
return True, nil
213+
}
214+
if a.Start != b.Start {
215+
return False, nil
216+
}
217+
218+
if a.Step == 1 {
219+
return True, nil
220+
}
221+
if a.Step != b.Step {
222+
return False, nil
223+
}
224+
225+
return True, nil
226+
}
227+
228+
func (a *Range) M__ne__(other Object) (Object, error) {
229+
b, ok := other.(*Range)
230+
if !ok {
231+
return NotImplemented, nil
232+
}
233+
234+
if a.Length != b.Length {
235+
return True, nil
236+
}
237+
238+
if a.Length == 0 {
239+
return False, nil
240+
}
241+
if a.Start != b.Start {
242+
return True, nil
243+
}
244+
245+
if a.Step == 1 {
246+
return False, nil
247+
}
248+
if a.Step != b.Step {
249+
return True, nil
250+
}
251+
252+
return False, nil
253+
}

0 commit comments

Comments
 (0)