Re: [Tutor] Simple Question On A Method (in subclass)
Marc Tompkins wrote: It can be a little hard to wrap your head around how Python handles variables/objects; in other languages you create a variable and assign a value to it, while in Python you create an object and assign a name to it - the name can change while the object remains unchanged. Here's a very simplified demo of what Dave is talking about: [...] It's extremely logical, but almost entirely backward from the way most other languages do things. Possibly it's because Guido is Dutch. Fortunately, that is untrue. I'm not sure where the myth that "Python is different from other languages" comes from. I suppose it is true only so far as *every* language is different from any other language (otherwise they would be the same language!). But Python is not so different from other common languages. In particular, I don't know of any language where assignment means aliasing. Take the example Marc gave earlier: t = 'this' s = 'that' group = [t, s] print group # => "['this', 'that'] s = 'the other' print group # => "['this', 'that'] I don't know of any language where the second item of group would now equal 'the other'. Pascal certainly isn't one: program example (input, output); var t,s: String(10); group: array[1..2] of String(10); begin t:='this'; s:='that'; group[1]:=t; group[2]:=s; writeln(group[1], ' ', group[2]); s:='the other'; writeln(group[1], ' ', group[2]); end. Running that program gives the equivalent output to Python: this that this that PHP is the same. Using the PHP interactive shell: php > $t = 'this'; php > $s = 'that'; php > $group[0] = $t; php > $group[1] = $s; php > print_r($group); Array ( [0] => this [1] => that ) php > $t = 'the other'; php > print_r($group); Array ( [0] => this [1] => that ) This myth of Python being radically different from other languages is especially mysterious since many of the most popular and common modern languages use *exactly* the same name binding execution model as Python, e.g. Java and Ruby. (In the case of Java, that only applies to boxed objects, and not unboxed low-level ints and similar. If this means nothing to you, feel fortunate.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
>I want to make >sure you guys, especially Alan & Dave, are appreciated for taking their >own personal time to do this, for free. Your bill will arrive shortly ;) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- -Original Message- From: tutor-bounces+ramit.prasad=jpmorgan@python.org [mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of Chris Kavanagh Sent: Monday, October 24, 2011 6:17 AM To: Alan Gauld Cc: tutor@python.org Subject: Re: [Tutor] Simple Question On A Method (in subclass) On 10/24/2011 4:40 AM, Alan Gauld wrote: > On 24/10/11 04:08, Chris Kavanagh wrote: > >> Thanks so much for the help Alan. . .I'm not trying to beat this >> question into the ground, LOL, but let me see if I can ask it a better >> way. > > Marc has already given a good answer, but I'll try a slightly different > approach to the same thing The differences are purely philosophical :-) > >> So we have {member.tell} as the last line of code. So trying to >> understand this piece of code, {member} the variable is considered an >> object? > > Almost. > > member is an object because it is an instance of a class. > We created instances of Teacher and Student (t and s) and put them in a > list (members) with this code: > > t = Teacher('Mrs. Shrividya', 40, 3) > s = Student('Swaroop', 22, 75) > members = [t, s] > > Thus for member in members takes each object in the list and assigns it > to member. > > We can read {for member in members} as > > "Let member take on, in turn, each value in the collection members" > > And making it more explicit with a while loop: > > index = 0 > while index < len(members): > member = members[index] > member.tell() > index += 1 > > > Therefore we can combine it with a function {tell()} using dot > > notation?? Is this correct??? I haven't seen anything but an > > object combined with a function using dot notation. > > dot notation is how we access anything that is defined inside the class > definition. In this case there are only the __init__() and tell() > methods but there could be data attributes too. Furthermore we can add > data attributes to the objects by using the self parameter, as is done > in __init__() So we end up with the instance containing the attributes > defined at the class level (including those of its superclass) plus any > added by the __init__() method which is called when the object is > created (or instantiated). So for Teacher the attributes will be: > > __init__() > tell() > name - from SchoolMember.__init__() > age - ditto > salary - from Teacher.__init__() > > So we could have accessed any of those for the first iteration of the > loop because member was a Teacher instance (t) using: > > member.tell, member.name, member.age, member.salary > > But the last one would have broken for the second object in the list > since it was a student, which doesn't have a salary. So when dealing > with a list of objects we have to restrict the access via dot notation > to those attributes that are common to all objects in the collection - > usually the ones defined in the super class. > > Python doesn't care whether the attributes we access are data or > functions because in Python everything is an "object" (see Marc's post) > But by putting parentheses after the object name Python treats the named > object as a function (specifically a "callable" object) > > You can see this diffence at work in the interpreter if you > type this: > > >>> def f(): > ... return "Called f" > ... > >>> print f > > >>> print f() > Called f > > See the difference the () made? Without it we print the function object > called f. With it we print the result of calling the function f. > Thats quite a subtle concept and usually hard for beginners to grasp but > it is a very powerful concept and you will find it being used in > more advanced programs, especially GUI development. > > > So I'm trying to figure out how we can >> combine the variable {member} with the function {tell}. Hope this >> question makes sense to you, LOL. Thanks again. > > How we combine it is done inside Python as part of the magic of classes > and instantiation. Basically you can call any function defined inside a > class and Python will automatically assign the first parameter of that > function to the object instace that you are referencing at the time. > > Thus when we do > > for member in members: > member.tell(
Re: [Tutor] Simple Question On A Method (in subclass)
On Tue, Oct 25, 2011 at 5:31 AM, Chris Kavanagh wrote: > > > On 10/25/2011 3:50 AM, Dave Angel wrote: > >> On 10/25/2011 12:20 AM, Chris Kavanagh wrote: >> >>> >>> >>> On 10/24/2011 12:06 AM, Marc Tompkins wrote: >>> On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh >>> >>> >>> My problem was, I wasn't seeing {member} as referring to the class >>> objects {t} and {s}. Since it was, we now can use member just like any >>> class object, and combine it with class functions (and class >>> variables), such as {member.tell}. I had never in my short programming >>> experience, seen an example like this. So I was confused, obviously, LOL. >>> >>> >> In the context of: >> >> t = Teacher('Mrs. Shrividya', 40, 3) >> s = Student('Swaroop', 22, 75) >> members = [t, s] >> >> for member in members; >> member.dosomething() >> >> member does not refer to t and s at all. It refers to the same object as >> t and as s, in succession. members is a list of references to objects. >> Each item in members is bound to a particular object. It is not bound in >> any way to s or t. >> >> For example, suppose we did: >> >> members = [t, s] >> t = 42 >> for member in members: >> member.dosomething() >> >> member still references the object holding Mrs. Shrividya, or Swaroop, >> in succession, even though t is now (bound to) an integer (object). >> >> >> I understand. . .Thanks for clearing that up for me Dave. > So much to learn, so little time! LOL. > Thanks again to everyone, it's much appreciated. > It can be a little hard to wrap your head around how Python handles variables/objects; in other languages you create a variable and assign a value to it, while in Python you create an object and assign a name to it - the name can change while the object remains unchanged. Here's a very simplified demo of what Dave is talking about: >>> t = "This" >>> s = "That" >>> members = [t, s] >>> print members ['This', 'That'] >>> s = "The other" >>> print members ['This', 'That'] In other words, "members" is NOT a list of "t" and "s" - it's a list of the objects that "t" and "s" pointed to AT THE MOMENT you created "members". You can re-assign "t" and "s" to other objects, but the objects that are members of "members" remain unchanged until you manipulate them directly: >>> members[0] = "Something else entirely" >>> print members ['Something else entirely', 'That'] >>> print t, s This The other It's extremely logical, but almost entirely backward from the way most other languages do things. Possibly it's because Guido is Dutch. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/25/2011 3:50 AM, Dave Angel wrote: On 10/25/2011 12:20 AM, Chris Kavanagh wrote: On 10/24/2011 12:06 AM, Marc Tompkins wrote: On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh My problem was, I wasn't seeing {member} as referring to the class objects {t} and {s}. Since it was, we now can use member just like any class object, and combine it with class functions (and class variables), such as {member.tell}. I had never in my short programming experience, seen an example like this. So I was confused, obviously, LOL. In the context of: t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) members = [t, s] for member in members; member.dosomething() member does not refer to t and s at all. It refers to the same object as t and as s, in succession. members is a list of references to objects. Each item in members is bound to a particular object. It is not bound in any way to s or t. For example, suppose we did: members = [t, s] t = 42 for member in members: member.dosomething() member still references the object holding Mrs. Shrividya, or Swaroop, in succession, even though t is now (bound to) an integer (object). I understand. . .Thanks for clearing that up for me Dave. So much to learn, so little time! LOL. Thanks again to everyone, it's much appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/25/2011 12:20 AM, Chris Kavanagh wrote: On 10/24/2011 12:06 AM, Marc Tompkins wrote: On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh My problem was, I wasn't seeing {member} as referring to the class objects {t} and {s}. Since it was, we now can use member just like any class object, and combine it with class functions (and class variables), such as {member.tell}. I had never in my short programming experience, seen an example like this. So I was confused, obviously, LOL. In the context of: t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) members = [t, s] for member in members; member.dosomething() member does not refer to t and s at all. It refers to the same object as t and as s, in succession. members is a list of references to objects. Each item in members is bound to a particular object. It is not bound in any way to s or t. For example, suppose we did: members = [t, s] t = 42 for member in members: member.dosomething() member still references the object holding Mrs. Shrividya, or Swaroop, in succession, even though t is now (bound to) an integer (object). -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On Mon, Oct 24, 2011 at 3:44 PM, bob gailer wrote: > On 10/24/2011 7:45 AM, Wayne Werner wrote: > > On Sun, Oct 23, 2011 at 11:06 PM, Marc Tompkins > wrote: > >> Things to remember: >> -you can get a value from a method, but you can't assign to it: >> variable = object.method() >> but NOT >> object.method() = variable >> > > As a slight aside, you _can_ assign to the method name: > > object.method = variable #object.method is now whatever variable was > > I'm not aware of any valid reason to do this, that is to say I don't know > of anything that you could do this way that you couldn't do another more > readable/maintainable way. > > > In my Python Pipelines program I deliberately reassign instance methods. > Example: > > Given a (trivial) pipeline specification: "< foo.txt | > goo.txt" > meaning: "open foo.txt, open goo.txt, read one record at a time from > foo.txt, write it to goo.txt, at end close both files." > The specification parser creates an instance of ReadFile and an instance of > WriteFile, It must then "connect" these two instances, such that the output > of the first goes to the input of the second. The IMHO easiest way to do > this: instance1.output = instance2.input where output and input are methods. > > I've had other cases where the __init__method of a class determines some > behaviors by assigning one of several methods to a particular method. > > I've had yet other cases where I swap methods during the first call, such > that subsequent calls have different behavior. > > All of these could be done differently, but reassigning methods makes the > most sense to me. > As Einstein said, "Everything should be made as simple as possible, but not simpler." I also reassign methods (it's not my first choice, but sometimes it's the best way to do what I want to do) but I thought it would muddy the waters if I brought it up. I apologize for over-simplifying. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On Mon, Oct 24, 2011 at 9:20 PM, Chris Kavanagh wrote: > Makes perfect sense now. . .Thanks again Marc (and Alan, Dave) > BTW, do you guys luv Python the way I do!?? I just luv the way everything > works together so explicitly. I LUV learning this stuff!! > Oh yeah, baby. Python makes programming fun again. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/24/2011 12:06 AM, Marc Tompkins wrote: On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh mailto:cka...@msn.com>> wrote: So we have {member.tell} as the last line of code. So trying to understand this piece of code, {member} the variable is considered an object? Therefore we can combine it with a function {tell()} using dot notation?? Is this correct??? I haven't seen anything but an object combined with a function using dot notation. When I say "object", I mean an "object" created from a class. So I'm trying to figure out how we can combine the variable {member} with the function {tell}. Hope this question makes sense to you, LOL. Thanks again. First of all: other languages distinguish between variables and objects, and between functions and objects, but in Python both variables and functions are objects. EVERYTHING is an object. This is an important thing to remember - even if you never create classes of your own (which would be a terrible waste, BTW) a lot of the language won't make sense unless you remember that everything's an object. Second, the line "members = [t, s]" creates a list "members" (which is also an object, by the way!) containing two objects - "t" is a Teacher, "s" is a Student - which are both subclassed from SchoolMember. The line "for member in members" means: step through the list "members" and work with each object we find in it; let's call that object "member" while we're working with it. As soon as we finish with the first object and move on to the next, call the next one "member" - and so on. The beauty of this approach is that it simply doesn't matter what the contents of the list are - one could be a Student, the next a WoollyMammoth - and as long as your code only references methods and attributes that work for all the items in the list, Python won't care. Third, dot notation: objects have "methods" (which in non-OOP contexts would be called "functions") and "attributes" (variables, more or less.) From outside of the class definition, you refer to the object's attributes like so: variable = object.attribute # if you want to read the attribute's current value or object.attribute = variable # if you want to set the attribute to a new value and to its methods like so: variable = object.method(parameter1, parameter2, etc) Like all functions, methods can take a fixed number of parameters, an optional bunch of named parameters, or no parameters at all; they may return a value or they may not; you may want to use that value, or ignore it. Things to remember: -you can get a value from a method, but you can't assign to it: variable = object.method() but NOT object.method() = variable -the only visible difference between reading an attribute and calling a method with no parameters is the parentheses at the end. Don't forget them, and don't be misled by the similarity. Hope that helps... Thanks so much for the explanation Marc! My problem was, I wasn't seeing {member} as referring to the class objects {t} and {s}. Since it was, we now can use member just like any class object, and combine it with class functions (and class variables), such as {member.tell}. I had never in my short programming experience, seen an example like this. So I was confused, obviously, LOL. Makes perfect sense now. . .Thanks again Marc (and Alan, Dave) BTW, do you guys luv Python the way I do!?? I just luv the way everything works together so explicitly. I LUV learning this stuff!! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/24/2011 7:45 AM, Wayne Werner wrote: On Sun, Oct 23, 2011 at 11:06 PM, Marc Tompkins mailto:marc.tompk...@gmail.com>> wrote: Things to remember: -you can get a value from a method, but you can't assign to it: variable = object.method() but NOT object.method() = variable As a slight aside, you _can_ assign to the method name: object.method = variable #object.method is now whatever variable was I'm not aware of any valid reason to do this, that is to say I don't know of anything that you could do this way that you couldn't do another more readable/maintainable way. In my Python Pipelines program I deliberately reassign instance methods. Example: Given a (trivial) pipeline specification: "< foo.txt | > goo.txt" meaning: "open foo.txt, open goo.txt, read one record at a time from foo.txt, write it to goo.txt, at end close both files." The specification parser creates an instance of ReadFile and an instance of WriteFile, It must then "connect" these two instances, such that the output of the first goes to the input of the second. The IMHO easiest way to do this: instance1.output = instance2.input where output and input are methods. I've had other cases where the __init__method of a class determines some behaviors by assigning one of several methods to a particular method. I've had yet other cases where I swap methods during the first call, such that subsequent calls have different behavior. All of these could be done differently, but reassigning methods makes the most sense to me. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 24/10/11 11:17, lina wrote: a quick Q: Every time call the method, need go through the __initi__( ) part? No. __init__() is only called when an instance is first created. Here is an example in the IDLE: >>> class C: def __init__(self): print("I'm in init") def method(self): print("I'm in method") >>> c = C() I'm in init >>> c.method() I'm in method >>> c2 = C().method() # calls init for C(), then the method I'm in init I'm in method >>> HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On Sun, Oct 23, 2011 at 11:06 PM, Marc Tompkins wrote: > Things to remember: > -you can get a value from a method, but you can't assign to it: > variable = object.method() > but NOT > object.method() = variable > As a slight aside, you _can_ assign to the method name: object.method = variable #object.method is now whatever variable was I'm not aware of any valid reason to do this, that is to say I don't know of anything that you could do this way that you couldn't do another more readable/maintainable way. But I could be wrong! HTH, -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/24/2011 4:40 AM, Alan Gauld wrote: On 24/10/11 04:08, Chris Kavanagh wrote: Thanks so much for the help Alan. . .I'm not trying to beat this question into the ground, LOL, but let me see if I can ask it a better way. Marc has already given a good answer, but I'll try a slightly different approach to the same thing The differences are purely philosophical :-) So we have {member.tell} as the last line of code. So trying to understand this piece of code, {member} the variable is considered an object? Almost. member is an object because it is an instance of a class. We created instances of Teacher and Student (t and s) and put them in a list (members) with this code: t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) members = [t, s] Thus for member in members takes each object in the list and assigns it to member. We can read {for member in members} as "Let member take on, in turn, each value in the collection members" And making it more explicit with a while loop: index = 0 while index < len(members): member = members[index] member.tell() index += 1 > Therefore we can combine it with a function {tell()} using dot > notation?? Is this correct??? I haven't seen anything but an > object combined with a function using dot notation. dot notation is how we access anything that is defined inside the class definition. In this case there are only the __init__() and tell() methods but there could be data attributes too. Furthermore we can add data attributes to the objects by using the self parameter, as is done in __init__() So we end up with the instance containing the attributes defined at the class level (including those of its superclass) plus any added by the __init__() method which is called when the object is created (or instantiated). So for Teacher the attributes will be: __init__() tell() name - from SchoolMember.__init__() age - ditto salary - from Teacher.__init__() So we could have accessed any of those for the first iteration of the loop because member was a Teacher instance (t) using: member.tell, member.name, member.age, member.salary But the last one would have broken for the second object in the list since it was a student, which doesn't have a salary. So when dealing with a list of objects we have to restrict the access via dot notation to those attributes that are common to all objects in the collection - usually the ones defined in the super class. Python doesn't care whether the attributes we access are data or functions because in Python everything is an "object" (see Marc's post) But by putting parentheses after the object name Python treats the named object as a function (specifically a "callable" object) You can see this diffence at work in the interpreter if you type this: >>> def f(): ... return "Called f" ... >>> print f >>> print f() Called f See the difference the () made? Without it we print the function object called f. With it we print the result of calling the function f. Thats quite a subtle concept and usually hard for beginners to grasp but it is a very powerful concept and you will find it being used in more advanced programs, especially GUI development. > So I'm trying to figure out how we can combine the variable {member} with the function {tell}. Hope this question makes sense to you, LOL. Thanks again. How we combine it is done inside Python as part of the magic of classes and instantiation. Basically you can call any function defined inside a class and Python will automatically assign the first parameter of that function to the object instace that you are referencing at the time. Thus when we do for member in members: member.tell() Python recognises that first time round member refers to the object t which is an instanmce of Teacher. It then calls the tell() method within the Teacher class definition and assigns t as the self parameter. We could write it explicitly as: Teacher.tell(member) # where member is t On the second time round member is assigned to s and Python recognizes that s in an instance of Student. So it calls Student.tell(member) where member is s. The member.tell() notation is just a convenient way for us to write it and allows Python to do the hard work of figuring out what kind of object member is at any given time. [Aside: In true OOP speak we describe the process as "sending a message to the object. Thus member.tell() sends the message "tell" to the object "member" and the OOP framework invokes the corresponding method tell() in the corresponding class. In some OOP languages you can use different names for the messages and corresponding methods but in Python they are the same so we just talk about calling the method.] HTH, Yes, both of you gave great answers. I just couldn't get it straight in my head for some reason. And I do get it now. I just wasn't seeing {member} as an instance of a class. I guess because I'm used to seeing simple straightforward examples of classes
Re: [Tutor] Simple Question On A Method (in subclass)
a quick Q: Every time call the method, need go through the __initi__( ) part? Thanks, I attached the one I used to practice fast-typing: #!/usr/bin/python3 class SchoolMember: '''Represents any school members.''' def __init__(self,name,age): self.name = name self.age = age print("Initialized School Memeber: ", self.name) def tell(self): '''Tell my details.''' print("Name:", self.name, "Age: ", self.age) class Teacher(SchoolMember): '''Represents a Teacher''' def __init__(self,name,age,salary): SchoolMember.__init__(self,name,age) self.salary = salary print("Initialized Teacher", self.name) def tell(self): SchoolMember.tell(self) print("Salary:", self.salary) class Student(SchoolMember): '''Represent a student.''' def __init__(self,name,age,marks): SchoolMember.__init__(self,name,age) self.marks = marks print("Initialized Student:", self.name) def tell(self): SchoolMember.tell(self) print("Marks: ", self.marks) t = Teacher('Mrs. Shrividya',40,3) s = Student('Swaroop',22,75) print() members=[t,s] for member in members: member.tell() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 24/10/11 04:08, Chris Kavanagh wrote: Thanks so much for the help Alan. . .I'm not trying to beat this question into the ground, LOL, but let me see if I can ask it a better way. Marc has already given a good answer, but I'll try a slightly different approach to the same thing The differences are purely philosophical :-) So we have {member.tell} as the last line of code. So trying to understand this piece of code, {member} the variable is considered an object? Almost. member is an object because it is an instance of a class. We created instances of Teacher and Student (t and s) and put them in a list (members) with this code: t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) members = [t, s] Thus for member in members takes each object in the list and assigns it to member. We can read {for member in members} as "Let member take on, in turn, each value in the collection members" And making it more explicit with a while loop: index = 0 while index < len(members): member = members[index] member.tell() index += 1 > Therefore we can combine it with a function {tell()} using dot > notation?? Is this correct??? I haven't seen anything but an > object combined with a function using dot notation. dot notation is how we access anything that is defined inside the class definition. In this case there are only the __init__() and tell() methods but there could be data attributes too. Furthermore we can add data attributes to the objects by using the self parameter, as is done in __init__() So we end up with the instance containing the attributes defined at the class level (including those of its superclass) plus any added by the __init__() method which is called when the object is created (or instantiated). So for Teacher the attributes will be: __init__() tell() name - from SchoolMember.__init__() age- ditto salary - from Teacher.__init__() So we could have accessed any of those for the first iteration of the loop because member was a Teacher instance (t) using: member.tell, member.name, member.age, member.salary But the last one would have broken for the second object in the list since it was a student, which doesn't have a salary. So when dealing with a list of objects we have to restrict the access via dot notation to those attributes that are common to all objects in the collection - usually the ones defined in the super class. Python doesn't care whether the attributes we access are data or functions because in Python everything is an "object" (see Marc's post) But by putting parentheses after the object name Python treats the named object as a function (specifically a "callable" object) You can see this diffence at work in the interpreter if you type this: >>> def f(): ... return "Called f" ... >>> print f >>> print f() Called f See the difference the () made? Without it we print the function object called f. With it we print the result of calling the function f. Thats quite a subtle concept and usually hard for beginners to grasp but it is a very powerful concept and you will find it being used in more advanced programs, especially GUI development. > So I'm trying to figure out how we can combine the variable {member} with the function {tell}. Hope this question makes sense to you, LOL. Thanks again. How we combine it is done inside Python as part of the magic of classes and instantiation. Basically you can call any function defined inside a class and Python will automatically assign the first parameter of that function to the object instace that you are referencing at the time. Thus when we do for member in members: member.tell() Python recognises that first time round member refers to the object t which is an instanmce of Teacher. It then calls the tell() method within the Teacher class definition and assigns t as the self parameter. We could write it explicitly as: Teacher.tell(member) # where member is t On the second time round member is assigned to s and Python recognizes that s in an instance of Student. So it calls Student.tell(member) where member is s. The member.tell() notation is just a convenient way for us to write it and allows Python to do the hard work of figuring out what kind of object member is at any given time. [Aside: In true OOP speak we describe the process as "sending a message to the object. Thus member.tell() sends the message "tell" to the object "member" and the OOP framework invokes the corresponding method tell() in the corresponding class. In some OOP languages you can use different names for the messages and corresponding methods but in Python they are the same so we just talk about calling the method.] HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/
Re: [Tutor] Simple Question On A Method (in subclass)
On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh wrote: > So we have {member.tell} as the last line of code. So trying to understand > this piece of code, {member} the variable is considered an object? Therefore > we can combine it with a function {tell()} using dot notation?? Is this > correct??? I haven't seen anything but an object combined with a function > using dot notation. When I say "object", I mean an "object" created from a > class. So I'm trying to figure out how we can combine the variable {member} > with the function {tell}. Hope this question makes sense to you, LOL. Thanks > again. > First of all: other languages distinguish between variables and objects, and between functions and objects, but in Python both variables and functions are objects. EVERYTHING is an object. This is an important thing to remember - even if you never create classes of your own (which would be a terrible waste, BTW) a lot of the language won't make sense unless you remember that everything's an object. Second, the line "members = [t, s]" creates a list "members" (which is also an object, by the way!) containing two objects - "t" is a Teacher, "s" is a Student - which are both subclassed from SchoolMember. The line "for member in members" means: step through the list "members" and work with each object we find in it; let's call that object "member" while we're working with it. As soon as we finish with the first object and move on to the next, call the next one "member" - and so on. The beauty of this approach is that it simply doesn't matter what the contents of the list are - one could be a Student, the next a WoollyMammoth - and as long as your code only references methods and attributes that work for all the items in the list, Python won't care. Third, dot notation: objects have "methods" (which in non-OOP contexts would be called "functions") and "attributes" (variables, more or less.) >From outside of the class definition, you refer to the object's attributes like so: variable = object.attribute # if you want to read the attribute's current value or object.attribute = variable # if you want to set the attribute to a new value and to its methods like so: variable = object.method(parameter1, parameter2, etc) Like all functions, methods can take a fixed number of parameters, an optional bunch of named parameters, or no parameters at all; they may return a value or they may not; you may want to use that value, or ignore it. Things to remember: -you can get a value from a method, but you can't assign to it: variable = object.method() but NOT object.method() = variable -the only visible difference between reading an attribute and calling a method with no parameters is the parentheses at the end. Don't forget them, and don't be misled by the similarity. Hope that helps... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/23/2011 8:28 PM, Alan Gauld wrote: On 24/10/11 00:54, Chris Kavanagh wrote: Speaking of the last line of code, I have a question about that also. The last line should have been (without my error) {member.tell()}. My question is, why couldn't this last line have been {print member}?? It could have been, but the output would have been different. When you call print it automatically calls the str() function on the arguments. So {print member} would actually print out {str(member)}. But str(member) is a message to say that member is an instance of a class, not a list of the attributes formatted in a specific way. Now Swaroop could have chosen to implement a __str__() method in the classes in which case print would have done what you expect. (But I guess he felt that would be too advanced at this stage in his tutor) Another option would have been to make tell() return a string rather than print it, in which case you could have used {print member.tell()} Every example of an iterator I've seen until this point of my learning, has had a print statement then the item variable. Thats just a coincidence with how you've seen iterators used. An iterator allows you to do anything you like to the items being iterated over. It just so happens that so far you have only seen them being printed. Printing is only one, and is far from the most common, use of iterators. the item variable to call the function {tell()}. This is sorta confusing to me. What if there was no function to print out the Student and Teacher variable?? You would have to rely on the str() conversion - which will always print something, even if its not very helpful! Or, you could manually print the contents of the member - but that gets very complicated where you have mixed sub and superclasses in a single collection. > How would the iterator been printed out in that case?? Being picky, you are not printing the iterator, you are printing the items returned by the iterator. But ignoring that, you would do one of the options I described above: 1) print the object using its built in str() convertion 2) create your own string conversion method (__str__() ) 3) print the individual data members by hand Personally I would choose the second one, but modifying the code is not always possible, in which case, option 3 wins. Option 1 is usually pretty useless! HTH Thanks so much for the help Alan. . .I'm not trying to beat this question into the ground, LOL, but let me see if I can ask it a better way. So we have {member.tell} as the last line of code. So trying to understand this piece of code, {member} the variable is considered an object? Therefore we can combine it with a function {tell()} using dot notation?? Is this correct??? I haven't seen anything but an object combined with a function using dot notation. When I say "object", I mean an "object" created from a class. So I'm trying to figure out how we can combine the variable {member} with the function {tell}. Hope this question makes sense to you, LOL. Thanks again. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 24/10/11 00:54, Chris Kavanagh wrote: Speaking of the last line of code, I have a question about that also. The last line should have been (without my error) {member.tell()}. My question is, why couldn't this last line have been {print member}?? It could have been, but the output would have been different. When you call print it automatically calls the str() function on the arguments. So {print member} would actually print out {str(member)}. But str(member) is a message to say that member is an instance of a class, not a list of the attributes formatted in a specific way. Now Swaroop could have chosen to implement a __str__() method in the classes in which case print would have done what you expect. (But I guess he felt that would be too advanced at this stage in his tutor) Another option would have been to make tell() return a string rather than print it, in which case you could have used {print member.tell()} Every example of an iterator I've seen until this point of my learning, has had a print statement then the item variable. Thats just a coincidence with how you've seen iterators used. An iterator allows you to do anything you like to the items being iterated over. It just so happens that so far you have only seen them being printed. Printing is only one, and is far from the most common, use of iterators. the item variable to call the function {tell()}. This is sorta confusing to me. What if there was no function to print out the Student and Teacher variable?? You would have to rely on the str() conversion - which will always print something, even if its not very helpful! Or, you could manually print the contents of the member - but that gets very complicated where you have mixed sub and superclasses in a single collection. > How would the iterator been printed out in that case?? Being picky, you are not printing the iterator, you are printing the items returned by the iterator. But ignoring that, you would do one of the options I described above: 1) print the object using its built in str() convertion 2) create your own string conversion method (__str__() ) 3) print the individual data members by hand Personally I would choose the second one, but modifying the code is not always possible, in which case, option 3 wins. Option 1 is usually pretty useless! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/23/2011 6:22 PM, Dave Angel wrote: (please do REPLY-ALL, or at least add the mailing list to the recipient list. Otherwise, only one person will see the message. I'm forwarding it to the group with my comments added.) Original Message Subject: Re: [Tutor] Simple Question On A Method (in subclass) Date: Sun, 23 Oct 2011 16:53:40 -0400 From: Chris Kavanagh Organization: Home Office To: d...@davea.name On 10/22/2011 6:59 PM, Dave Angel wrote: My question is regarding the tell methods in the subclasses,the code {SchoolMember.tell(self)}, in the class Teacher & Student. I just don't understand what this is doing? Calling the first method {def tell} from the parent class, I assume? There is already a print statement in each of the subclass {def tell} showing details, why call another print statement (from parent class {def tell})?? I know this should be simple, but I'm confused. LOL, obviously. Thank you in advance! Welcome to the python-tutor list. We are all volunteers here, and most, like me, ask questions as well as answering them. it's a two-way street. The whole point of subclassing is to share either code, data, or both. In this example, the amount of shared data is just the name and age, and the shared method is tell(). Because the parent knows how to 'tell' its information, the child doesn't need to. So instead of altering the prints in both the child classes to print name & age, you let the common code in the parent handle it. It becomes more clearly a win when you have much more data, or much more complex methods involved. But you have to start simple. BTW, there were some transcription errors in the email. For example, the code as written would be using a separate line for Salary on Mrs. Shrividya's record. And you're missing the content of the for member in members: loop. No problem, but it might have affected our discussion. Did you retype it all, or was it just a glitch? Presumably you know how to copy/paste from and to a console prompt? Chris Kavanagh said: Thanks to both Alan Gauld & Dave Angel for the help!!! I'm not sure how the transcrption errors happened, I copied & pasted. Must have not copied the entire thing. I'll correct it below. Speaking of the last line of code, I have a question about that also. The last line should have been (without my error) {member.tell()}. My question is, why couldn't this last line have been {print member}?? Every example of an iterator I've seen until this point of my learning, has had a print statement then the item variable. Instead, Swaroop uses the item variable to call the function {tell()}. This is sorta confusing to me. What if there was no function to print out the Student and Teacher variable?? How would the iterator been printed out in that case?? Thanks again, in advance. Here's the correct code. class SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.age = age print '(Initialized SchoolMember: %s)' % self.name def tell(self): '''Tell my details.''' print 'Name:"%s" Age:"%s"' % (self.name, self.age), class Teacher(SchoolMember): '''Represents a teacher.''' def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) self.salary = salary print '(Initialized Teacher: %s)' % self.name def tell(self): SchoolMember.tell(self) print 'Salary: "%d"' % self.salary class Student(SchoolMember): '''Represents a student.''' def __init__(self, name, age, marks): SchoolMember.__init__(self, name, age) self.marks = marks print '(Initialized Student: %s)' % self.name def tell(self): SchoolMember.tell(self) print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) print # prints a blank line members = [t, s] for member in members: member.tell() # works for both Teachers and Students Output $ python inherit.py (Initialized SchoolMember: Mrs. Shrividya) (Initialized Teacher: Mrs. Shrividya) (Initialized SchoolMember: Swaroop) (Initialized Student: Swaroop) Name:"Mrs. Shrividya" Age:"40" Salary: "3" Name:"Swaroop" Age:"22" Marks: "75" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> You always have a choice of where to put useful code. if you put it in member functions, it can be shared by all the callers. There's no right or wrong way to do it. The only "rule" I can think of is not to mix significant calculations with prints. Make them two different methods, so the user of the class c
Re: [Tutor] Simple Question On A Method (in subclass)
(please do REPLY-ALL, or at least add the mailing list to the recipient list. Otherwise, only one person will see the message. I'm forwarding it to the group with my comments added.) Original Message Subject: Re: [Tutor] Simple Question On A Method (in subclass) Date: Sun, 23 Oct 2011 16:53:40 -0400 From: Chris Kavanagh Organization: Home Office To: d...@davea.name On 10/22/2011 6:59 PM, Dave Angel wrote: My question is regarding the tell methods in the subclasses,the code {SchoolMember.tell(self)}, in the class Teacher & Student. I just don't understand what this is doing? Calling the first method {def tell} from the parent class, I assume? There is already a print statement in each of the subclass {def tell} showing details, why call another print statement (from parent class {def tell})?? I know this should be simple, but I'm confused. LOL, obviously. Thank you in advance! Welcome to the python-tutor list. We are all volunteers here, and most, like me, ask questions as well as answering them. it's a two-way street. The whole point of subclassing is to share either code, data, or both. In this example, the amount of shared data is just the name and age, and the shared method is tell(). Because the parent knows how to 'tell' its information, the child doesn't need to. So instead of altering the prints in both the child classes to print name & age, you let the common code in the parent handle it. It becomes more clearly a win when you have much more data, or much more complex methods involved. But you have to start simple. BTW, there were some transcription errors in the email. For example, the code as written would be using a separate line for Salary on Mrs. Shrividya's record. And you're missing the content of the for member in members: loop. No problem, but it might have affected our discussion. Did you retype it all, or was it just a glitch? Presumably you know how to copy/paste from and to a console prompt? Chris Kavanagh said: Thanks to both Alan Gauld & Dave Angel for the help!!! I'm not sure how the transcrption errors happened, I copied & pasted. Must have not copied the entire thing. I'll correct it below. Speaking of the last line of code, I have a question about that also. The last line should have been (without my error) {member.tell()}. My question is, why couldn't this last line have been {print member}?? Every example of an iterator I've seen until this point of my learning, has had a print statement then the item variable. Instead, Swaroop uses the item variable to call the function {tell()}. This is sorta confusing to me. What if there was no function to print out the Student and Teacher variable?? How would the iterator been printed out in that case?? Thanks again, in advance. Here's the correct code. class SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.age = age print '(Initialized SchoolMember: %s)' % self.name def tell(self): '''Tell my details.''' print 'Name:"%s" Age:"%s"' % (self.name, self.age), class Teacher(SchoolMember): '''Represents a teacher.''' def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) self.salary = salary print '(Initialized Teacher: %s)' % self.name def tell(self): SchoolMember.tell(self) print 'Salary: "%d"' % self.salary class Student(SchoolMember): '''Represents a student.''' def __init__(self, name, age, marks): SchoolMember.__init__(self, name, age) self.marks = marks print '(Initialized Student: %s)' % self.name def tell(self): SchoolMember.tell(self) print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya', 40, 3) s = Student('Swaroop', 22, 75) print # prints a blank line members = [t, s] for member in members: member.tell() # works for both Teachers and Students Output $ python inherit.py (Initialized SchoolMember: Mrs. Shrividya) (Initialized Teacher: Mrs. Shrividya) (Initialized SchoolMember: Swaroop) (Initialized Student: Swaroop) Name:"Mrs. Shrividya" Age:"40" Salary: "3" Name:"Swaroop" Age:"22" Marks: "75" >>>>>>>>>>>&g
Re: [Tutor] Simple Question On A Method (in subclass)
On 22/10/11 23:10, Chris Kavanagh wrote: My question is regarding the tell methods in the subclasses,the code {SchoolMember.tell(self)}, in the class Teacher & Student. I just don't understand what this is doing? Calling the first method {def tell} from the parent class, I assume? Thats right, the child class is calling its parent class method. This saves the child class from copying the code in the parent. There is already a print statement in each of the subclass {def tell} > showing details, why call another print statement In this case it doesn't same much code (a few charactrs less) but with a more complex method it couyld be a lot iof saving. However, that's not the only advantage. If you want to change the format of the parent message you only need to change the parent code, the children get the change for free. Also if you copied the code into each child there is a real risk that you'd get the formatting slightly different. Then when you try to call the tell() method of a list of objects, some parents and some children the outputs would look different - thats messy. class SchoolMember: def tell(self): '''Tell my details.''' print 'Name:"%s" Age:"%s"' % (self.name, self.age), class Teacher(SchoolMember): def tell(self): SchoolMember.tell(self) print 'Salary: "%d"' % self.salary class Student(SchoolMember): def tell(self): SchoolMember.tell(self) print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya',40,3) s = Student('Swaroop',22,75) members = [t,s] for member in members: member.tell() Name:"Mrs. Shrividya" Age:"40" Salary: "3" Name:"Swaroop" Age:"22" Marks: "75" HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question On A Method (in subclass)
On 10/22/2011 06:10 PM, Chris Kavanagh wrote: Hello, First, thank you for providing this GREAT service, & THANKS to everyone who contributes. It's greatly appreciated. . .I'm new to Python (2.7, Win XP) & new to programming in general. I have been studying on my own for about a month now. I believe I have a good grasp of the basics. Secondly, I have several questions about this piece of code from 'A Byte Of Python' by Swaroop. Hope that's ok to use this code. I guess I could try to write a new ex. on my own, but, figured it would be too confusing. My question is regarding the tell methods in the subclasses,the code {SchoolMember.tell(self)}, in the class Teacher & Student. I just don't understand what this is doing? Calling the first method {def tell} from the parent class, I assume? There is already a print statement in each of the subclass {def tell} showing details, why call another print statement (from parent class {def tell})?? I know this should be simple, but I'm confused. LOL, obviously. Thank you in advance! class SchoolMember: '''Represents any school member.''' def __init__(self,name,age): self.name = name self.age = age print '(Initialized SchoolMember: %s)' %self.name def tell(self): '''Tell my details.''' print 'Name:"%s" Age:"%s"' % (self.name, self.age), class Teacher(SchoolMember): '''Represents a teacher''' def __init__(self,name,age,salary): SchoolMember.__init__(self,name,age) self.salary = salary print '(Initialized Teacher: %s)' %self.name def tell(self): SchoolMember.tell(self) print 'Salary: "%d"' % self.salary class Student(SchoolMember): '''Represents a student.''' def __init__(self,name,age,marks): SchoolMember.__init__(self,name,age) self.marks = marks print '(Initialized Student: %s)' %self.name def tell(self): SchoolMember.tell(self) print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya',40,3) s = Student('Swaroop',22,75) print ##prints a blank line members = [t,s] for member in members: OUTPUT $ python inherit.py (Initialized SchoolMember: Mrs. Shrividya) (Initialized Teacher: Mrs. Shrividya) (Initialized SchoolMember: Swaroop) (Initialized Student: Swaroop) Name:"Mrs. Shrividya" Age:"40" Salary: "3" Name:"Swaroop" Age:"22" Marks: "75" Welcome to the python-tutor list. We are all volunteers here, and most, like me, ask questions as well as answering them. it's a two-way street. The whole point of subclassing is to share either code, data, or both. In this example, the amount of shared data is just the name and age, and the shared method is tell(). Because the parent knows how to 'tell' its information, the child doesn't need to. So instead of altering the prints in both the child classes to print name & age, you let the common code in the parent handle it. It becomes more clearly a win when you have much more data, or much more complex methods involved. But you have to start simple. BTW, there were some transcription errors in the email. For example, the code as written would be using a separate line for Salary on Mrs. Shrividya's record. And you're missing the content of the for member in members: loop. No problem, but it might have affected our discussion. Did you retype it all, or was it just a glitch? Presumably you know how to copy/paste from and to a console prompt? -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor