1

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 might add more chars so the function can't be static . The main function will start like

def Function(*args):
  table_line = ""
  for x in range(Number/2): 

but by giving the Function args it no longer knows that it should use the supplied argument number in that third line.

cs95
  • 379,657
  • 97
  • 704
  • 746
Tim
  • 59
  • 1
  • 8

2 Answers2

2

Pass number as the first argument:

def func(number, *char):
    ...
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
1

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 args except the last is sent to chars, which now becomes a list. You may now use the number variable as a separate variable.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Forgot to mention, python2.7 , that method isn't supported on python<3. – Tim Oct 20 '17 at 06:28
  • @Tim Uff, those details are important. Go for what [this answer](https://stackoverflow.com/a/46843396/4909087) suggests then. – cs95 Oct 20 '17 at 06:29
  • Done thanks for the time you've taken to answer tho. – Tim Oct 20 '17 at 06:30