what is wrong with my code?
rzed
rzantow at gmail.com
Wed Dec 20 21:38:26 EST 2006
Pyenos <pyenos at pyenos.org> wrote in
news:87mz5i56j7.fsf at pyenos.pyenos.org:
> thanks for your point. so because i have said class blah: i must
> explicitly say self for every instantiation of object?
>
No, for every method within the class.
Given:
class Blah(object):
def method1(self,arg1):
self.x = arg1
b = Blah() # b is an instance of class Blah
b.method1(32) # when you invoke it, just pass arg1
print b.x # prints 32
The use of 'self' is just a convention, but a *very* common one,
and one you should follow if you expect other Python programmers
to read your code. This is legal, and has the same effect as the
above class:
class Blah2(object):
def method1(spugsl,arg1):
spugsl.x = arg1
Spugsl (or self) is just the way to refer to the instance within
the code.
In your code, each method wound up with a different name, and none
of the names would have been associated with what you would have
expected. So for example, in
def removeEntry(pid_to_remove, task_to_remove):
... your equivalent to 'self' would be pid_to_remove, and the pid
you passed in would have been associated with task_to_remove.
--
rzed
More information about the Python-list
mailing list