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

Skip to content

Commit 7d7cee9

Browse files
committed
Merge heads
2 parents 6ab96e4 + d64fc39 commit 7d7cee9

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

Doc/library/functions.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,9 @@ are always available. They are listed here in alphabetical order.
10421042
...]``. If *step* is positive, the last element is the largest ``start + i *
10431043
step`` less than *stop*; if *step* is negative, the last element is the
10441044
smallest ``start + i * step`` greater than *stop*. *step* must not be zero
1045-
(or else :exc:`ValueError` is raised). Example:
1045+
(or else :exc:`ValueError` is raised). Range objects have read-only data
1046+
attributes :attr:`start`, :attr:`stop` and :attr:`step` which return the
1047+
argument values (or their default). Example:
10461048

10471049
>>> list(range(10))
10481050
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1100,6 +1102,9 @@ are always available. They are listed here in alphabetical order.
11001102
sequence of values they define (instead of comparing based on
11011103
object identity).
11021104

1105+
.. versionadded:: 3.3
1106+
The :attr:`start`, :attr:`stop` and :attr:`step` attributes.
1107+
11031108

11041109
.. function:: repr(object)
11051110

Lib/test/test_range.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,35 @@ def test_comparison(self):
560560
range(0) >= range(0)
561561

562562

563+
def test_attributes(self):
564+
# test the start, stop and step attributes of range objects
565+
self.assert_attrs(range(0), 0, 0, 1)
566+
self.assert_attrs(range(10), 0, 10, 1)
567+
self.assert_attrs(range(-10), 0, -10, 1)
568+
self.assert_attrs(range(0, 10, 1), 0, 10, 1)
569+
self.assert_attrs(range(0, 10, 3), 0, 10, 3)
570+
self.assert_attrs(range(10, 0, -1), 10, 0, -1)
571+
self.assert_attrs(range(10, 0, -3), 10, 0, -3)
572+
573+
def assert_attrs(self, rangeobj, start, stop, step):
574+
self.assertEqual(rangeobj.start, start)
575+
self.assertEqual(rangeobj.stop, stop)
576+
self.assertEqual(rangeobj.step, step)
577+
578+
with self.assertRaises(AttributeError):
579+
rangeobj.start = 0
580+
with self.assertRaises(AttributeError):
581+
rangeobj.stop = 10
582+
with self.assertRaises(AttributeError):
583+
rangeobj.step = 1
584+
585+
with self.assertRaises(AttributeError):
586+
del rangeobj.start
587+
with self.assertRaises(AttributeError):
588+
del rangeobj.stop
589+
with self.assertRaises(AttributeError):
590+
del rangeobj.step
591+
563592
def test_main():
564593
test.support.run_unittest(RangeTest)
565594

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #9896: Add start, stop, and step attributes to range objects.
14+
1315
- Issue #13343: Fix a SystemError when a lambda expression uses a global
1416
variable in the default value of a keyword-only argument:
1517
(lambda *, arg=GLOBAL_NAME: None)

Objects/rangeobject.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Range object implementation */
22

33
#include "Python.h"
4+
#include "structmember.h"
45

56
/* Support objects whose length is > PY_SSIZE_T_MAX.
67
@@ -880,6 +881,13 @@ static PyMethodDef range_methods[] = {
880881
{NULL, NULL} /* sentinel */
881882
};
882883

884+
static PyMemberDef range_members[] = {
885+
{"start", T_OBJECT_EX, offsetof(rangeobject, start), READONLY},
886+
{"stop", T_OBJECT_EX, offsetof(rangeobject, stop), READONLY},
887+
{"step", T_OBJECT_EX, offsetof(rangeobject, step), READONLY},
888+
{0}
889+
};
890+
883891
PyTypeObject PyRange_Type = {
884892
PyVarObject_HEAD_INIT(&PyType_Type, 0)
885893
"range", /* Name of this type */
@@ -909,7 +917,7 @@ PyTypeObject PyRange_Type = {
909917
range_iter, /* tp_iter */
910918
0, /* tp_iternext */
911919
range_methods, /* tp_methods */
912-
0, /* tp_members */
920+
range_members, /* tp_members */
913921
0, /* tp_getset */
914922
0, /* tp_base */
915923
0, /* tp_dict */

0 commit comments

Comments
 (0)