Wednesday, April 13, 2011

classes

when defining classes
we use the paramer self in much of the methods to refer to the name of whatever object we create using the class

ergo:

class Student(object):
def getScore(self):
....

s=Student(Omar, 4)

s.getScore()

in the method def getScore(self) self is bounded to whatever object is create which in this case is "s"


why can't you do s.getScore() cause wouldnt it know that s is a part of class Student and therefore when I do s.getScore it knows and i wont have to write self as a parameter?
seems redundant

so when I do s.getscore it knows getscore is a method of object "s"



cool notes:

so i dont' have to write self, i can actually write object or any other word in there so long as im consistent and i write SOMETHING. Cause when I dont it says the method takes no arguments and one given


some other things: you can set default paramters in normal functions that dont exist inside of a class but you can't set instance variabales in normal functions that dont exist inside a class

i played around with classes alot to see how it works and doesn't work when i change certain things

and in normal functions you can write jobersih and the program works so long as it doesn't call that line in the code:

however when writing classes you can't do that python will get mad even if the code or function is not called

there's a lot of pointer passing that goes on in python, that's why you need self, cause it passes arguments like that (dont ask me why). all that i can extrapolate is that when you do:
s1=Omar()
s1 is really self
and so
def runner(self,parameter="None")
s1.runner() means that s1 is self, calling the function runner (though instnatieing it should link the two) passing a paramter of nothing.

No comments: