0

Let's say I have a code like this:

def read_from_file(filename):
    list = []
    for i in filename:
        value = i[0]
        list.append(value)
    return list

def other_function(other_filename):
    """
       That's where my question comes in. How can I get the list
       from the other function if I do not know the value "filename" will get?
       I would like to use the "list" in this function
    """
read_from_file("apples.txt")
other_function("pears.txt")

I'm aware that this code might not work or might not be perfect. But the only thing I need is the answer to my question in the code.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 3
    1. Return the list from the first function. 2. Pass it to the second function. What's the problem? At the moment, you're just ignoring it! – jonrsharpe Oct 04 '15 at 20:23
  • The thing is that i must not change the name of the function so i cant make it into two variable function. It must remain as other_function("pears.txt") –  Oct 04 '15 at 20:29
  • 1
    Then you will have to use global state, which is very poor practice - you should complain to whoever set you this homework. – jonrsharpe Oct 04 '15 at 20:31
  • And globals are not allowed either –  Oct 04 '15 at 20:33
  • In that case I don't know how you're expected to get access to the list content inside the second function. – jonrsharpe Oct 04 '15 at 20:33

4 Answers4

1

You have two general options. You can make your list a global variable that all functions can access (usually this is not the right way), or you can pass it to other_function (the right way). So

def other_function(other_filename, anylist):
    pass # your code here

somelist = read_from_file("apples.txt")
other_function("pears.txt.", somelist)
BlivetWidget
  • 10,543
  • 1
  • 14
  • 23
  • The thing is that i must not change the name of the function so i cant make it into two variable function. It must remain as other_function("pears.txt") –  Oct 04 '15 at 20:29
  • And I'm not allowed to use globals –  Oct 04 '15 at 20:32
  • If we're going to be pedantic, I'm going to point out two things: changing the definition of the function does not change its name, and other_function has access to the global namespace without creating a global variable. Oh, and as long as your instructor is giving troll homework, you should inform them that it's horrendously bad practice to use keywords as variable names (list). – BlivetWidget Oct 04 '15 at 20:37
  • Like I said.. I just wrote it as an example, the actual code is a bit bigger –  Oct 04 '15 at 20:39
0

You need to "catch" the value return from the first function, and then pass that to the second function.

file_name = read_from_file('apples.txt')
other_function(file_name)
dursk
  • 4,435
  • 2
  • 19
  • 30
  • The thing is that i must not change the name of the function so i cant make it into two variable function. It must remain as other_function("pears.txt") –  Oct 04 '15 at 20:29
0

You need to store the returned value in a variable before you can pass it onto another function.

a = read_from_file("apples.txt")
lanceavil
  • 1
  • 2
-1

There are at least three reasonable ways to achieve this and two which a beginner will probably never need:

  1. Store the returned value of read_from_file and give it as a parameter to other_function (so adjust the signature to other_function(other_filename, whatever_list))
  2. Make whatever_list a global variable.
  3. Use an object and store whatever_list as a property of that object
  4. (Use nested functions)
  5. (Search for the value via garbage collector gc ;-) )

Nested functions

def foo():
    bla = "OK..."

    def bar():
        print(bla)
    bar()

foo()

Global variables

Misc

  • You should not use list as a variable name as you're overriding a built-in function.
  • You should use a descriptive name for your variables. What is the content of the list?
  • Using global variables can sometimes be avoided in a good way by creating objects. While I'm not always a fan of OOP, it sometimes is just what you need. Just have a look of one of the plenty tutorials (e.g. here), get familiar with it, figure out if it fits for your task. (And don't use it all the time just because you can. Python is not Java.)
Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958