-1

I'm just wondering about some aspects of pythons range() function.

If you give it a single argument like range(10) It will take this argument as the point to stop. If you give two args, it will take them as start, stop, and if you give three args, it will handle them as start, stop, steps.

How is it that the order of arguments changes? With one argument it's just "stop", but the docs say "start, stop, steps"!

For example, if I make a function with default parameters (I think, it has to do something with that), like foo(bar=None, baz=None, arg=None), and call it like foo(1, 2),"bar" will be 1 and "baz" will be 2.

Thank you for help.

code11
  • 1,986
  • 5
  • 29
  • 37
YpsilonZett
  • 100
  • 1
  • 7
  • 3
    Deepest understanding possible: https://hg.python.org/cpython/file/tip/Objects/rangeobject.c – J0HN Nov 01 '16 at 14:42

3 Answers3

2

If you want to do something similar, you can accept any number of arguments, and then manually raise errors if necessary

def foo(*args):
    if not args:
        raise TypeError("foo expected 1 arguments, got 0")
    elif len(args)>3:
        raise TypeError("foo expected at most 3 arguments, got {}".format(len(args)))
    pass

And then assign start = args[0] etc, based on len(args)

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

If I understand your question correctly, you are wondering how does the range function get defined with start, stop, steps in that order if giving one argument like range(10) allows it to get used as the stop parameter instead of start which is the first one?

I imagine the code could be something like:

def range(start=0, stop=None, steps=None):
    if stop is None:
        stop = start
        start = 0
    ...

Then it may continue

ugoano
  • 118
  • 1
  • 5
-2

Do you mean this?

>>> def range_named_args(**args):
        if 'start' not in args.keys():
            args['start'] = 0
        if 'steps' not in args.keys():
            return(range(args['start'],args['stop']))
        else:
            return(range(args['start'],args['stop'],args['steps']))


>>> for i in range_named_args(stop = 3):
        print(i)
0
1
2
>>> for i in range_named_args(stop = 5, start = 2):
        print(i)
2
3
4
>>> for i in range_named_args(steps = 2, stop = 6, start = 2):
        print(i)
2
4
Rejected
  • 4,445
  • 2
  • 25
  • 42
Evan
  • 2,120
  • 1
  • 15
  • 20
  • No, this doesn't answer the question at all. He asked about how arguments can "change places" as more arguments are provided. You don't answer this at all, and deal strictly with keyword arguments. – Rejected Nov 01 '16 at 15:21
  • What are you talking about? He made it quite clear that he was looking to add functionality to built-in range() through a function of his own. – Evan Nov 01 '16 at 15:27
  • Re-read his question, he didn't ask/say that at all. He specifically said "I'm just wondering about some aspects of pythons range() function", and then lists various scenarios where providing additional arguments changes their placement (which is what he's asking about). When he specifies his own function, it's only to show that Python's default behaviour differs from that of the `range()` function, in that it keeps the order of passed arguments. Never does he say he want's to extend the functionality, nor does anything in your question address his. – Rejected Nov 01 '16 at 15:33
  • "How could it be made, that the order of arguments changes" – Evan Nov 01 '16 at 15:44
  • Taken out of context, and disregarding everything else written, maybe you can arrive at that being the question. In context, it's pretty clear that the author is questioning how argument order changes with the inclusion of more arguments. Additionally, it's pretty easy to see the author is likely not a native English speaker, further exemplified by "I think it has to do something with that". – Rejected Nov 01 '16 at 15:50
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/14158868) – 463035818_is_not_an_ai Nov 03 '16 at 09:51
  • @tobi303 In spite of the first line, this *is* an answer, not a new question - your review comment is not applicable. – Ajean Nov 03 '16 at 20:36