2

I have come across that only parameters at the END of a paramater list can have a default value as VALUES are assigned by position in a function as how it is defined below

def greeting(a,b=7):
    pass

and NOT

def greeting(a=4,b):
    pass

The doubt which I have in built-in range() function is we can pass the arguments as follows

range(start_value, stop_value, increment_value)

and not all the arguments are needed while calling the function, so is range() function an overloaded function, or does it has default paraments in the definition?

Epsi95
  • 8,832
  • 1
  • 16
  • 34
  • If you want to implement a similar function, refer to [class - Python function overloading - Stack Overflow](https://stackoverflow.com/questions/6434482/python-function-overloading). – user202729 Jan 20 '21 at 05:20
  • But for "how is it implemented", ... I think the implementation is in C (and why would you want to know anyway?) – user202729 Jan 20 '21 at 05:21
  • Your question is unclear, `range()` does indeed have optional parameters. Is it unclear how to use it? Or is it unclear how you would write such a function yourself? Please be specific. – Grismar Jan 20 '21 at 05:22
  • @Grismar, I know to use the range() function and I am not writing similar function. My doubt is if there are optional parameters, then it should be only at the end and not at the first right (I am asking about start_value in built-in range() function. – venkatraman Balasubramanian Jan 20 '21 at 05:32
  • So you are asking how the `range()` function manages to deal with the optional start value? Why do you need to know? If you want to write a similar function, ask a direct question about that. If you are just curious, look at the source code. If you don't understand how to use it, give an example of use. – Grismar Jan 20 '21 at 06:35
  • Yes, I was asking how does it manages with optional start value. And I do not have personal laptop to install Python and I cannot install it in my organization's laptop. So, if anyone tells how it works, it will be good instead of asking why should I need to know that – venkatraman Balasubramanian Jan 20 '21 at 08:19

1 Answers1

2

range gets three arguments and it checks count of argument then returns generated iterible object

If you want to do something like it so you shoud replace all arguments with *args then do like following

def range_(*args):
    start = 0
    end = 0
    step = 0
    # args is a list of arguments
    if len(args) == 1:
        end = args[0]
    elif len(args) == 2:
        start, end = args
    elif len(args) == 3:
        start, end, step = args
    else:
        print("the function gets maximum three arguments")
    # calculating something with start, end, step
    # and return result

P. S. if you want define function with default arguments then default arguments should be defined before others

Artyom Vancyan
  • 5,029
  • 3
  • 12
  • 34