[Tutor] __init__ doesn't seem to be running
Dave Angel
davea at davea.name
Fri Mar 15 23:52:20 CET 2013
(By top-posting, you lost all the context. Next time, post your
responses AFTER the part you're quoting. And don't just quote it all,
only the part that's relevant.)
On 03/15/2013 06:25 PM, Cameron Macleod wrote:
> I added the "self." to both the references to tasks and then I added the
> print statement to the __init__ function and I got the dreaded NameError of
> death:
>
>
> Traceback (most recent call last):
> File "C:\Python33\todo.py", line 6, in <module>
> class todo:
> File "C:\Python33\todo.py", line 31, in todo
> self.tasks.append(line.strip())
> NameError: name 'self' is not defined
Every one of your methods should normally start with a 'self' parameter.
You have one in __init__(), but you forgot it in writeTasks(). And
your indentation of the original code is trashed, with a number of lines
(starting with the try statement) that are NOT in the writeTask()
method. Thus they're considered part of the class definition, and are
interpreted in a very different way.
Please capitalize the class name, so we won't be confused by the code --
I interpreted the above stacktrace as reporting a problem in the todo()
method, when it's actually in the class itself because of the above
indentation bug.
>
> I then moved the entirety of the code that appeared outside of the
> functions but within the class into the writeTask function, this produced
> the longed after print statement and no errors, but was inappropriate for
> the task in hand. So I re-jiggled most of it into the __init__ method and
> left the call to writeTask outside of the class, passing in the argument
> main.todoList . This gave me the print statement (the init is running now!)
> but also this error:
>
>
> Traceback (most recent call last):
> File "C:\Python33\todo.py", line 34, in <module>
> main.writeTask(main.todoList)
> AttributeError: 'todo' object has no attribute 'todoList'
>
> I fixed this with an attachment to self on the todoList object, but then
> got this error instead.
>
> Traceback (most recent call last):
> File "C:\Python33\todo.py", line 34, in <module>
> main = todo()
> File "C:\Python33\todo.py", line 14, in __init__
> for line in todoList:
> NameError: global name 'todoList' is not defined
>
> My code now looks something like this:
>
> class todo():
> def __init__(self):
> self.tasks = []
> print("Hello")
> try:
> self.todoList = open('todo.txt', 'r+')
> except IOError:
> self.todoList = open('todo.txt', 'w')
> for line in todoList:
> self.tasks.append(line.strip())
>
>
> def writeTask(todoList):
that needs to be
def writeTask(self, todoList):
> name = input("Please enter the task > ")
> desc = input("Please enter a short description (optional) > ")
> while True:
> try:
> imp = int(input("How important is this task? 1-100 > "))
> break
> except TypeError:
> imp = int(input("How important is this task? 1-100 > "))
> if imp > 100:
> imp = 100
> elif imp < 1:
> imp = 1
> todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) +
> "\"")
> print("Task written!")
>
>
> main = todo()
> main.writeTask(main.todoList)
That line is trying to access an attribute of the main object called
todoList. Wouldn't it be much clearer if you just used self.todoList in
this method, and NOT have such an argument. That way, the definition
would be
def writeTask(self):
....
and the toplevel code would be:
main = todo()
main.writeTask()
>
> It's recognising todoList as global now which is better than not being
> acknowledged at all, but why would it not be defined?
>
todoList isn't a global in any case.
> (Sorry if this came up twice, wasn't sure whether or not I'd replied to all)
>
>
Please start over with a set of code that we can see in its entirety,
and report an error traceback for that particular code. As this message
appears now, some errors are in the earlier code, some in code we
haven't seen, and some in the new code posted here.
--
DaveA
More information about the Tutor
mailing list