Skip to content

Introspectable range objects #54105

Closed
Closed

Description

@durban
mannequin
BPO 9896
Nosy @rhettinger, @terryjreedy, @mdickinson, @pitrou, @ezio-melotti, @merwok, @durban, @smarnach
Files
  • range_attrs.diff: Patch adding the start, stop and step attributes to range
  • range_attrs_2.patch: Updated patch
  • range_attrs_3.patch: Corrected patch following Éric's comments
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2011-11-05.19:19:10.729>
    created_at = <Date 2010-09-18.18:39:25.954>
    labels = ['interpreter-core', 'type-feature']
    title = 'Introspectable range objects'
    updated_at = <Date 2011-11-05.19:19:10.728>
    user = 'https://github.com/durban'

    bugs.python.org fields:

    activity = <Date 2011-11-05.19:19:10.728>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-11-05.19:19:10.729>
    closer = 'python-dev'
    components = ['Interpreter Core']
    creation = <Date 2010-09-18.18:39:25.954>
    creator = 'daniel.urban'
    dependencies = []
    files = ['18922', '21692', '21733']
    hgrepos = []
    issue_num = 9896
    keywords = ['patch']
    message_count = 13.0
    messages = ['116824', '116836', '116839', '116846', '116853', '117330', '117366', '133921', '134067', '134102', '145712', '147096', '147104']
    nosy_count = 9.0
    nosy_names = ['rhettinger', 'terry.reedy', 'mark.dickinson', 'pitrou', 'ezio.melotti', 'eric.araujo', 'daniel.urban', 'python-dev', 'smarnach']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9896'
    versions = ['Python 3.3']

    Activity

    1. durban commented on Sep 18, 2010

      durbanmannequin
      MannequinAuthor

      The attached patch adds the start, stop and step members to range objects, corresponding the contructor arguments. The patch also include tests and documentation changes.

    2. added
      interpreter-core
      type-feature
      on Sep 18, 2010
    3. rhettinger commented on Sep 18, 2010

      @rhettinger
      Contributor

      I don't see anything wrong with this idea, but I don't really see a need for it either. AFAICT, this has never been requested in the 20 years that range() has been in Python. This is probably YAGNI. It may also be in violation of the language moratorium (it's one more thing that other implementations would have to mirror).

    4. rhettinger commented on Sep 18, 2010

      @rhettinger
      Contributor

      One other thought. If there is a perceived need, I would rather an alternate approach that unifies a language a bit by letting range() expose its arguments as a slice and modify its input to accept a slice.

      >>> range(0, 20, 2).slice
      slice(0, 20, 20)
      >>> range(_)
      range(0, 20, 2)
      
      >>> s = range(0, 20, 2).slice
      >>> s.start
      0
      >>> s.stop
      20
      >>> s.step
      2
    5. BreamoreBoy commented on Sep 19, 2010

      BreamoreBoymannequin
      Mannequin

      What is the use case for this?

    6. durban commented on Sep 19, 2010

      durbanmannequin
      MannequinAuthor

      What is the use case for this?

      The basic idea was, that in Python almost everything is introspectable, so why range objects aren't. It was pretty straightforward to implement it, so I've done it (actually when I was working on rangeobject.c, I was surprised to see that these members aren't available from python code).
      But if this isn't needed, I'm absolutely ok with that. I think this would be a nice to have feature, but of course not necessary. Also, if it violates the moratorium (I don't know), then of course this sould be at least postponed.

    7. terryjreedy commented on Sep 24, 2010

      @terryjreedy
      Member

      Range objects are new in 3.0; they supersede 2.x xrange objects, which are perhaps 10 years old. I do not remember that xrange objects had such attributes. On the other hand, I believe there were requests.

      The request, with either implementation, seems reasonable in that range objects could be regarded as iterable slice objects. The differences are that range objects have __getitem__, __iter__, and __reversed__ special methods while slice objects have start, stop, and stop real-only attributes and the indices method. (I could not really understand the last from the doc; I had to experiment.) Starting fresh, we might do with just one class instead of two.

      I guess this makes me +0.3 at the moment.

    8. pitrou commented on Sep 25, 2010

      @pitrou
      Member

      One other thought. If there is a perceived need, I would rather an
      alternate approach that unifies a language a bit by letting range()
      expose its arguments as a slice and modify its input to accept a slice.

      This sounds like an obscure complication to me. A range is not a slice and I don't see the point of trying to bridge both concepts. It is even more YAGNI than the original request.

    9. durban commented on Apr 17, 2011

      durbanmannequin
      MannequinAuthor

      Now that the moratorium has already ended, I'll try again. I've updated the patch.

      It seems, that this idea has already came up in the past: Guido in msg70525 said: "I also think ranges should be introspectable, exposing their start, stop and step values just like slice objects."

      A possible use case: the range slicing logic is quite complex (with a lot of corner cases). It would be good, if this logic would be accessible from Python code, for example to compute a slice of a slice-like-thing. Currently slicing a range object returns another range object, but there is no way to determine the bounds of this new object.

    10. merwok commented on Apr 19, 2011

      @merwok
      Member

      Looks good, apart from the use of assertEquals (deprecated in favor of assertEqual). I’d also remove “merely” from the doc addition.

    11. durban commented on Apr 19, 2011

      durbanmannequin
      MannequinAuthor

      Thanks, I've corrected my patch.

    12. ezio-melotti commented on Oct 17, 2011

      @ezio-melotti
      Member

      See also bpo-13200.

    13. merwok commented on Nov 5, 2011

      @merwok
      Member

      I think this is ready for commit.

    14. python-dev commented on Nov 5, 2011

      python-devmannequin
      Mannequin

      New changeset 4643be424293 by Benjamin Peterson in branch 'default':
      add introspection to range objects (closes bpo-9896)
      http://hg.python.org/cpython/rev/4643be424293

    15. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-coretype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions