[Tutor] Re: Newbie OOP Question.(diff between function, module and class)
SA
sarmstrong13@mac.com
Mon, 10 Jun 2002 10:39:37 -0500
On 6/10/02 10:17 AM, "Skip Montanaro" <skip@pobox.com> wrote:
> Here's my take on things:
>
> * A function is an action to perform using a set of input arguments and
> returning a set of output arguments. For example:
>
>>>> print sin(47)
> 0.123573122745
>
Got it.
> * A class associates a chunk of data with a set of functions which operate
> on that data. The first argument to each function in a class (typically
> called a "method" in Python) is a reference to the data stored in that
> instance of the class. For example:
>
> import math
> class Point:
> def __init__(self, x, y):
> self.x = x
> self.y = y
>
> def polar(self):
> return math.sqrt(self.x**2+self.y**2), math.atan2(self.y, self.x)
>
So the "def __init__" is a function in the class and is assigning the
variables x and y as attributes of the class, correct? If so, are the
variables x and y supplied outside of the class (I guess this would be
global)?
> * A module is an object that partitions the namespace in a hierarchy. For
> example, you can have a function named "sin" which is distinct from the
> sin function in the math module ("math.sin"). Modules used in a
> straightforward way only add a single extra level of hierarchy to the
> namespace. Packages generalize this concept to multiple levels (modules
> inside modules).
A module is more or less, in very general terms, a package of classes and/or
functions that are then imported into the __main__ program, correct?
One last question, sorry if this is very basic but I feel these terms need
better definitions for newbies like me (the only other languages I have
experience with are Qbasic and HTML) what is a namespae? Is that analagous
to pwd of the program? For instance, if the program is currently in one are
of the script doing work, would that be considered the namespace?
See everything I read explains these terms in definitions that are readibly
understandable by people with programming experience. I kind of need a
"layman's" terms tutorial because I'm so new to programming.
But this has been very helpful dialogue in defining these terms and I feel
the people on this list have been very helpful. Thank You all.
Thanks.
SA