0

So I do not want to use global variables, and I have a list that I want to use i different functions. How do I do it?

I got this so far:

class List:
    def __init__(self):
        self.myList = list()

def func1():
    l = List()
    l.myList.append(22)

def func2():
    l.myList.append(34)

I get an error when executing the second function and the question is how I continue to put elements into myList using different functions.

Thank you

2
  • 1
    Can't you pass it as arguments to the functions? Commented Feb 23, 2014 at 18:09
  • I guess this is just for demonstration, but the class here adds nothing at all. Commented Feb 23, 2014 at 18:15

2 Answers 2

1

Pass the list as an argument to the two functions:

def func1(l):
    l.myList.append(22)

def func2(l):
    l.myList.append(34)

def driver():
    l = List()
    func1(l)
    func2(l)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was heplful, little late though. Better late than never right.:)
0

l object created in finc1:

l = List()

So, it's local. You should consider scope of variables and objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.