0

suppose I have many variables, instead of writing them each every time inside (), is there a simpler way to compress all variables into one word?

def numbers():
    a = 1
    b = 2
    c = 3
    d = 4
    e = 5
    f = 6
    g = 7
    h = 8
    i = 9

    new_variable(a, b, c, d, e, f, g, h, i)


def new_variable(a, b, c, d, e, f, g, h, i):
    j = g + i
    print (j)
    new_variable_2(a, b, c, d, e, f, g, h, i)

def new_variable_2(a, b, c, d, e, f, g, h, i):
    k = g + h
    print (k)
    new_variable_3(a, b, c, d, e, f, g, h, i)

    def new_variable_3(a, b, c, d, e, f, g, h, i):
    l = h + i
    print (i)

numbers()
Johnny Kang
  • 145
  • 1
  • 12
  • Possible duplicate of [What does \*\* (double star) and \* (star) do for parameters?](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-parameters) – Stephen Rauch Apr 07 '17 at 21:50
  • Okay I read it but I don't quite think it's answering my question. My question is, instead of typing 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' every time, I want to make it simpler such as just typing 'alphabet' and make it able to recall all the variables. So instead of new_variable(a, b, c, d, e, f, g, h, i), can it make it into new_variable(alphabet)? If you know how to, could you show it to me? – Johnny Kang Apr 07 '17 at 22:01
  • @JohnnyKang I made an edit to my post, it might be what you were looking for. `*` unpacks the list. – zipa Apr 07 '17 at 22:31

1 Answers1

1

You can try it like this:

a, b, c, d, e, f, g, h, i = range(1,10)
alphabet = [a, b, c, d, e, f, g, h, i]

def new_variable(*alphabet):
    j = g + i
    print (j)
    new_variable_2(*alphabet)

def new_variable_2(*alphabet):
    k = g + h
    print (k)
    new_variable_3(*alphabet)

def new_variable_3(*alphabet):
    l = h + i
    print (i)

new_variable(*alphabet)

Returns same numbers as your code.

zipa
  • 27,316
  • 6
  • 40
  • 58