On 10/24/2011 12:06 AM, Marc Tompkins wrote:
On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh <cka...@msn.com
<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

Reply via email to