r/learnpython•
4evrAStudent

Object-oriented Design Help

I'm working on this assignment from dailyprogrammer, but trying to solve using the object-oriented paradigm. Here is the code:

class Todo(object):

    def __init__(self, lst = [1, 2, 3]):
        self.lst = lst

    def add(self, item): #why isn't lst defined?
        self.lst.append(item)
        print(lst)

Here's what I'm trying to run it in the interpreter:

>>> todo = Todo()
>>> todo.add(4)

The error I'm getting is "NameError: Name 'lst' is not defined. How can I write this code so that 'lst' is recognized as a variable throughout the entire program? Thanks for the help!

Posted Aug 1, 2015, 8:57 PM UTC

28
Loading comments…