Functions with optional Python parameters

Asked

Viewed 362 times

1

I want to create a function that has the first optional parameter.

For example: the function range([start, ] stop [, step])

Thus, if only one argument is passed it is considered as the second parameter.

3 answers

1


In python it is usually not necessary to overload functions because it is a dynamically typed language and supports the passage of optional arguments to the functions.

In most cases just check the parameters:

def teste(begin=0, end=None):
  if end is None:
    for i in range(begin):
      print(i)
  else:
     for i in range(begin, end):
      print(i)

teste(10)
teste(10,20)

Code in the Rep.it: https://repl.it/repls/HeartfeltJumpyFiletype

But in one case or another you may need to simulate a function overload and for that you can use the library Multiple Dispatch.

Just memorize the functions to be overloaded with @dispatch followed by the types of the arguments.

from multipledispatch import dispatch


@dispatch(int, int, int)
def teste(begin=0, end=1, skip=1):
  for i in range(begin, end, skip):
    print(i)

@dispatch(int)
def teste(end):
  for i in range(end):
    print(i)


@dispatch(int, int)
def teste(begin=0, end=1):
  for i in range(begin, end):
    print(i)


teste(10)
teste(11,20)
teste(21,30,2)

Code in Repl.it: https://repl.it/repls/AffectionateIllfatedExecutable

Behind the scenes this library creates an object Dispatcherthat uses a developer to store different implementations of the function and for the programmer creates a function of the same name that selects between different implementations based on the parameters passed.

0

You must use * to pass unlimited values as parameter or else keyword Arguments to set a key and value.

After that, you would need to manage past arguments, as in the example below:

def simula_range(*args):

    start = 0
    step = 1
    stop = args[0]

    if len(args) > 1:

        step = args[2] if len(args) > 2 else step

        start = args[0]
        stop = args[1]

    print("Start =", start, "Stop =", stop, "Step =", step)

If you want something smaller, you can use predefined parameters and check if the parameter stop has a value or not.

If it has no value, you must change the value of start hair of stop and vice versa. See the example below:

def simula_range(start, stop = 0, step = 1):

    if not stop:
        start , stop = stop, start

    print("Start =", start, "Stop =", stop, "Step =", step)

simula_range(2)      # Start = 0 Stop = 2 Step = 1
simula_range(3,7)    # Start = 3 Stop = 7 Step = 1
simula_range(3,7,2)  # Start = 3 Stop = 7 Step = 2

0

That’s some kind of Range you’re making?

The way you described it isn’t possible, or at least I’ve never seen anything like it. If a parameter is optional, it needs to have a default value, and also the following parameters need to be optional.

You could declare the second and third parameters as optional, starting the second with None, and then check inside the function if you received something in this second parameter.

Example:

def range_test(start, stop = None, step = 1):
    if stop == None:
        start, stop = 0, start

    print(f'Range de {start} a {stop}, incremento de {step}')


range_test(10) # Range de 0 a 10, incremento de 1
range_test(5, 10) # Range de 5 a 10, incremento de 1
range_test(0, 20, 2) # Range de 0 a 20, incremento de 2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.