[Tutor] Simple Question On A Method (in subclass)

Marc Tompkins marc.tompkins at gmail.com
Mon Oct 24 06:06:05 CEST 2011

On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh <ckava1 at 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
    object.attribute = variable # if you want to set the attribute to a new

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...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111023/466fe7f6/attachment.html>

More information about the Tutor mailing list