pixel rain
pixel rain

Reputation: 75

Saving data of a function without use of Globals

below is my attempted function:

 def function(x):
        list1 = []
        if("word" in x):
          print("Option 1: XXX, Option 2: YYY ...")
          list1.extend(input("Enter a word: "))

What I'm attempting to do here is to place the value X with a string of my choosing (a question to the user) and have the user enter their answer. I then want to add that answer to a list that is NOT a global variable (As this is the main point I'm avoiding) without having that list be wiped everytime I call this function; as I wish to call function(x) several times for several questions. After this is done, I also need to be able to pull this list and pass it to another function so that math etc. can be done on the users answers.

Any advice at all? If you could EXPLAIN what's going on as well I would really appreciate it as I'm struggling to grasp what's actually happening here. I've been told that placing the list in the function argument works (and it does) but this doesn't allow me to later take the list and do things to it (and is apparently a bug in python). Running a loop for this also doesn't help much.

EDIT: I'll be back in a few hours and will mark an answer then; haven't read through them all yet but so far I think I'm getting a handle on it.

Upvotes: 1

Views: 65

Answers (2)

Tom Fuller
Tom Fuller

Reputation: 5349

You can use return to avoid using global variables

def function(x):
    answer = "n/a"
    if("word" in x): # test if x contains "word", not sure why you have this
        print("Option 1: XXX, Option 2: YYY ...")
        answer = input("Enter a word: ")

    return answer # this is the value given when the function is called

list1 = []
list1.append(function("...")) # add the value returned by the function
list1.append(function("..."))
...

Upvotes: 2

cdarke
cdarke

Reputation: 44404

I think what you are looking for is called a static in languages like C. Python does not need such a keyword because it is so flexible. One way to achieve it is to use a closure which maintains encapsulation. You will notice that the setupf function returns two internal functions, whose names can be anything that's legal.

def setupf():
    list1 = []    # This is the "static" variable

    def internal(x):
        if ("word" in x):
            print("Option 1: XXX, Option 2: YYY ...")
            list1.append(input("Enter a word: "))

    def getit():
        return list1

    return internal, getit


function, getlist = setupf()

function("word")
function("word")
function("word")
function("word")

print(getlist())

Gives:

Option 1: XXX, Option 2: YYY ...
Enter a word: hello
Option 1: XXX, Option 2: YYY ...
Enter a word: goodbye 
Option 1: XXX, Option 2: YYY ...
Enter a word: stuff
Option 1: XXX, Option 2: YYY ...
Enter a word: another
['hello', 'goodbye', 'stuff', 'another']

You might notice I replaced .extend with .append - in the C implementation of Python using .append is more efficient because of the way lists are implemented.

I am puzzled by the test for "word", perhaps you could explain.

Upvotes: 2

Related Questions