shanenin Posted June 29, 2005 Report Share Posted June 29, 2005 (edited) I am really struggling trying to understand what the following code is doing. in particulare this line, I do not get what it is doing(where it came from)self.name = namewhere is self.name coming from?below is the code it came fromclass Critter(object): """A virtual pet""" def __init__(self, name): print "A new critter has been born!" self.name = name def __str__(self): rep = "Critter object\n" rep += "name: " + self.name + "\n" return rep def __cmp__(self, other): if self.name > other.name: return 1 if self.name < other.name: return -1 if self.name == other.name: return 0 def talk(self): print "Hi. I'm", self.name, "\n"# maincrit1 = Critter("Poochie")crit1.talk()crit2 = Critter("Randolph")crit2.talk()print "Printing crit1:"print crit1print "Directly accessing crit1.name:"print crit1.name Edited June 29, 2005 by shanenin Quote Link to post Share on other sites
jcl Posted June 29, 2005 Report Share Posted June 29, 2005 Not sure if you mean self.name specifically or self, so I'll hit both.'self' is the object itself. That is, in obj.method()the 'self' parameter inside method() refers to obj. The object is implicitly passed as the first argument to the function, as though the code wasobj.method(obj).self.name is a field (also known as an attribute, slot, property, member variable...) in self. A variable that's attached to the self object. It's created implicitly when a value is assigned to it, in this case in __init__(). (The details are more complicated, but then the details of all variables are more complicated than they appear.) Quote Link to post Share on other sites
shanenin Posted June 29, 2005 Author Report Share Posted June 29, 2005 I am still haveing some trouble grasping this. If I stare at the code long enough, i think it will make sence. Quote Link to post Share on other sites
jcl Posted June 29, 2005 Report Share Posted June 29, 2005 Object-oriented programming (OOP) requires a bit of a change of perspective.There's are conceptual models of OOP that can make it easier to understand, but I'm hesistant to describe them because I don't want corrupt you with an incomplete or faulty model. The the OO world is divided into several camps and the divisions run deep. There's frequently friction when members of different camps come together and their mental models clash.There are many people who never grok object-orientation or reject it. Quote Link to post Share on other sites
shanenin Posted June 30, 2005 Author Report Share Posted June 30, 2005 I am probably saying something really dumb , but here goes:is the following code just assigning the variable name(self.name) to the value name, or is something more going on. self.name = nameif that is the case, why can't any variable be used like the followingword = name Quote Link to post Share on other sites
shanenin Posted June 30, 2005 Author Report Share Posted June 30, 2005 (edited) I think I might be on to something. the folowing code does not workclass Critter(object): def __init__(self,test): print 'I am born' self.test = test def talk(self): print 'my name is',testcrit = Critter('poochie')crit.talk()but the following doesclass Critter(object): def __init__(self,test): print 'I am born' self.test = test def talk(self): print 'my name is',self.testcrit = Critter('poochie')crit.talk()is this to do with the encapsulation? the only way to get the value of test('poochie') available to the talk method is by using self.something. This gets around the encapsulation. Edited June 30, 2005 by shanenin Quote Link to post Share on other sites
jcl Posted June 30, 2005 Report Share Posted June 30, 2005 I am probably saying something really dumbYou're not.is the following code just assigning the variable name(self.name) to the value name, or is something more going on. self.name = nameIt's a simple assignment, just as you've seen before. The difference is that the 'self.name' designates a variable called 'name' that is a member of the object denoted by 'self'. If it helps, you can think of the object as being like an array except that instead of referencing the elements by integers, you use names. 'self.name' is sort of like 'self[0]'.if that is the case, why can't any variable be used like the followingword = name<{POST_SNAPBACK}>More on that below. It's interesting that you asked that though, because in most OOPLs you could in fact use 'word=name'. Python's use of an explicit 'self' parameter is unusual; Modula 3 is the only other language I can think of that uses it. Most languages have a magic variable named 'self' or 'this' that does the same thing but it only has to be used for self-reference or disambiguation.is this to do with the encapsulation? the only way to get the value of test('poochie') available to the talk method is by using self.something. This gets around the encapsulation.<{POST_SNAPBACK}>Right, it's encapsulation in action. The object designated by 'name' is encapulated in the object designated by 'self'. The expression 'self.name' reaches into 'self' to access 'name'.(This is really a borderline explanation. The nature of encapsulation is one of those conceptual issues I wanted to avoid until you get a feel for it. Even as I wrote the preceding paragraph I thought of models in which that description is invalid.) Quote Link to post Share on other sites
shanenin Posted June 30, 2005 Author Report Share Posted June 30, 2005 can the parameter "poochie" in the following line of code only be passed to the __init__() constructer?crit1 = Critter("Poochie") Quote Link to post Share on other sites
jcl Posted June 30, 2005 Report Share Posted June 30, 2005 can the parameter "poochie" in the following line of code only be passed to the __init__() constructer?crit1 = Critter("Poochie")<{POST_SNAPBACK}>No. You could add a mutation function to set the name if you wished.class Critter(object): """A virtual pet""" def __init__(self): print "A new critter has been born!" self.name = "" # No name yet def __str__(self): rep = "Critter object\n" rep += "name: " + self.name + "\n" return rep def __cmp__(self, other): if self.name > other.name: return 1 if self.name < other.name: return -1 if self.name == other.name: return 0 def rename(self, name): self.name = name def talk(self): print "Hi. I'm", self.name, "\n"You could also assign it directlycrit1.name = "Spot"but you shouldn't do that. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.