0

I was working in Python and wondered if there was a to access the current list being built. Here is what I mean:

def foo():
    return 10
def bar(baz):
    return baz * 2
list = [foo(), bar(this_list[0])]

Notice this_list. The output should be 20 because foo returns 10, then is passed to bar which doubles it, getting 20. How could I do this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
xilpex
  • 3,097
  • 2
  • 14
  • 45

1 Answers1

0

In your example code, you are trying to assign something to the variable list in a way that tries to use the contents of list itself. Yet, you haven't assigned any contents to list before trying to do this assignment, so the computer has no idea what "the contents of list itself" are supposed to be.

One can't reference the value of a variable before one puts a value inside the variable!

To achieve the desired effect, it's cleaner to simply do things step-by-step, in a "boring" way.

def foo():
    return 10
def bar(baz):
    return baz * 2

list = [foo()]
list.append(baz(list[0]))
Asker
  • 1,299
  • 2
  • 14
  • 31