Skip to content Skip to sidebar Skip to footer

How To Fix The Function Issue While Allowing It To Be Dynamic?

Let's say i'm calling a function given the following arguments Function(char1, char2, char3, char4, Number) where the first 4 terms represent chars and the last is an int, i mig

Solution 1:

Pass number as the first argument:

def func(number, *char):
    ...

Solution 2:

If you're using python3.x (which you're now not, as you mentioned in the comments), you could unpack your args using * iterable unpacking, before using them:

def function(*args):
    *chars, number = args
    ...

It works like this:

In [8]: *a, b = (1, 2, 3, 4)

In [9]: a
Out[9]: [1, 2, 3]

In [10]: b
Out[10]: 4

Every element in argsexcept the last is sent to chars, which now becomes a list. You may now use the number variable as a separate variable.

Post a Comment for "How To Fix The Function Issue While Allowing It To Be Dynamic?"