<div dir="ltr"><div><div><div><div>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:<div class="im"><br><br>Traceback (most recent call last):<br></div>
File "C:\Python33\todo.py", line 6, in <module><br> class todo:<br> File "C:\Python33\todo.py", line 31, in todo<br> self.tasks.append(line.strip())<br>NameError: name 'self' is not defined<br>
<br></div>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:<div class="im"><br>
<br>Traceback (most recent call last):<br></div> File "C:\Python33\todo.py", line 34, in <module><br> main.writeTask(main.todoList)<br>AttributeError: 'todo' object has no attribute 'todoList'<br>
<br></div><div>I fixed this with an attachment to self on the todoList object, but then got this error instead.<br></div><div><div class="im"><br>Traceback (most recent call last):<br></div> File "C:\Python33\todo.py", line 34, in <module><br>
main = todo()<br> File "C:\Python33\todo.py", line 14, in __init__<br> for line in todoList:<br>NameError: global name 'todoList' is not defined<br><br></div>My code now looks something like this:<br>
<br>class todo():<br> def __init__(self):<br> self.tasks = []<br> print("Hello")<br> try:<br> self.todoList = open('todo.txt', 'r+')<br> except IOError:<br>
self.todoList = open('todo.txt', 'w')<br> for line in todoList:<br> self.tasks.append(line.strip())<div class="im"><br><br> def writeTask(todoList):<br> name = input("Please enter the task > ")<br>
desc = input("Please enter a short description (optional) > ")<br> while True:<br> try:<br> imp = int(input("How important is this task? 1-100 > "))<br> break<br>
except TypeError:<br> imp = int(input("How important is this task? 1-100 > "))<br> if imp > 100:<br> imp = 100<br> elif imp < 1:<br> imp = 1<br>
todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) + "\"")<br> print("Task written!")<br> <br><br></div>
main = todo()<br>
main.writeTask(main.todoList)<br><br></div>It's recognising todoList as global now which is better than not being acknowledged at all, but why would it not be defined?<br><br></div>(Sorry if this came up twice, wasn't sure whether or not I'd replied to all)<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Mar 15, 2013 at 9:42 PM, Hugo Arts <span dir="ltr"><<a href="mailto:hugo.yoshi@gmail.com" target="_blank">hugo.yoshi@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5">On Fri, Mar 15, 2013 at 9:21 PM, Cameron Macleod <span dir="ltr"><<a href="mailto:cmacleod170@googlemail.com" target="_blank">cmacleod170@googlemail.com</a>></span> wrote:<br>
</div></div><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>Hello everyone, I'm using Python 3.3 and am trying to write a simple to-do list program. I have a class which runs pretty much everything called todo and the __init__ method doesn't seem to be running.<br>
<br>class todo():<br> def __init__(self):<br> tasks = []<br><br> def writeTask(todoList):<br> name = input("Please enter the task > ")<br> desc = input("Please enter a short description (optional) > ")<br>
while True:<br> try:<br> imp = int(input("How important is this task? 1-100 > "))<br> break<br> except TypeError:<br> imp = int(input("How important is this task? 1-100 > "))<br>
if imp > 100:<br> imp = 100<br> elif imp < 1:<br> imp = 1<br> todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) + "\"")<br>
print("Task written!")<br> try:<br> todoList = open('todo.txt', 'r+')<br> except IOError:<br> todoList = open('todo.txt', 'w')<br> for line in todoList:<br>
tasks.append(line.strip())<br> writeTask(todoList)<br> todoList.close()<br><br>main = todo()<br><br></div>Whenever I run this, I get the error:<br><br>Traceback (most recent call last):<br> File "C:\Python33\todo.py", line 8, in <module><br>
class todo():<br> File "C:\Python33\todo.py", line 34, in todo<br> tasks.append(line.strip())<br>NameError: name 'tasks' is not defined<br><br></div>Indicating that __init__ hasn't run since that is the initialization for tasks. I had always understood that __init__ was the equivalent of a constructor, and should be run at the instantiation of any class.<br>
</div></div></div></div></blockquote><div><br></div></div></div><div>That's not what it indicates, you should assume less and debug more. If you add a print statement in __init__, I can guarantee that you will see it executed. Go, try it now, don't assume I'm right ;)</div>
<div><br></div><div>Now, the only thing that this error actually indicates is that "tasks" doesn't exist when writeTask is being executed. In both __init__ and writeTask, the "tasks" variable is function-local: it exists only for the duration of the function. What you want is to attach tasks to your object instance, which you do through "self". self is the reference to the current instance, much like the this pointer in Java or C++. So if you do:</div>
<div><br></div><div>class Todo:</div><div> def __init__(self):</div><div> self.tasks = []</div></div><br></div><div class="gmail_extra"> def write_task(self, task):</div><div class="gmail_extra">
# other code around here</div><div class="gmail_extra"> self.tasks.append(line.strip())</div><div class="gmail_extra"><br></div><div class="gmail_extra">everything will be fine.</div><div class="gmail_extra">
<br></div><div class="gmail_extra">HTH,</div><div class="gmail_extra">Hugo</div></div>
</blockquote></div><br></div>